Common Binary Tree

Source: Internet
Author: User

Some recently written programs are basically related to Binary Trees. Now I am posting them! The following is a common Binary Tree, including addition, deletion, modification, pre-order, middle-order, post-order, hierarchical printing, and the height of the number of prints, there is also a tree print of the number of Integers (this takes a lot of time, mainly considering how to control the printing of spaces)

The test program is not pasted. It's messy! The result is as follows: F

*

A B

C d _ H

E f _ g _ J _

*

A B

C d _ H

E _ g _ J _

H *

A B

C d _ 0

E _ g _ J _

Press any key to continue

The main program is as follows (header file) head_bitree.h

# Include <iostream>
# Include <deque>
Using namespace STD;

Int max (INT X1, int x2) // maximum Function
{
If (x1> x2)
Return x1;
Else
Return X2;
}

Int Ming (int x, int N) // Ming Function
{
Int result = 1;
For (INT I = 0; I <n; I ++)
{
Result = Result * X;
}
Return result;
}

Void disspace (int n) // outputs n Space Functions
{
For (INT I = 0; I <n; I ++)
{
Cout <"";
}
}

Template <class T> class bitree;

Template <class T>
Class bitreenode
{
Friend class bitree <t>;
PRIVATE:
Bitreenode <t> * right;
Bitreenode <t> * left;
T item;
Public:
Bitreenode (): Right (null), left (null ){}
Bitreenode (T item, bitreenode <t> * Right = NULL, bitreenode <t> * Left = NULL)
{
This-> item = item;
This-> right = right;
This-> left = left;
}
~ Bitreenode (){}
};

Template <class T>
Class bitree
{
PRIVATE:
Bitreenode <t> * root;
Bitreenode <t> * current;
Bitreenode <t> * searchparent (bitreenode <t> * & parent, bitreenode <t> * child );//
Public:
Bitree (t x)
{
Bitreenode <t> * P = new bitreenode <t> (X );
This-> root = P;
This-> current = root;
}
~ Bitree ();//

Bitreenode <t> * root (); // return the root node of the bitree
Bitreenode <t> * parent (); // return the parent node of the current node
Bitreenode <t> * brother (); // return the brother node of the current node
Bitreenode <t> * child (); // return the child node of the current node

Void deletenode (); // Delete the current node
Void deleteroot (bitreenode <t> * cur); // Delete the root node
Void searchnode (t x, bitreenode <t> * psearch); // seach the item that values X and return the node
Void insertnode (t x, int rorl = 0); // insert a node values X to the current node /**/
Void changenode (t x); // change the current node 'value to X

Void preorder (bitreenode <t> * pnode); // preorder to visit the bitree
Void midorder (bitreenode <t> * pnode); // midorder to visit the bitree
Void lastorder (bitreenode <t> * pnode); // lastorder to visit the bitree
Void layorder (); // layorder to visit the bitree

Void display (); // display the current node's Value
Void displaytotal (); // display the bitree totally (Tree Model)
Int countlayer (bitreenode <t> * cNode); // return the total layer of the tree
};

Template <class T>
Bitree <t> ::~ Bitree ()
{
Deleteroot (Root );
}

Template <class T>
Void bitree <t >:: deleteroot (bitreenode <t> * cur)
{
If (cur! = NULL)
{
If (cur-> left = NULL) & (cur-> right = NULL ))
{
Bitreenode <t> * P;
P = cur;
Delete P;
}
Else
{
Deleteroot (cur-> left );
Deleteroot (cur-> right );
}
}
}

Template <class T>
Bitreenode <t> * bitree <t>: Root ()
{
If (root! = NULL)
{
This-> current = root;
Return this-> root;
}
Else
{
Return NULL;
}
}

Template <class T>
Bitreenode <t> * bitree <t>: searchparent (bitreenode <t> * & parent, bitreenode <t> * child)
{
If (parent = NULL)
Return NULL;
If (parent-> left = Child) | (parent-> right = Child ))
{
Current = parent;
Return parent;
}
Else
{
If (parent-> left! = Child) & (parent-> left! = NULL ))
Searchparent (parent-> left, child );
If (parent-> right! = Child) & (parent-> right! = NULL ))
Searchparent (parent-> right, child );
Return Current;
}
}

Template <class T>
Bitreenode <t> * bitree <t>: parent ()
{
If (current = NULL) | (current = root ))
Return root;
Else
{
Current = searchparent (root, current );
Return Current;
}
}

Template <class T>
Void bitree <t>: Display ()
{
Cout <Current-> item <"";
}

Template <class T>
Void bitree <t>: insertnode (t x, int rorl) // 0 is to insert a node left to right
{// 1 is to insert a node only left
If (current! = NULL) // 2 is to insert a node only right
{
If (rorl = 0) | (rorl = 1) & (current-> left = NULL ))
{
Bitreenode <t> * P = new bitreenode <t> (X );
Current-> left = P;
Current = P;
}
Else if (rorl = 0) | (rorl = 2) & (current-> right = NULL ))
{
Bitreenode <t> * P = new bitreenode <t> (X );
Current-> right = P;
Current = P;
}
Else
{
Cout <"can't insert a node .." <Endl;
}
}
Else
{
Cout <"can't insert a node .." <Endl;
}
}

