Various algorithms achieve a balanced binary sort tree (including recursive traversal of a binary tree, non-recursive traversal)

Original link: http://www.cnblogs.com/arcfat/archive/2012/11/20/2779028.html
#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;
// structure balanced binary tree sort 
typedef struct Tnode
{
ElemType data;
struct Tnode *lchild,*rchild;
int height; // node in the subtree rooted at a height 
}BBTnode,*BBTree;
 
typedef BBTree Position;
// define the stack 
typedef struct
{
BBTree * base, * top; // stack pointer member 
int initsize; // stack initial length 
}Stack;
 
 
// queue definition 
typedef struct
{
BBTree * front, * rear; // queue pointer member 
int InitSize; // stack initial length 
}Queue;
 
// function to use the following 
status InsertBBT (BBTree & T, ElemType e); // insert a new node 
status CreatStack (Stack & S); // build stack 
status CreatQueue (Queue & Q); // build queue   
 
status FirstViewRoot (BBTree T); // recursive before preorder 
status MiddleViewRoot (BBTree T); // recursive preorder  
status LastViewRoot (BBTree T); // recursive after preorder 
status ViewAll (BBTree T); // when traversing often be used to reduce the code amount 
 
status NonFirstView (BBTree T, Stack S); // before nonrecursive preorder 
status NonMiddleView (BBTree T, Stack S); // nonrecursive preorder  
status NonLastView (BBTree T, Stack S); // after the non-recursive preorder 
status NonViewAll (BBTree T, Stack S); // when traversing often be used to reduce the code amount 
status LevelView (BBTree T, Queue Q); // traverse the level  
 
status FindKeyword (BBTree T, ElemType e); // Find the binary tree for a given keyword (a function return value success, failure 0) 
status SwapSubtree (BBTree T); // left and right subtrees of each switching node 
int DeepOfTree (BBTree T); // binary tree of depth
int TotalNodeNumber (BBTree T); // total number of nodes  
int LeafNodeNumber (BBTree T); // leaf node points 
status DeleteBBT (BBTree & T, ElemType e); // delete a node 
 
// The following function is to achieve a balanced tree algorithm I designed 
Position FindMin (BBTree T); // find the smallest Element 
int Height (BBTree T); Number of levels / * find tree height (in an empty tree is -1, and the height of the tree is defined as 1 minus the tree, as only a
Height of the tree node is 0, two layers of tree height 1) * /
int Max (int l, int r); // find a large number of  
BBTree LL_SingleRotate (BBTree k2); // rotate left once 
BBTree RR_SingleRotate (BBTree k1); // Rotate right time 
BBTree LR_DoubleRotate (BBTree k3); // turn left again, turn right again 
BBTree RL_DoubleRotate (BBTree k1); // Turn right again, turn left again 
 
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 >> and;
InsertBBT(T,e);
}
 
