#include<iostream>
#include <string>
#include <stack>
using namespace std;
struct MyStruct
{
int Nodedata;
MyStruct *pLeft;
MyStruct *pRight;
}BTree,*pBTree;
void show(MyStruct *proot ,
int n)
{
if (proot==
nullptr)
{
return;
}
else
{
show(proot->pRight, n +
1);
for (
int i =
0; i < n;i++)
{
cout <<
" ";
}
cout << proot->Nodedata << endl;
show(proot->pLeft, n +
1);
}
}
void zhong(MyStruct *proot)
{
if (proot!=
nullptr)
{
if (proot->pLeft!=
nullptr)
{
zhong(proot->pLeft);
}
cout <<
" " << proot->Nodedata ;
if (proot->pRight!=
nullptr)
{
zhong(proot->pRight);
}
}
}
void xian(MyStruct *proot)
{
if (proot!=
nullptr)
{
cout <<
" " << proot->Nodedata ;
if (proot->pLeft!=
nullptr)
{
xian(proot->pLeft);
}
if (proot->pRight!=
nullptr)
{
xian(proot->pRight);
}
}
}
void hou(MyStruct *proot)
{
if (proot!=
nullptr)
{
if (proot->pLeft!=
nullptr)
{
hou(proot->pLeft);
}
if (proot->pRight!=
nullptr)
{
hou(proot->pRight);
}
cout <<
" " << proot->Nodedata ;
}
}
void stackzhong(MyStruct *proot)
{
MyStruct * pcurr = proot;
MyStruct * mystack[
100];
int top =
0;
while ( top!=
0 || pcurr !=
nullptr)
{
while (pcurr !=
nullptr)
{
mystack[top++] = pcurr;
pcurr = pcurr->pLeft;
}
if (top>
0)
{
top--;
pcurr = mystack[top];
cout <<
" " << pcurr->Nodedata << endl;
pcurr = pcurr->pRight;
}
}
}
void stackzhongA(MyStruct *proot)
{
MyStruct * pcurr = proot;
stack<MyStruct *> mystack;
while (!mystack.empty() || pcurr !=
nullptr)
{
while (pcurr !=
nullptr)
{
mystack.push(pcurr);
pcurr = pcurr->pLeft;
}
if (!mystack.empty())
{
pcurr = mystack.top();
cout <<
" " << pcurr->Nodedata << endl;
mystack.pop();
pcurr = pcurr->pRight;
}
}
}
int getyenum(MyStruct *proot)
{
int left =
0;
int right =
0;
if (proot==
nullptr)
{
return 0;
}
if (proot->pLeft==
nullptr && proot->pRight==
nullptr)
{
return 1;
}
left = getyenum(proot->pLeft);
right = getyenum(proot->pRight);
return left + right;
}
int getheight(MyStruct *proot)
{
int height =
0;
int left =
0;
int right =
0;
if (proot ==
nullptr)
{
return 0;
}
left = getheight(proot->pLeft);
right = getheight(proot->pRight);
height = left > right ? left : right;
return height +
1;
}
void ceng(MyStruct *proot)
{
if (proot ==
nullptr)
{
return;
}
MyStruct * myq[
100];
int tou =
0;
int wei =
0;
MyStruct * pcurr =
nullptr;
myq[wei++] = proot;
while (tou !=wei)
{
pcurr = myq[tou];
tou++;
cout << pcurr->Nodedata << endl;
if (pcurr->pLeft!=
nullptr)
{
myq[wei++] = pcurr->pLeft;
}
if (pcurr->pRight !=
nullptr)
{
myq[wei++] = pcurr->pRight;
}
}
}
int getba(MyStruct *pRoot,
int num)
{
if (pRoot==
nullptr)
{
return 0;
}
if (pRoot->pLeft!=
nullptr && pRoot->pLeft->Nodedata==num)
{
return pRoot->Nodedata;
}
if (pRoot->pRight !=
nullptr && pRoot->pRight->Nodedata == num)
{
return pRoot->Nodedata;
}
getba(pRoot->pLeft, num);
getba(pRoot->pRight, num);
}
int getleft(MyStruct *pRoot,
int num)
{
if (pRoot ==
nullptr)
{
return 0;
}
if (pRoot->pRight && pRoot->pRight->Nodedata == num)
{
if (pRoot->pLeft)
{
return pRoot->pLeft->Nodedata;
}
}
getleft(pRoot->pLeft, num);
getleft(pRoot->pRight, num);
}
int findmax(MyStruct *proot)
{
int max = -
99999;
MyStruct * pcurr = proot;
MyStruct * mystack[
100];
int top =
0;
while (top !=
0 || pcurr !=
nullptr)
{
while (pcurr !=
nullptr)
{
mystack[top++] = pcurr;
pcurr = pcurr->pLeft;
}
if (top >
0)
{
top--;
pcurr = mystack[top];
if (max< pcurr->Nodedata)
{
max = pcurr->Nodedata;
}
pcurr = pcurr->pRight;
}
}
return max;
}
MyStruct * insertnode(MyStruct *proot,
int num)
{
if (proot==
nullptr)
{
MyStruct *pnew =
new MyStruct;
pnew->Nodedata = num;
pnew->pLeft =
nullptr ;
pnew->pRight =
nullptr ;
proot = pnew;
}
else if ( num <= proot->Nodedata)
{
proot->pLeft = insertnode(proot->pLeft, num);
}
else
{
proot->pRight = insertnode(proot->pRight, num);
}
return proot;
}
void main01()
{
MyStruct *pRoot;
MyStruct s1 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s2 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s3 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s4 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s5 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s6 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s7 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s8 = {
0 ,
nullptr ,
nullptr} ;
MyStruct s9 = {
0 ,
nullptr ,
nullptr} ;
pRoot = &s1;
s1.Nodedata =
1;
s2.Nodedata =
2;
s3.Nodedata =
3;
s4.Nodedata =
4;
s5.Nodedata =
5;
s6.Nodedata =
6;
s7.Nodedata =
7;
s8.Nodedata =
8;
s9.Nodedata =
9;
s1.pLeft = &s2;
s1.pRight = &s3;
s2.pLeft = &s4;
s2.pRight = &s5;
s3.pLeft = &s6;
s3.pRight = &s7;
s4.pLeft = &s8 ;
s4.pRight =
nullptr ;
s8.pRight = &s9 ;
s8.pLeft =
nullptr ;
show( pRoot ,
1) ;
cout<<endl ;
zhong(pRoot) ;
cout<<endl ;
xian(pRoot) ;
cout<<endl ;
hou(pRoot) ;
cout<< endl << getyenum( pRoot );
cout<< endl << getheight( pRoot ) ;
ceng( pRoot ) ;
cin.get();
}
void mainA()
{
MyStruct *pRoot;
MyStruct sarray[
100];
pRoot = sarray;
for (
int i =
1; i <=
100;i++)
{
sarray[i].Nodedata = i;
}
for (
int i =
0; i <=
50;i++)
{
if (i<=(
99-
1)/
2)
{
sarray[i].pLeft = &sarray[
2 * i +
1];
}
if (i<=(
99-
2)/
2)
{
sarray[i].pRight = &sarray[
2 * i +
2];
}
}
show(pRoot,
1);
cin.get();
}
void main()
{
MyStruct *pRoot=
nullptr;
for (
int i =
0; i <
10; i+=
2)
{
pRoot = insertnode(pRoot, i);
}
for (
int i =
5; i >=
0; i-=
2)
{
pRoot = insertnode(pRoot, i);
}
show(pRoot,
1);
cin.get();
}