Template <class T>
Bitreenode <t> * bitree <t>: Brother ()
{
If (current = NULL)
{
Return NULL;
}
Else
{
Bitreenode <t> * P;
P = current;
Current = This-> parent ();
If (current-> left = P) & (current-> right! = NULL ))
{
Current = Current-> right;
Return Current;
}
Else if (current-> right = P) & (current-> left! = NULL ))
{
Current = Current-> left;
Return Current;
}
Else
{
Return NULL;
}
}
}

Template <class T>
Bitreenode <t> * bitree <t>: Child ()
{
If (current = NULL)
{
Return NULL;
}
Else
{
If (current-> left! = NULL)
{
Current = Current-> left;
Return Current;
}
Else if (current-> right! = NULL)
{
Current = Current-> right;
Return Current;
}
}
}

Template <class T>
Void bitree <t >:: deletenode ()
{
Bitreenode <t> * P1, * P2;
If (current! = NULL)
{
If (current-> left = NULL) & (current-> right = NULL ))
{
P1 = current;
P2 = This-> parent ();
If (P2-> left = p1)
{
Delete P1;
P2-> left = NULL;
}
Else if (P2-> right = p1)
{
Delete P1;
P2-> right = NULL;
}

}
}
}

Template <class T>
Void bitree <t >:: searchnode (t x, bitreenode <t> * psearch)
{
If (psearch! = NULL)
{
If (psearch-> item = X)
{
This-> current = psearch;
}
Else
{
If (psearch-> left! = NULL)
Searchnode (x, psearch-> left );
If (psearch-> right! = NULL)
Searchnode (x, psearch-> right );
}
}
}

Template <class T>
Void bitree <t>: changenode (t x)
{
If (current! = NULL)
{
Current-> item = X;
}
}

Template <class T>
Void bitree <t >:: preorder (bitreenode <t> * pnode)
{
If (pnode! = NULL)
{
Cout <pnode-> item <"";
Preorder (pnode-> left );
Preorder (pnode-> right );
}
}

Template <class T>
Void bitree <t >:: midorder (bitreenode <t> * pnode)
{
If (pnode! = NULL)
{
Midorder (pnode-> left );
Cout <pnode-> item <"";
Midorder (pnode-> right );
}
}

Template <class T>
Void bitree <t >:: lastorder (bitreenode <t> * pnode)
{
If (pnode! = NULL)
{
Lastorder (pnode-> left );
Lastorder (pnode-> right );
Cout <pnode-> item <"";
}
}

Template <class T>
Int bitree <t >:: countlayer (bitreenode <t> * cNode)
{
If (cNode = NULL)
Return 0;
Else
{
Return 1 + max (countlayer (cNode-> left), countlayer (cNode-> right ));
}
}

Template <class T>
Void bitree <t >:: layorder ()
{
Deque <bitreenode <t> *> deque;
Bitreenode <t> * TMP;
Deque. push_back (this-> root );
While (! Deque. Empty ())
{
TMP = deque. Front ();
Deque. pop_front ();
Cout <TMP-> item <"";
If (TMP-> left! = NULL)
Deque. push_back (TMP-> left );
If (TMP-> right! = NULL)
Deque. push_back (TMP-> right );
}
}

Template <class T>
Void bitree <t>: displaytotal ()
{
Deque <bitreenode <t> *> deque1;
Deque <t> deque2;
Bitreenode <t> * TMP;
Int TL = 0;
Int TR = 0;
Int I;
Int COUNT = This-> countlayer (this-> root );
Deque1.push _ back (this-> root );
For (I = 1; I <= Ming (2, count)-1; I ++)
{
TMP = deque1.front ();
Deque1.pop _ Front ();
Deque2.push _ back (TMP-> item );
If (TMP-> left! = NULL)
{
TL = 0;
Deque1.push _ back (TMP-> left );
}
Else
{
Bitreenode <t> * P = new bitreenode <t> ('_');
Deque1.push _ back (P );
TL = 1;
}
If (TMP-> right! = NULL)
{
Tr = 0;
Deque1.push _ back (TMP-> right );
}
Else
{
Bitreenode <t> * P = new bitreenode <t> ('_');
Deque1.push _ back (P );
Tr = 1;
}
}

// The above is to complete the binary tree and become a Complete Binary Tree, stored in a queue deque2
// Deque2 print will pop up below
Int temp = Ming (2, Count + 1 );
Int temp1 = count;
Int DIS;
T tmp2;
For (I = 1; I <= count; I ++)
{
If (I = 1)
{
Disspace (temp );
Tmp2 = deque2.front ();
Deque2.pop _ Front ();
Cout <tmp2;
}
Else
{
Dis = temp-(ming (2, I-2)-1) * Ming (2, temp1)-ming (2, temp1)/2;
Disspace (DIS );
For (Int J = 1; j <= Ming (2, I-1); j ++)
{
Tmp2 = deque2.front ();
Deque2.pop _ Front ();
Cout <tmp2;
Disspace (ming (2, temp1)-1 );
}
Temp1 --;
}
Cout <Endl;
Cout <Endl;
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.