【0】二叉树节点
struct MyStruct
{
int Nodedata = 0 ;
MyStruct * pleft = nullptr ;
MyStruct * pright = nullptr ;
} BTree, * pBTree;
【1】前序遍历
void qian ( MyStruct * proot)
{
if ( proot != nullptr )
{
cout << " " << proot- > Nodedata;
if ( proot- > pleft != nullptr )
{
qian ( proot- > pleft) ;
}
if ( proot- > pright != nullptr )
{
qian ( proot- > pright) ;
}
}
}
【2】中序遍历
void zhong ( MyStruct * proot)
{
if ( proot!= nullptr )
{
if ( proot- > pleft!= nullptr )
{
zhong ( proot- > pleft) ;
}
cout << " " << proot- > Nodedata;
if ( proot- > pright != nullptr )
{
zhong ( proot- > pright) ;
}
}
}
【3】非递归中序遍历
void stackzhong ( 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;
}
}
}
【4】后序遍历
void hou ( MyStruct * proot)
{
if ( proot != nullptr )
{
if ( proot- > pleft != nullptr )
{
hou ( proot- > pleft) ;
}
if ( proot- > pright != nullptr )
{
hou ( proot- > pright) ;
}
cout << " " << proot- > Nodedata;
}
}
【5】插入节点
MyStruct * insertnode ( MyStruct * proot, int num)
{
if ( proot == nullptr )
{
MyStruct * pnew = new MyStruct;
pnew- > Nodedata = num;
proot = pnew;
}
else if ( num <= proot- > Nodedata)
{
proot- > pleft = insertnode ( proot- > pleft, num) ;
}
else
{
proot- > pright = insertnode ( proot- > pright, num) ;
}
return proot;
}
【6】寻找最大值
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;
}
【7】叶子节点个数
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;
}
【8】树的高度
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 ;
}
【9】获取某个数的父节点
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) ;
}
【10】返回左兄弟
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) ;
}
【11】 完整程序
#include "pch.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct MyStruct
{
int Nodedata = 0 ;
MyStruct * pleft = nullptr ;
MyStruct * pright = nullptr ;
} BTree, * pBTree;
int main ( )
{
MyStruct * pRoot;
MyStruct s1;
MyStruct s2;
MyStruct s3;
MyStruct s4;
MyStruct s5;
MyStruct s6;
MyStruct s7;
pRoot = & s1;
s1. Nodedata = 1 ;
s2. Nodedata = 2 ;
s3. Nodedata = 3 ;
s4. Nodedata = 4 ;
s5. Nodedata = 5 ;
s6. Nodedata = 6 ;
s7. Nodedata = 7 ;
s1. pleft = & s2;
s1. pright = & s3;
s2. pleft = & s4;
s2. pright = & s5;
s3. pleft = & s6;
s3. pright = & s7;
cout << "前序遍历二叉树" << endl;
qian ( pRoot) ;
cout << endl;
cout << "中序遍历二叉树" << endl;
zhong ( pRoot) ;
cout << endl;
cout << "后序遍历二叉树" << endl;
hou ( pRoot) ;
cout << endl;
MyStruct * pRootBack= insertnode ( pRoot, 17 ) ;
cout << "插入节点后序遍历二叉树" << endl;
hou ( pRoot) ;
cout << endl;
int maxnum = findmax ( pRoot) ;
cout<< "二叉树中的最大值:" << maxnum<< endl;
cin. get ( ) ;
}