#include<iostream>
#include<cstdlib>
using namespace std;
const int MAXSIZE = 100;
const int OK = 1;
const int ERROR = 0;
typedef int status;
typedef int ElemType;
//平衡二叉排序树的结构
typedef struct Tnode
{
ElemType data;
struct Tnode *lchild,*rchild;
int height; //以该节点为根的子树的高度
}BBTnode,*BBTree;
typedef BBTree Position;
//栈定义
typedef struct
{
BBTree *base,*top; //栈指针成员
int initsize; //栈初始长度
}Stack;
//队列定义
typedef struct
{
BBTree *front,*rear; //队列指针成员
int InitSize; //栈初始长度
}Queue;
//要用到的函数如下
status InsertBBT(BBTree &T,ElemType e); //插入新结点
status CreatStack(Stack &S); //建立栈
status CreatQueue(Queue &Q); //建立队列
status FirstViewRoot(BBTree T); //递归前序遍历
status MiddleViewRoot(BBTree T); //递归中序遍历
status LastViewRoot(BBTree T); //递归后序遍历
status ViewAll(BBTree T); //当经常要遍历时可以用来减少代码量
status NonFirstView(BBTree T,Stack S); //非递归前序遍历
status NonMiddleView(BBTree T,Stack S); //非递归中序遍历
status NonLastView(BBTree T,Stack S); //非递归后序遍历
status NonViewAll(BBTree T,Stack S); //当经常要遍历时可以用来减少代码量
status LevelView(BBTree T,Queue Q); //层次遍历
status FindKeyword(BBTree T,ElemType e);//在二叉树中查找给定关键字(函数返回值为成功1,失败0)
status SwapSubtree(BBTree T); //交换各结点的左右子树
int DeepOfTree(BBTree T); //求二叉树的深度
int TotalNodeNumber(BBTree T); //总的结点数
int LeafNodeNumber(BBTree T); //叶子结点数
status DeleteBBT(BBTree &T,ElemType e); //删除某结点
//以下函数是为了实现我的平衡树算法而设计
Position FindMin(BBTree T); //找最小的Element
int Height(BBTree T); /*求树的高度(以空树为-1,并定义树的高度为树的层次数减1,如只有一个
节点的树的高度为0,两层的树高度为1)*/
int Max(int l,int r); //求较大的数
BBTree LL_SingleRotate(BBTree k2); //向左旋转一次
BBTree RR_SingleRotate(BBTree k1); //向右旋转一次
BBTree LR_DoubleRotate(BBTree k3); //向左转一次,向右转一次
BBTree RL_DoubleRotate(BBTree k1); //向右转一次,向左转一次
int main()
{
int i,n,chose,cnt=0;
char c;
ElemType keyword,e,delKey;
Stack S;
Queue Q;
BBTree T = NULL;
cout << "Please input the number of Elements: ";
cin >> n;
cout << "Now,please input " << n << " Elements:\n";
for(i=0;i<n;i++)
{
cin >> e;
InsertBBT(T,e);
}
do
{
cout << "\nPlease chose one from these:\n";
cout << " 1.插入新结点\n"
<< " 2.前序、中序、后序遍历二叉树(递归)\n"
<< " 3.前序、中序、后序遍历的非递归算法\n"
<< " 4.层次遍历二叉树\n"
<< " 5.在二叉树中查找给定关键字\n"
<< " 6.交换各结点的左右子树\n"
<< " 7.求二叉树的深度\n"
<< " 8.叶子结点数\n"
<< " 9.删除某结点\n"
<< " 0.退出\n";
cin >> chose;
switch(chose)
{
case 1:
cout << "Please input one Element you want to insert: ";
cin >> e;
if(InsertBBT(T,e) == OK)
cout << "*** The Element " << e << " inserted successfully! ***\n";
else
cout << "*** The Element " << e << " failed to insert! ***\n";
break;
case 2:
//以下是递归遍历
cout << "\nThe recursive traversal are follow:\n";
ViewAll(T);
break;
case 3:
//以下是非递归遍历
cout << "\nThe non-recursive traversal are follow:\n";
NonViewAll(T,S);
break;
case 4:
//以下是层次遍历
cout << "the LevelView is:\n";
CreatQueue(Q);
LevelView(T,Q);
cout << "\n";
break;
case 5:
//在二叉树中查找给定关键字
cout << "Please input the keyword you want to find in the tree: ";
cin >> keyword;
if(FindKeyword(T,keyword) == OK)
cout << "*** The keyword " << keyword << " found successfully! ***\n";
else
cout << "*** The keyword " << keyword << " not found! ***\n";
break;
case 6:
//交换各结点的左右子树
SwapSubtree(T);
break;
case 7:
//求二叉树的深度
cout << "\nThe deep of the tree is: ";
cout << DeepOfTree(T) <<endl;
break;
case 8:
//叶子结点数
cout << "\nThe number of leaves is: ";
cout << LeafNodeNumber(T) <<endl;
cout << "And the total number of nodes is: ";
cout << TotalNodeNumber(T) <<endl;
break;
case 9:
//删除某结点
cout << "Please input the Element you want to Delete: ";
cin >> delKey;
if(DeleteBBT(T,delKey) == OK)
{
cout << "*** The Element " << delKey << " Deleted successfully! ***\n";
}
else
{
cout << "*** Failed to Delete!Because the element not found! ***\n";
}
break;
case 0:return 0;break;
default:
cout << "\a***Command Not Vilid!!!***\n";
break;
}
cout << "\nContinue...?(please answer y or n). "
<< "if you answer y,\nthen will continue,others will exit!\n"
<< "Your answer is: ";
cin >> c;
}while(c == 'y');
return 0;
}
/*---------------------以下是InsertBBT和DeleteBBT所需函数-------------------------*/
status InsertBBT(BBTree &T,ElemType e) //插入新结点
{
if(T == NULL) //空树,建一个节点给树
{
T = (BBTree)malloc(sizeof(BBTnode));
if(!T) return ERROR;
T->data = e;
T->lchild = T->rchild = NULL;
T->height = 0;
}
else if(e < T->data) //向左插入
{
InsertBBT(T->lchild,e);
if(Height(T->lchild) - Height(T->rchild) == 2) //出现不平衡了
{
//如果用序列(5,2,1...)就可会出现LL型,用序列(5,2,3...)就可会出现LR型
if(e < T->lchild->data) //这样对应于 LL 型
T = LL_SingleRotate(T);
else //这个对应于 LR 型
T = LR_DoubleRotate(T);
}
}
else if(e > T->data) //向右插入
{
InsertBBT(T->rchild,e);
if(Height(T->rchild) - Height(T->lchild) == 2) //出现不平衡了
{
//如果用序列(5,6,7...)就可会出现RR型,用序列(5,7,6...)就可会出现RL型
if(e > T->rchild->data) //这样对应于 RR 型
T = RR_SingleRotate(T);
else //这样对应于 RL 型
T = RL_DoubleRotate(T);
}
}
//如果 e == T->data 的话什么也不干,最后要记录T->height
T->height = Max(Height(T->lchild),Height(T->rchild)) + 1;
return OK;
}
status DeleteBBT(BBTree &T,ElemType e) //删除某结点
{
Position temp;
if(T == NULL) return ERROR;
else if(e < T->data)
return DeleteBBT(T->lchild,e);
else if(e > T->data)
return DeleteBBT(T->rchild,e);
else //即 e == T->data的情况
{
if(T->lchild != NULL && T->rchild !=NULL) //有两个孩子
{
temp = FindMin(T->rchild);
T->data = temp->data;
DeleteBBT(T->rchild,T->data); //删除右边刚刚找出的最小的节点
}
else //有一个或者没有孩子
{
temp = T;
if(T->lchild == NULL) //也处理了0个孩子的情况
T = T->rchild;
else if(T->rchild == NULL)
T = T->lchild;
free(temp);
}
return OK;
}
}
Position FindMin(BBTree T) //找最小的Element
{
if(T == NULL) return NULL;
else if(T->lchild == NULL) return T;
else return FindMin(T->lchild);
}
int Height(BBTree T) //求树的高度
{
if(T == NULL) return -1;
else return T->height;
}
int Max(int l,int r) //求较大的数
{
return l>r?l:r;
}
BBTree LL_SingleRotate(BBTree k2) //向左旋转一次,抓住k1(小的),让重力话事
{
BBTree k1;
k1 = k2->lchild;
k2->lchild = k1->rchild;
k1->rchild = k2;
k1->height = Max(Height(k1->lchild),k2->height) + 1;
k2->height = Max(Height(k2->lchild),Height(k2->rchild)) +1;
return k1; //新的root
}
BBTree RR_SingleRotate(BBTree k1) //向右旋转一次,抓住k2(大的),让重力话事
{
BBTree k2;
k2 = k1->rchild;
k1->rchild = k2->lchild;
k2->lchild = k1;
k1->height = Max(Height(k1->lchild),Height(k1->rchild)) +1;
k2->height = Max(Height(k1->rchild),k1->height) + 1;
return k2; //新的root
}
BBTree LR_DoubleRotate(BBTree k3) //向左转一次,向右转一次
{
k3->lchild = RR_SingleRotate(k3->lchild); //先逆时针转
return LL_SingleRotate(k3); //再顺时针转
}
BBTree RL_DoubleRotate(BBTree k1) //向右转一次,向左转一次
{
k1->rchild = LL_SingleRotate(k1->rchild); //先顺时针转
return RR_SingleRotate(k1); //再逆时针转
}
/*--------------------以上是InsertBBT和DeleteBBT所需函数-------------------------*/
status CreatStack(Stack &S) //建立栈
{
S.base = (BBTree *)malloc(MAXSIZE * sizeof(BBTree));
if(!S.base) return ERROR;
S.top = S.base;
S.initsize = MAXSIZE;
return OK;
}
status CreatQueue(Queue &Q) //建立队列
{
Q.front = (BBTree *)malloc(MAXSIZE * sizeof(BBTree));
if(!Q.front) return ERROR;
Q.rear = Q.front;
Q.InitSize = MAXSIZE;
return OK;
}
status FirstViewRoot(BBTree T) //递归前序遍历
{
if(T != NULL)
{
cout << T->data << " ";
FirstViewRoot(T->lchild);
FirstViewRoot(T->rchild);
}
return OK;
}
status MiddleViewRoot(BBTree T) //递归中序遍历
{
if(T != NULL)
{
MiddleViewRoot(T->lchild);
cout << T->data << " ";
MiddleViewRoot(T->rchild);
}
return OK;
}
status LastViewRoot(BBTree T) //递归后序遍历
{
if(T != NULL)
{
LastViewRoot(T->lchild);
LastViewRoot(T->rchild);
cout << T->data << " ";
}
return OK;
}
status ViewAll(BBTree T)
{
cout << "the FirstViewRoot is:\n";
FirstViewRoot(T);
cout << "\n";
cout << "the MiddleViewRoot is:\n";
MiddleViewRoot(T);
cout << "\n";
cout << "the LastViewRoot is:\n";
LastViewRoot(T);
cout << "\n";
return OK;
}
status NonFirstView(BBTree T,Stack S) //非递归前序遍历
{
while(S.base != S.top || T != NULL)
{
while(T != NULL) //向左走到最左
{
cout << T->data << " "; //输出元素
*S.top++ = T;
T = T->lchild;
}
T=*--S.top; //出栈
T = T->rchild; //转向右
}
return OK;
}
status NonMiddleView(BBTree T,Stack S) //非递归中序遍历
{
while(S.base != S.top || T != NULL)
{
while(T != NULL) //向左走到最左
{
*S.top++ = T;
T = T->lchild;
}
T=*--S.top; //出栈
cout << T->data << " "; //输出元素
T = T->rchild; //转向右
}
return OK;
}
status NonLastView(BBTree T,Stack S)
{
BBTree pre = NULL;
while(S.base != S.top || T != NULL)
{
while(T != NULL) //向左走到最左
{
*S.top++ = T;
T = T->lchild;
}
T=*(S.top - 1); //取栈顶节点
if(T->rchild == NULL || T->rchild == pre) //如果T没有右孩子或者其右孩子刚刚被访问过
{
cout << T->data << " "; //输出元素
S.top--;
pre = T;
T = NULL;
}
else
T = T->rchild; //转向右
}
return OK;
}
status NonViewAll(BBTree T,Stack S)
{
cout << "the FirstViewRoot is:\n";
CreatStack(S);
NonFirstView(T,S);
cout << "\n";
cout << "the MiddleViewRoot is:\n";
CreatStack(S);
NonMiddleView(T,S);
cout << "\n";
cout << "the LastViewRoot is:\n";
CreatStack(S);
NonLastView(T,S);
cout << "\n";
return OK;
}
status LevelView(BBTree T,Queue Q) //层次遍历
{
if(T != NULL)
{
*Q.rear++ = T;
while(Q.front != Q.rear) //队列不空时
{
if(T->lchild != NULL) *Q.rear++ = T->lchild; //左子树进队
if(T->rchild != NULL) *Q.rear++ = T->rchild; //右子树进队
T = *Q.front++; //出队
cout << T->data << " ";
T = *Q.front; //最新的队头
}
}
return OK;
}
status FindKeyword(BBTree T,ElemType e) //在二叉树中查找给定关键字(函数返回值为成功1,失败0)
{
if(T!=NULL)
{
if(e == T->data) return OK;
else if(e < T->data) return FindKeyword(T->lchild,e);
else return FindKeyword(T->rchild,e);
}
else return ERROR;
}
status SwapSubtree(BBTree T) //交换各结点的左右子树
{
BBTree tmpNode;
if(T != NULL)
{
tmpNode = T->lchild;
T->lchild = T->rchild;
T->rchild = tmpNode;
SwapSubtree(T->lchild);
SwapSubtree(T->rchild);
}
return OK;
}
int DeepOfTree(BBTree T) //求二叉树的深度
{
int deep,ldeep = 0,rdeep = 0;
if(T != NULL)
{
ldeep = DeepOfTree(T->lchild);
rdeep = DeepOfTree(T->rchild);
deep = Max(ldeep,rdeep) + 1;
}
else return 0;
return deep;
}
int TotalNodeNumber(BBTree T) //总的结点数
{
int sum = 0,lsum = 0,rsum = 0;
if(T != NULL)
{
lsum = TotalNodeNumber(T->lchild);
rsum = TotalNodeNumber(T->rchild);
sum = lsum + rsum + 1;
return sum;
}
else return 0;
}
int LeafNodeNumber(BBTree T) //叶子结点数
{
int cnt = 0,lcnt = 0,rcnt = 0;
if(T != NULL)
{
if(T->lchild == NULL && T->rchild == NULL) cnt=1;
else
{
lcnt = LeafNodeNumber(T->lchild);
rcnt = LeafNodeNumber(T->rchild);
cnt = lcnt + rcnt;
}
}
else return 0;
return cnt;
}
转载于:https://www.cnblogs.com/arcfat/archive/2012/11/20/2779028.html