您的位置:首页 > 其它

二叉树的遍历 插入 查找 删除 最大值 最小值 前驱 后继节点的查找

2015-04-29 21:57 801 查看
#include <iostream>
#include<queue>
using namespace std;
//binery search-tree ADT
struct treeNode
{
int value;
treeNode* left;
treeNode*right;
treeNode*parent;
};
//fun1  二叉树的插入即建立
treeNode * treeInsert(treeNode* head,int n)
{
treeNode* newNode=new treeNode;
newNode->left=newNode->right=NULL;
newNode->parent=NULL;
newNode->value=n;
if (head==NULL)
{
newNode->parent=NULL;
return newNode;
}

treeNode *head1=head,*head2=NULL;
while (head1!=NULL)//先确定待插入的父亲节点
{
head2=head1;
if(head1->value>n)
head1=head1->left;
else
head1=head1->right;

}
newNode->parent=head2;
if(head2->value>n)
{
head2->left=newNode;

}

else
{
head2->right=newNode;
}

return head;
}
void PrintInOrderTreeWalk(treeNode* head)
{
if(head!=NULL)
{
PrintInOrderTreeWalk(head->left);
cout<<head->value<<" ";
PrintInOrderTreeWalk(head->right);

}
}
void PrintTreeWalkByFloor(treeNode* head)
{
if(head==NULL)
return ;
queue<treeNode*>myQueue;
myQueue.push(head);
treeNode* temp=NULL;
while (!myQueue.empty())
{
temp=myQueue.front();
cout<<temp->value<<" ";
if(temp->left!=NULL)
myQueue.push(temp->left);
if(temp->right!=NULL)
myQueue.push(temp->right);
myQueue.pop();
}
}
treeNode* searchNode(treeNode * head,int k)
{
treeNode *head1=head;
while (head1!=NULL&&head1->value!=k)
{
if(head1->value>k)
head1=head1->left;
else
head1=head1->right;

}
return head1;//if 未找到return NULL

}
treeNode*Maximum(treeNode* head)
{
treeNode* head1=head,*head2=NULL;
while (head1!=NULL)
{
head2=head1;
head1=head1->right;
}
return head2;
}

treeNode* Minimum(treeNode *head)
{
treeNode* head1=head,*head2=NULL;
while(head1!=NULL)
{
head2=head1;
head1=head1->left;
}
return head2;

}
treeNode * Successor(treeNode * head,int k)//k中序遍历的后一个节点
{
treeNode* kValueNode=searchNode(head,k);//先找到该节点
if(kValueNode==NULL)
return NULL;
if(kValueNode->right!=NULL)
{
return Minimum(kValueNode->right);
}
treeNode * y=kValueNode;
while(y!=NULL&&((y->left)!=kValueNode))
{
kValueNode=y;
y=y->parent;
}
return y;
}
treeNode *Predecessor(treeNode * head,int k)//返回值为k的前一个节点
{

treeNode* kValueNode=searchNode(head,k);
if(kValueNode==NULL)
return NULL;
if(kValueNode->left!=NULL)
{
return  Maximum(kValueNode);
}
treeNode*  parent=kValueNode->parent;
while(parent!=NULL&&(parent->right)!=kValueNode)
{
kValueNode=parent;
parent=parent->parent;
}
return parent;

}
treeNode*  deleteNode(treeNode* head,int value)
{ treeNode* head1=head;
treeNode* toBeDeleteNode=searchNode(head,value);
if(toBeDeleteNode==NULL)
return NULL;
if(toBeDeleteNode->left!=NULL&&toBeDeleteNode->right!=NULL)//如果待删节点有左右子树,用后续节点覆盖
{
treeNode* next=Successor(toBeDeleteNode,toBeDeleteNode->value);//找到后续节点
toBeDeleteNode->value=next->value;//覆盖待删节点
next->parent->right=next->right;//让后续节点的父亲的右指针指向后续节点的右孩子 一定是左指针?反证法
if(next->right!=NULL)
next->right->parent=next->parent;
delete next;//删除后续节点
return head;
}
if(toBeDeleteNode==head)
if(head1->left==NULL&&head1->right==NULL)
{
delete head1;
return NULL;

}
else if
(head1->left!=NULL)
{
head=head1->left;
head->parent==NULL;
delete head1;
return head;
}
else
{
head=head1->right;
head->parent==NULL;
delete head1;
return head;
}

bool isLeftChild=(toBeDeleteNode==toBeDeleteNode->parent->left); //isleftChile 表示待删节点是否为父亲的子节点
if(toBeDeleteNode->left!=NULL||toBeDeleteNode->right!=NULL)//待删节点只有一个孩子
{

if(toBeDeleteNode->left==NULL)
{
if(isLeftChild)
{
toBeDeleteNode->parent->left=toBeDeleteNode->right;
}
else
{
toBeDeleteNode->parent->right=toBeDeleteNode->right;
}
toBeDeleteNode->right->parent=toBeDeleteNode->parent;
delete toBeDeleteNode;
return head;
}
if(toBeDeleteNode->right==NULL)
{
if(isLeftChild)
{
toBeDeleteNode->parent->left=toBeDeleteNode->left;
}
else
{
toBeDeleteNode->parent->right=toBeDeleteNode->left;
}
toBeDeleteNode->left->parent=toBeDeleteNode->parent;
delete toBeDeleteNode;
return head;
}
}
else
{  if(isLeftChild)
toBeDeleteNode->parent->left=NULL;
else
toBeDeleteNode->parent->right=NULL;
delete toBeDeleteNode;
return head;
}

}
/*treeNode *transToList(treeNode *head)//将二叉树在不使用其它空间的情况下 转化为已排序的双向链表
{

}*/
int main()
{
treeNode *head=NULL;
head=treeInsert(head,2);
head=treeInsert(head,4);
head=treeInsert(head,1);
head=treeInsert(head,18);
head=treeInsert(head,23);
head=treeInsert(head,78);
head=treeInsert(head,89);
//单元测试1:

//head=searchNode(head,89);
//head=Maximum(head);
head=deleteNode(head,4);
head=deleteNode(head,89);
//if(head!=NULL)
//cout<<head->value;
PrintTreeWalkByFloor(head);
//PrintInOrderTreeWalk(head);

}
各操作的平均时间性能大都为O(lgn )花几个小时自己实现一遍还是很有收获的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