But since the childhood input within four normal
More than four collapses
program source code# include
using namespace std;
The class TreeNode {
Public:
The int value.
TreeNode * Ltree;
TreeNode * Rtree;
TreeNode (int the info) {value=https://bbs.csdn.net/topics/info; Ltree=NULL; Rtree=NULL; }
};
The class BFtree {
Private:
Root TreeNode *;
Int getHeight (TreeNode * p);
Void insertNode (TreeNode * p, int the info).
Void Lrotata TreeNode * (p);
Void Rrotata TreeNode * (p);
Void BFtreeclear TreeNode * (p);
Int getBF TreeNode * (p);
Void Firstprintf TreeNode * (p);
Void middleprintf TreeNode * (p);
TreeNode * findparent TreeNode * (p);
Public:
BFtree ();
~ BFtree ();
Void insertNode (int the info);
Void firstprintf ();
Void middleprintf ();
};
BFtree: : BFtree ()//constructor
{
The root=NULL;
}
BFtree: : ~ BFtree ()//destructors
{
If (root==NULL);
return;
BFtreeclear (root);
}
Void BFtree: : BFtreeclear (TreeNode * p)//clear the tree node is called the destructor
{
If (p!=NULL)
{
BFtreeclear (p - & gt; Ltree);
BFtreeclear (p - & gt; Rtree);
The delete p;
}
}
Int BFtree: : getHeight (TreeNode * p)//access nodes highly
{
Int the HL, HR, MAXH;
If (p!=NULL)
{
HL=getHeight (p - & gt; Ltree);
HR=getHeight (p - & gt; Rtree);
MAXH=HL> HR? HL: HR;
Return MAXH + 1;
}
The else
return 0;
}
Void BFtree: : insertNode (int the info)//insert node
{
If (root==NULL)
The root=new TreeNode (info);
The else
InsertNode (root, info);
}
TreeNode * BFtree: : findparent (TreeNode * p)//find the parent node
{the parent=root TreeNode *;
If (p==root)
return NULL;
While (the parent - & gt; Ltree!=p& & The parent - & gt; Rtree!=NULL)
{
If (the parent - & gt; Value> P - & gt; Value)
The parent=parent - & gt; Ltree;
The else
The parent=parent - & gt; Rtree;
}
Return the parent;
}
Int BFtree: : getBF (TreeNode * p)//returns the node around children height difference namely balance factor
{
Return getHeight (p - & gt; Ltree) - getHeight (p - & gt; Rtree);
}
Void BFtree: : Lrotata (TreeNode * p)//left rotating
{
TreeNode * temp=p - & gt; Rtree;
P - & gt; Rtree=temp - & gt; Ltree;
Temp - & gt; Ltree=p;
If (p==root)
The root=temp;
Else//the subtree of the parent node point to the right of his children
{
TreeNode * parent=findparent (p);
If (the parent - & gt; Ltree==p)
The parent - & gt; Ltree=temp;
The else
The parent - & gt; Rtree=temp;
}
}
Void BFtree: : Rrotata (TreeNode * p)//right
{
TreeNode * temp=p - & gt; Ltree;
P - & gt; Ltree=temp - & gt; Rtree;
Temp - & gt; Rtree=p;
If (p==root)
The root=temp;
Else//the subtree of the parent node, pointing to the left child
{
TreeNode * parent=findparent (p);
If (the parent - & gt; Ltree==p)
The parent - & gt; Ltree=temp;
The else
The parent - & gt; Rtree=temp;
}
}
Void BFtree: : insertNode (TreeNode * p, int the info)
{
If (p - & gt; Value> Info)
{
If (p - & gt; Ltree==NULL)
P - & gt; Ltree=new TreeNode (info);
The else
{
InsertNode (p - & gt; Ltree, info);
If (getBF (p)==2)//if the balance factor is equal to 2 to do rotation adjustment
{
If (getBF (p - & gt; Ltree)==1)//if left subtree balance factor is equal to 1 is right
Rrotata (p);
Else if (getBF (p - & gt; Ltree)==1)//is equal to 1 is around rotating
{
Lrotata (p - & gt; Ltree);
Rrotata (p);
}
}
}
}
The else
{
If (p - & gt; Rtree==NULL)
P - & gt; Rtree=new TreeNode (info);
The else
{
InsertNode (p - & gt; Rtree, info);
If (getBF (p)==2)//same as above to do adjustment
{
If (getBF (p - & gt; Rtree)==1)//left rotating determine
Lrotata (p);
Else if (getBF (p - & gt; Rtree)==1) turn to determine/right/left
{
Rrotata (p - & gt; Rtree);
Lrotata (p);
}
}
}
}
}
Void BFtree: : firstprintf ()//first sequence traversal
{
If (root==NULL)
{
Cout<& lt;" The tree is empty "& lt;
}
Firstprintf (root);
Cout
Void BFtree: : Firstprintf (TreeNode * p)//call the first sequence traversal
{
If (p!=NULL)
{
CoutFirstprintf (p - & gt; Ltree);
Firstprintf (p - & gt; Rtree);
}
}
Void BFtree: : middleprintf ()//in the call sequence traversal
{
If (root==NULL)
{
Cout<& lt;" The tree is empty "& lt;
}
Middleprintf (root);
Cout
Void BFtree: : middleprintf (TreeNode * p)//in sequence traversal
{
If (p!=NULL)
{
Middleprintf (p - & gt; Ltree);
CoutMiddleprintf (p - & gt; Rtree);
}
}
Int main () {
BFtree a;
Anderson nsertNode (1);
Anderson nsertNode (2);
Anderson nsertNode (3);
Anderson nsertNode (4);
Anderson nsertNode (5);
//Anderson nsertNode (6);
Cout<& lt;" The tree of the first sequence traversal sequence is: ";
A.f irstprintf ();
Cout<& lt;" The tree in sequence traversal sequence is: ";
Arjun iddleprintf ();
return 0;
}