do
{
cout << "\nPlease chose one from these:\n";
cout << "1. Insert a new node \ n" 
 << "2. preamble, in sequence, after the binary tree traversal (recursive) \ n"
 << "3. preamble, in order, a non-recursive algorithm traversing \ n"
 << "4. level binary tree traversal \ n"
 << "5. Look for a given keyword in a binary tree \ n"
 << "6. The switching nodes each left and right subtrees \ n"
 << "7 binary tree of depth \ n"
 << "leaf node number 8. \ n"
 << "9. To delete a node \ n"
 << "0. exit \ n";
 
cin >> chose;
switch(chose)
{
case 1:
cout << "Please input one Element you want to insert: ";
cin >> and;
if(InsertBBT(T,e) == OK) 
cout << "*** The Element " << e << " inserted successfully! ***\n";
else
cout << "*** The Element " << e << " failed to insert! ***\n";
break;
 
case 2:
// The following is a recursive traversal  
cout << "\nThe recursive traversal are follow:\n";
ViewAll(T);
break;
 
case 3:
// The following non-recursive traversal 
cout << "\nThe non-recursive traversal are follow:\n";
NonViewAll(T,S);
break;
 
case 4:
// The following is the hierarchy traversal 
cout << "the LevelView is:\n";
CreatQueue(Q);
LevelView(T,Q);
cout << "\n";
break;
 
case 5:
// Find the keywords in a given binary tree 
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:
// left and right subtrees of each switching node 
SwapSubtree(T);
break;
 
case 7:
// find the depth of a binary tree 
cout << "\nThe deep of the tree is: ";
cout << DeepOfTree(T) <<endl;
break;
 
case 8:
// leaf node points 
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:
// delete a node 
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;
}
 
/ * --------------------- The following are InsertBBT and DeleteBBT required function ------------------- ------ * / 
 
status InsertBBT (BBTree & T, ElemType e) // inserting new node 
{
if (T == NULL) // empty tree, to build a node tree 
{
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) // left inserted  
{
InsertBBT(T->lchild,e);
if (Height (T-> lchild) - Height (T-> rchild) == 2) // imbalance of 
{
// if the sequence (5,2,1 ...) can occur LL type, the sequence (5,2,3 ...) can occur LR type   
if (e <T-> lchild-> data) // this type corresponds to the LL 
T = LL_SingleRotate(T);
else // this corresponds to the type LR  
T = LR_DoubleRotate(T); 
}
}
else if (e> T-> data) // inserted rightward 
{
InsertBBT(T->rchild,e);
if (Height (T-> rchild) - Height (T-> lchild) == 2) // imbalance of 
{
// if the sequence (5,6,7 ...) can occur RR type, the sequence (5,7,6 ...) can occur RL type 
if (e> T-> rchild-> data) // this type corresponds to RR 
T = RR_SingleRotate(T);
else // RL corresponding to this type 
T = RL_DoubleRotate(T); 
}
// If e == T-> data, then do nothing, the last to be recorded T-> height  
T->height = Max(Height(T->lchild),Height(T->rchild)) + 1;
return OK;
}
 
status DeleteBBT (BBTree & T, ElemType e) // To delete a node 
{
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 // i.e. e == T-> data case  
{
if (T-> lchild! = NULL && T-> rchild! = NULL) // with two children 
{
temp = FindMin(T->rchild);
T->data = temp->data;
DeleteBBT (T-> rchild, T-> data); // Remove the right just to find the smallest node  
}
else // have one or no children
{
temp = T;
if (T-> lchild == NULL) // also addressed the situation of children 0  
T = T->rchild;
else if(T->rchild == NULL)
T = T->lchild;
free(temp);
return OK;
}
}
 
Position FindMin (BBTree T) // find the smallest Element 
{
if(T == NULL) return NULL;
else if(T->lchild == NULL) return T;
else return FindMin(T->lchild);
}
 
int Height (BBTree T) // required height of the tree 
{
if(T == NULL) return -1;
else return T->height;
}
 
int Max (int l, int r) // find a large number of 
{
return l>r?l:r;
}
 
BBTree LL_SingleRotate (BBTree k2) // Rotate Left once, grab k1 (small), then let gravity do 
{
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; // new root 
}
 
BBTree RR_SingleRotate (BBTree k1) // Rotate right time to seize k2 (large), then let gravity do 
{
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; // new root 
}
 
BBTree LR_DoubleRotate (BBTree k3) // a left turn, turn right again 
{
k3-> lchild = RR_SingleRotate (k3-> lchild); // first turn counterclockwise 
 
return LL_SingleRotate (k3); // re-clockwise  
}
 
BBTree RL_DoubleRotate (BBTree k1) // Turn right again, turn left again 
{
k1-> rchild = LL_SingleRotate (k1-> rchild); // first clockwise 
 
return RR_SingleRotate (k1); // then counter-clockwise rotation 
}
/ * -------------------- upper InsertBBT desired function and DeleteBBT -------------------- ----- * / 
 
status CreatStack (Stack & S) // build stacks 
{
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) // build queue  
{
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) // recursive front preorder 
{
if(T != NULL)
{
cout << T->data << " ";
FirstViewRoot(T->lchild);
FirstViewRoot(T->rchild);
}
return OK;
}
status MiddleViewRoot (BBTree T) // recursive preorder  
{
if(T != NULL)
{
MiddleViewRoot(T->lchild);
cout << T->data << " ";
MiddleViewRoot(T->rchild);
}
return OK;
}
status LastViewRoot (BBTree T) // recursive after preorder 
{
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) // nonrecursive preorder traversal 
{
while(S.base != S.top || T != NULL)
{
while (T! = NULL) // left went leftmost  
{
cout << T-> data << ""; // output element   
*S.top++ = T;
T = T->lchild;
}
T = * - S.top; // Stack 
T = T-> rchild; // a right turn  
}
return OK;
}
 
status NonMiddleView (BBTree T, Stack S) // nonrecursive preorder  
{
while(S.base != S.top || T != NULL)
{
while (T! = NULL) // left went leftmost  
{
*S.top++ = T;
T = T->lchild;
}
T = * - S.top; // Stack 
cout << T-> data << ""; // output element  
T = T-> rchild; // a right turn  
}
return OK;
}
 
status NonLastView(BBTree T,Stack S)         
{
BBTree pre = NULL;
while(S.base != S.top || T != NULL)
{
while (T! = NULL) // left went leftmost  
{
*S.top++ = T;
T = T->lchild;
}
T = * (S.top - 1); // get node stack  
if (T-> rchild == NULL || T-> rchild == pre) // if T has no right child or a right child has just been visited 
{
cout << T-> data << ""; // output element 
S.top--;
pre = T;
T = NULL;
}
else  
T = T-> rchild; // a right turn  
}
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) // traverse the level 
{
if(T != NULL)
{
*Q.rear++ = T;
while (Q.front! = Q.rear) // queue is not empty  
{
if (! T-> lchild = NULL) * Q.rear ++ = T-> lchild; // left subtree into the team 
if (! T-> rchild = NULL) * Q.rear ++ = T-> rchild; // right subtree into the team  
T = * Q.front ++; // a team  
cout << T->data << " ";
T = * Q.front; // latest team head 
}
}
return OK;
}
 
status FindKeyword (BBTree T, ElemType e) // find keywords given in the binary tree (a function return value success, failure 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) // exchange about each node subtrees 
{
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) // find the depth of a binary tree 
{
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) // total number of nodes 
{
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) // leaf node points 
{
int cnt = 0 = 0 lcnt, 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;
}

Reproduced in: https: //www.cnblogs.com/arcfat/archive/2012/11/20/2779028.html

Guess you like

Origin blog.csdn.net/weixin_30291791/article/details/94789595