Home > Back-end >  In this code calculation based on binary tree nodes, leaf nodes, and height
In this code calculation based on binary tree nodes, leaf nodes, and height

Time:11-12

#include
using namespace std;
Struct BiNode
{
char data;
Lchild BiNode * and * rchild;
};
The class BiTree
{
Public:
BiTree () {root=Creat (root); }//constructor, the establishment of a binary tree
~ BiTree () {Release (root); }//destructors, release of each node storage
Void PreOrder () {PreOrder (root); }//preorder traversing binary tree
Void InOrder () {InOrder (root); }//sequence traversal of binary tree
Void PostOrder () {PostOrder (root); }//order after traversing binary tree
Private:
Creat BiNode * (bt) BiNode *;//constructor calls
Void Release (bt) BiNode *;//the destructor call
Void PreOrder BiNode * (bt);//before sequence traversal function call
Void InOrder BiNode * (bt);//in the sequence traversal function call
Void PostOrder BiNode * (bt);//after sequence traversal function call

BiNode * root;//pointer to the head of the root node
};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Binary list class BiTree preorder traversal
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void BiTree: : PreOrder BiNode * (bt)
{
If (bt==nullptr) return;//the end of the recursive call condition
The else {
Cout & lt; PreOrder (bt - & gt; Lchild);//first order recursive traversal bt left subtree
PreOrder (bt - & gt; Rchild);//first order recursive traversal of bt right subtree
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
In the binary class list BiTree sequence traversal
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void BiTree: : InOrder (bt) BiNode *
{
If (bt==nullptr) return;//the end of the recursive call condition
The else {
InOrder (bt - & gt; Lchild);//first order recursive traversal bt left subtree
Cout & lt; InOrder (bt - & gt; Rchild);//first order recursive traversal of bt right subtree
}
}


BiNode * BiTree: : Creat (bt) BiNode *
{
char ch;
Cout & lt; <"Please enter the extension, the preamble of binary tree traversal sequence input one character at a time:";
Cin & gt;> Ch;//the data information of input nodes, assuming the characters
If (ch=='#') bt=nullptr;//
a hollow treeThe else {
Bt=new BiNode; Bt - & gt; Data=https://bbs.csdn.net/topics/ch;
Bt - & gt; Lchild=Creat (bt - & gt; Lchild);//recursive building left subtree
Bt - & gt; Rchild=Creat (bt - & gt; Rchild);//recursive set up right subtree
}
The return of bt;
}
Void BiTree: : Release (bt) BiNode *
{
If (bt==nullptr) return;
The else {
Release (bt - & gt; Lchild);//release the left subtree
Release (bt - & gt; Rchild);//release right subtree
Delete the bt;//release the root node
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
After the binary class list BiTree sequence traversal
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void BiTree: : PostOrder BiNode * (bt)
{
If (bt==nullptr) return;//the end of the recursive call condition
The else {
InOrder (bt - & gt; Lchild);//first order recursive traversal bt left subtree
InOrder (bt - & gt; Rchild);//first order recursive traversal of bt right subtree
Cout & lt; }
}
Int main ()
{
BiTree T;//object variables T
Cout & lt; <"The preamble of the binary tree traversal sequence is:";
T.P the reOrder ();
Cout & lt; <"\ n the order in the binary tree traversal sequence is:";
T.I nOrder ();
Cout & lt; <"\ n order after the binary tree traversal sequence is:";
T.P ostOrder ();
return 0;
}
  • Related