?? delete tree.txt
字號:
Write a C program to delete a tree (i.e, free up its nodes)
Discuss it!
struct Bintree{
int element;
struct Bintree *left;
struct Bintree *right;
};
typedef struct Bintree* Tree;
Tree findMin( Tree T) // Recursively finding min element
{
if( !T )
return NULL;
else if( !T->left )
return T;
else
return findMin( T->left );
}
Tree DeleteTree( int x, Tree T) // To Delete whole tree recursively
{
if(!T)
{
DeleteTree(T->left);
DeleteTree(T->right);
free(T);
}
return NULL;
}
Tree DeleteNode( int x, Tree T ) // To Delete a Node with element x
{
Tree tmp;
if(!T)
return NULL;
else if( x < T->element )
T->left = DeleteNode( x, T->left );
else if( x > T->element )
T->right = DeleteNode( x, T->right );
else if( T->left && T->right )
{
tmp = findMin( T-> right );
T->element = tmp->element;
T->right = DeleteNode( T->element, T->right );
}
else
{
tmp = T;
if( T->left )
T = T->left;
else if( T->right )
T = T->right;
free(tmp);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -