二叉查找树的平均深度是O(㏒ N),所以不用担心栈空间被用尽。
注意事项:
如果插入重复的元素,可以不做处理或者根据自身需求对某些数据进行更新,这里采用前者;
诸多操作中,最难操作的是删除操作,因为要分三种情况
①需要删除的结点是树叶,那么可以立即删除;
②如果删除的结点具有一个孩子,那么可以对该节点的父节点进行操作,改变其父节点的指针;
③如果需要删除的结点有两个孩子,则默认将其右子树的最小值替换到该结点位置,然后删除这个右子树最小值(这个是进行了两次Delete操作,但是右子树的最小值是不具备左子树的,所以第二次删除相对方便,这里也可以默认删除左子树的最大值,原因同上)
但是这种删除操作是有一定的缺陷的,例如,默认找右子树的最小值,将会导致左子树相对于右子树越来越深,这种不平衡现象将导致查找效率逐渐降低,所以就有了另一种树:平衡二叉树(详情参见下节,这里不做描述)
本来想再添加个层序遍历,但是要用到队列有点麻烦饿了,想去吃饭 ,有时间再补上
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int ElemType;
typedef struct TreeNode
{
ElemType Element;
TreeNode *Left;
TreeNode *Right;
} TreeNode,*Position,*SearchTree;
SearchTree MakeEmpty(SearchTree T);
Position Find(ElemType X,SearchTree T);
Position FindMin(SearchTree T);
Position FindMax(SearchTree T);
SearchTree Insert(ElemType X,SearchTree T);
SearchTree Delete(ElemType X,SearchTree T);
ElemType Retrieve(Position P);
int Depth(SearchTree T);
void PreOrderTraversal(SearchTree T);
void InOrderTraversal(SearchTree T);
void PostOrderTraversal(SearchTree T);
SearchTree MakeEmpty(SearchTree T)
{
if(T!=NULL)
{
MakeEmpty(T->Left);
MakeEmpty(T->Right);
free(T);
}
return NULL;
}
Position Find(ElemType X,SearchTree T)
{
if(T==NULL)
return NULL;
if(X<T->Element)
return Find(X,T->Left);
else if(X>T->Element)
return Find(X,T->Right);
else
return T;
}
Position FindMin(SearchTree T)
{
if(T==NULL)
return NULL;
if(T->Left)
return FindMin(T->Left);
else
return T;
}
Position FindMax(SearchTree T)
{
if(T==NULL)
return NULL;
if(T->Right)
return FindMax(T->Right);
else
return T;
}
SearchTree Insert(ElemType X,SearchTree T)
{
if(T==NULL)
{
T = (SearchTree)malloc(sizeof(TreeNode));
if(T==NULL)
{
printf("MALLOC ERROR\n");
exit(1);
}
else
{
T->Element = X;
T->Left = T->Right = NULL;
}
}
else
{
if(X<T->Element)
T->Left = Insert(X,T->Left);
else if(X>T->Element)
T->Right = Insert(X,T->Right);
}
return T;
}
SearchTree Delete(ElemType X,SearchTree T)
{
Position temp;
if(T==NULL)
{
printf("Element not found\n");
return NULL;
}
else if(X<T->Element)
{
T->Left = Delete(X,T->Left);
}
else if(X>T->Element)
{
T->Right = Delete(X,T->Right);
}
//此时已经找到需要删除的结点
else if(T->Left && T->Right) //该结点具有两个孩子
{
temp = FindMin(T->Right);
T->Element = temp->Element;
T->Right = Delete(T->Element,T->Right);
}
else //该结点具有0个或1个孩子
{
temp = T;
if(T->Left==NULL)
T=T->Right;
else if(T->Right==NULL)
T->Left;
free(temp);
}
return T;
}
int Depth(SearchTree T)
{
int Ldepth,Rdepth;
if(T==NULL)
return 0;
if(T->Left)
Ldepth = Depth(T->Left);
else
Ldepth = 0;
if(T->Right)
Rdepth = Depth(T->Right);
else
Rdepth = 0;
return Ldepth>Rdepth ? Ldepth+1:Rdepth+1;
}
void PreOrderTraversal(SearchTree T)
{
if(T==NULL)
return ;
printf("%d ",T->Element);
PreOrderTraversal(T->Left);
PreOrderTraversal(T->Right);
}
void InOrderTraversal(SearchTree T)
{
if(T==NULL)
return ;
InOrderTraversal(T->Left);
printf("%d ",T->Element);
InOrderTraversal(T->Right);
}
void PostOrderTraversal(SearchTree T)
{
if(T==NULL)
return ;
PostOrderTraversal(T->Left);
PostOrderTraversal(T->Right);
printf("%d ",T->Element);
}
int main()
{
SearchTree T=NULL;
T = MakeEmpty(T);
int depth = Depth(T);
printf("The depth of this tree is :%d\n",depth);
T = Insert(5,T);
T = Insert(3,T);
T = Insert(2,T);
T = Insert(1,T);
T = Insert(4,T);
T = Insert(8,T);
T = Insert(7,T);
T = Insert(9,T);
depth = Depth(T);
printf("The depth of this tree is :%d\n",depth);
printf("PreOrderTraversal: ");
PreOrderTraversal(T);
printf("\n");
printf("InOrderTraversal: ");
InOrderTraversal(T);
printf("\n");
printf("PostOrderTraversal: ");
PostOrderTraversal(T);
printf("\n");
T = Delete(3,T);
printf("The depth of this tree is :%d\n",depth);
printf("---After Delete---\n");
printf("PreOrderTraversal: ");
PreOrderTraversal(T);
printf("\n");
printf("InOrderTraversal: ");
InOrderTraversal(T);
printf("\n");
printf("PostOrderTraversal: ");
PostOrderTraversal(T);
printf("\n");
return 0;
}
执行结果:
The depth of this tree is :0
The depth of this tree is :4
PreOrderTraversal: 5 3 2 1 4 8 7 9
InOrderTraversal: 1 2 3 4 5 7 8 9
PostOrderTraversal: 1 2 4 3 7 9 8 5
The depth of this tree is :4
---After Delete---
PreOrderTraversal: 5 4 2 1 8 7 9
InOrderTraversal: 1 2 4 5 7 8 9
PostOrderTraversal: 1 2 4 7 9 8 5
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.