#include <IOSTREAM.H>
#include <STDLIB.H>
//二叉树的生成和释放
typedef struct Node
{
int data;
struct Node * pParent;
struct Node * pLeftChild;
struct Node * pRightChild;
}Node;
Node * Create_BTree(int *array,Node* pParent=NULL)//二叉排序树的创建,按照先序遍历的方法进行构造
{
static int i=0;
if (array[i]==0)
{
return NULL;
}
Node *temp=(Node *)malloc(sizeof(Node));
temp->data=array[i];
temp->pParent=pParent;
i++;
temp->pLeftChild=Create_BTree(array,temp);
i++;
temp->pRightChild=Create_BTree(array,temp);
return temp;
}
void Mid_Order(Node* tree)//二叉排序树的中序遍历
{
if (tree==NULL)
{
return;
}
Mid_Order(tree->pLeftChild);
cout<<tree->data<<" ";
Mid_Order(tree->pRightChild);
}
void Destroy_BTree(Node* tree)//二叉排序树的是否
{
if (tree==NULL)
{
return;
}
Destroy_BTree(tree->pLeftChild);
Destroy_BTree(tree->pRightChild);
free(tree);
}
Node* Tree_Search(Node* tree,int x)//二叉排序树的查找
{
Node* temp=tree;
while(temp)
{
if(temp->data==x)
break;
else if(temp->data>x)
temp=temp->pLeftChild;
else
temp=temp->pRightChild;
}
return temp;
}
Node * Tree_Minimum(Node* tree)//二叉排序树的最小节点
{
while(tree&&tree->pLeftChild)
{
tree=tree->pLeftChild;
}
return tree;
}
Node * Tree_Maximum(Node* tree)//二叉排序树的最大节点
{
while(tree&&tree->pRightChild)
{
tree=tree->pRightChild;
}
return tree;
}
Node * Tree_Successor(Node *p)//返回节点p的后继//中序遍历
{
if(p==NULL)
return p;
else if(p->pRightChild)
return Tree_Minimum(p->pRightChild);
else
{
while(p->pParent&&p->pParent->pRightChild==p)
p=p->pParent;
return p->pParent;
}
}
Node *Tree_PreDecessor(Node* p)//中序遍历 求p的前驱节点
{
if (p==NULL)
return p;
else if(p->pLeftChild)
return Tree_Maximum(p->pLeftChild);
else
{
while(p->pParent&&p->pParent->pLeftChild==p)
p=p->pParent;
return p->pParent;
}
}
void Tree_Insert(Node* &tree,int x)//给二叉排序树插入新节点
{
Node * pNodeNew=(Node*)malloc(sizeof(Node));
pNodeNew->pLeftChild=NULL;
pNodeNew->pRightChild=NULL;
pNodeNew->data=x;
if(tree==NULL)
{
pNodeNew->pParent=NULL;
tree=pNodeNew;
return;
}
Node * pTempParent=NULL,*pTemp=tree;
while(pTemp)
{
pTempParent=pTemp;
if (pTemp->data<x)
{
pTemp=pTemp->pRightChild;
}
else
{
pTemp=pTemp->pLeftChild;
}
}
pNodeNew->pParent=pTempParent;
if (pTempParent->data<x)
{
pTempParent->pRightChild=pNodeNew;
}
else
pTempParent->pLeftChild=pNodeNew;
}
void Tree_Delete(Node* &tree,Node *p)//删除二叉排序树中一个节点
{
if(p->pLeftChild==NULL&&p->pRightChild==NULL)//删除叶节点
{
Node *pParent=p->pParent;
if (pParent)
{
if (pParent->data<p->data)
{
pParent->pRightChild=NULL;
}
else
pParent->pLeftChild=NULL;
free(p);
}
else
{
tree=NULL;
free(p);
}
}
else if (p->pLeftChild&&p->pRightChild==NULL||p->pRightChild&&p->pLeftChild==NULL)//只有一个子树
{
Node *pParent=p->pParent;
if (pParent)
{
if (pParent->data<p->data)//子树连到父节点的右孩子
{
if(p->pLeftChild)//哪个子树不为空连哪个
{
pParent->pRightChild=p->pLeftChild;
p->pLeftChild->pParent=pParent;
}
else
{
pParent->pRightChild=p->pRightChild;
p->pRightChild->pParent=pParent;
}
free(p);
}
else
{
if(p->pLeftChild)//哪个子树不为空连哪个
{
pParent->pLeftChild=p->pLeftChild;
p->pLeftChild->pParent=pParent;
}
else
{
pParent->pLeftChild=p->pRightChild;
p->pRightChild->pParent=pParent;
}
free(p);
}
}
else
{
if(p->pLeftChild)
{
p->pLeftChild->pParent=NULL;
tree=p->pLeftChild;
}
else
{
p->pRightChild->pParent=NULL;
tree=p->pRightChild;
}
free(p);
}
}else//有两个子树
{
Node *temp=Tree_Maximum(p->pLeftChild);
int x=p->data;p->data=temp->data;temp->data=x;
Tree_Delete(tree,temp);
}
}
void main()
{
int array[]={15,6,3,2,0,0,4,0,0,7,0,13,9,0,0,0,18,17,0,0,20,0,0};
Node *tree=Create_BTree(array);
Tree_Insert(tree,21);
Mid_Order(tree);
cout<<endl;
Node *temp=Tree_Search(tree,7);//查找
Tree_Delete(tree,temp);
Mid_Order(tree);
cout<<endl;
Destroy_BTree(tree);
}