如何删除树中的节点?
Posted
技术标签:
【中文标题】如何删除树中的节点?【英文标题】:How to delete a node in a tree? 【发布时间】:2014-02-26 13:16:28 【问题描述】:我正在尝试使用指针在 C++ 中实现一个简单的二叉树。
我已经成功实现了插入和遍历,但是我在尝试删除节点时遇到了一些问题:
我的主要功能是:
main()
node* root=createNode(1);
root->left=createNode(2);
root->right=createNode(3);
root->left->left=createNode(4);
root->left->right=createNode(5);
root->right->left=createNode(6);
root->right->right=createNode(7);
inorder(root);
//till here it works fine
delete root->left->left; //problem starts here
cout<<"\n";
inorder(root); //exception is thrown here...
return 0;
`
中序函数是非常基本的递归函数:
void inorder(node* root)
if(root!=NULL)
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
谁能告诉我删除行有什么问题?
【问题讨论】:
抛出什么异常?你确定这是个例外吗?createNode
是否将left
和right
指针设置为null
?
@JosephMansfield,我敢打赌是 sigsegv 导致应用程序崩溃。
【参考方案1】:
在delete
之后,尝试访问已删除的指针会导致此问题。你可能想添加
root->left->left = 0
在删除行之后。
【讨论】:
【参考方案2】:您必须将 root->left->left
指针设置为 NULL
,即它没有任何意义
在你的代码中
main()
node* root=createNode(1);
root->left=createNode(2);
root->right=createNode(3);
root->left->left=createNode(4);
root->left->right=createNode(5);
root->right->left=createNode(6);
root->right->right=createNode(7);
inorder(root);
//Now, Complete program will work
delete root->left->left = 0; //problem solved
cout<<"\n";
inorder(root); // Do you still face any problem ?
return 0;
【讨论】:
【参考方案3】:删除节点后,需要将父节点的左指针重置为零。 不这样做可能会导致不可预知的结果,具体取决于内存管理的实现方式。一些系统对释放内存的引用表示例外
【讨论】:
【参考方案4】:Your trying to delete node which has not null nodes in both right and left side, so you to delete node like this ..
/* deletes a node from the binary search tree */
void delete ( struct btreenode **root, int num )
int found ;
struct btreenode *parent, *x, *xsucc ;
/* if tree is empty */
if ( *root == NULL )
printf ( "\nTree is empty" ) ;
return ;
parent = x = NULL ;
/* call to search function to find the node to be deleted */
search ( root, num, &parent, &x, &found ) ;
/* if the node to deleted is not found */
if ( found == FALSE )
printf ( "\nData to be deleted, not found" ) ;
return ;
/* if the node to be deleted has two children */
if ( x -> leftchild != NULL && x -> rightchild != NULL )
parent = x ;
xsucc = x -> rightchild ;
while ( xsucc -> leftchild != NULL )
parent = xsucc ;
xsucc = xsucc -> leftchild ;
x -> data = xsucc -> data ;
x = xsucc ;
/* if the node to be deleted has no child */
if ( x -> leftchild == NULL && x -> rightchild == NULL )
if ( parent -> rightchild == x )
parent -> rightchild = NULL ;
else
parent -> leftchild = NULL ;
free ( x ) ;
return ;
/* if the node to be deleted has only rightchild */
if ( x -> leftchild == NULL && x -> rightchild != NULL )
if ( parent -> leftchild == x )
parent -> leftchild = x -> rightchild ;
else
parent -> rightchild = x -> rightchild ;
free ( x ) ;
return ;
/* if the node to be deleted has only left child */
if ( x -> leftchild != NULL && x -> rightchild == NULL )
if ( parent -> leftchild == x )
parent -> leftchild = x -> leftchild ;
else
parent -> rightchild = x -> leftchild ;
free ( x ) ;
return ;
【讨论】:
以上是关于如何删除树中的节点?的主要内容,如果未能解决你的问题,请参考以下文章