本博文转载自
https://blog.youkuaiyun.com/Tanswer_/article/details/52965108
https://www.cnblogs.com/SHERO-Vae/p/5800363.html
首先定义节点
typedef struct BTree
{
int value;
struct BTree *lchild;
struct BTree *rchild;
}BTree;
前序递归建立二叉树
/*
**num 前序序列
**index 下标
*/
BTree *CreateBTree(BTree *node,int *num,int& index)
{
if(num[index] == 0)
return NULL;
else
{
node = new BTree;
node -> value = num[index];
node -> lchild = CreateBTree(node->lchild,num,++index);
node -> rchild = CreateBTree(node->rchild,num,++index);
}
return node;
}
几种遍历
递归--前序遍历
void preOrder(BTree * root)
{
if(root == NULL)
return;
cout << root -> value << " "; //先输出树的根节点的值
preOrder(root -> lchild); //递归 左子树
preOrder(root -> rchild); //递归 右子树
}
非递归--前序遍历
根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:
对于任一结点P:
1) 访问结点P,并将结点P入栈;
2) 判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,
循环至1);若不为空,则将P的左孩子置为当前的结点P;
3) 直到P为NULL并且栈为空,则遍历结束。
void preOrder_dxm(BTree * root)
{
stack<BTree*> S;
BTree *p = root;
while(p != NULL || !S.empty())
{
while(p != NULL)
{
cout << p -> value << " ";
S.push(p);
p = p -> lchild;
}
if(!S.empty())
{
S.pop();
if(S.empty())
return ;
p = S.top();
S.pop();
p = p -> rchild;
}
}
}
递归--中序遍历
void inOrder(BTree * root)
{
if(root == NULL)
return;
inOrder(root -> lchild);
cout << root -> value << " ";
inOrder(root -> rchild);
}
非递归--中序遍历
根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:
对于任一结点P,
1)若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;
2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的P置为栈顶结点的右孩子;
3)直到P为NULL并且栈为空则遍历结束。
void inOrder_dxm(BTree * root)
{
stack<BTree*> S;
BTree *p = root;
while(p != NULL || !S.empty())
{
while(p != NULL)
{
//cout << p -> value << " ";
S.push(p);
p = p -> lchild;
}
if(!S.empty())
{
p = S.top();
cout << p -> value << " ";
S.pop();
if(S.empty())
return ;
//S.pop();
p = S.top();
cout << p -> value << " ";
S.pop();
p = p -> rchild;
}
}
}
递归--后序遍历
void postOrder(BTree * root)
{
if(root == NULL)
return;
postOrder(root -> lchild);
postOrder(root -> rchild);
cout << root -> value << " ";
}
非递归--后序遍历
要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它;或者P存 在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了 每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。
void postOrder_dxm(BTree * root)
{
stack<BTree*> S;
BTree *cur;
BTree *pre = NULL;
S.push(root);
while(!S.empty())
{
cur = S.top();
if((cur -> lchild == NULL && cur -> rchild == NULL) ||
(pre != NULL && (pre == cur -> lchild || pre == cur ->rchild)))
{
cout << cur -> value << " ";
S.pop();
pre = cur;
}
else
{
if(cur -> rchild != NULL)
S.push(cur -> rchild);
if(cur -> lchild != NULL)
S.push(cur -> lchild);
}
}
}
树状打印二叉树
void print(BTree *root,int h)
{
if(root != NULL)
{
print(root -> rchild,h+1);
for(int i=0; i<h; i++)
cout << " ";
cout << root -> value;
print(root -> lchild,h+1);
}
cout << endl;
}
主函数
int main()
{
int num[] = {1,2,4,8,0,0,9,0,0,5,10,0,0,11,0,0,3,6,12,0,0,13,0,0,7,14,0,0,15,0,0};
BTree *root = NULL;
int index = 0;
root = CreateBTree(root,num,index);
cout << "前序非递归遍历: " << endl;
preOrder_dxm(root);
cout << endl;
cout << "中序递归遍历: " << endl;
inOrder_dxm(root);
cout << endl;
cout << "后续非递归遍历: " << endl;
postOrder_dxm(root);
cout << endl << endl;;
cout << "此二叉树的形状为: " << endl;
print(root,1);
return 0;
}
二叉树形状竖着看