#include <iostream>#defineMaxSize 5using namespaceStd;typedefstructbtnode{Chardata; structBtnode *Lchild; structBtnode *Rchild;} Btnode; Btnode*Initbtnode () {Btnode*node = (btnode*)malloc(sizeof(Btnode)); Node->lchild=0; Node->rchild=0; returnnode;} Btnode* Init (Btnode *p) {Btnode*a=Initbtnode (); Btnode*b=Initbtnode (); Btnode*c=Initbtnode (); Btnode*d=Initbtnode (); Btnode*e=Initbtnode (); Btnode*f=Initbtnode (); A->data='A'; B->data='B'; C->data='C'; D->data='D'; E->data='E'; F->data='F'; C->lchild=E; C->rchild=F; B->lchild=D; A->rchild=C; A->lchild=B; P=A; returnp;}voidVisit (Btnode *p) {cout<< P->data <<" ";}voidPreorder (Btnode *p) { if(p!=0) {visit (P); Preorder (P-lchild); Preorder (P-rchild); }}voidInorder (Btnode *p) { if(p!=0) {inorder (P-lchild); Visit (p); Inorder (P-rchild); }}voidPostorder (Btnode *p) { if(p!=0) {postorder (P-lchild); Postorder (P-rchild); Visit (p); }}voidLevel (Btnode *p) { intfront,rear; Btnode*Que[maxsize]; Front= Rear =0; Btnode*Q; if(p!=0) {Rear= (rear+1)%maxSize; Que[rear]=p; while(front!=rear) {Front= (front+1)%maxSize; Q=Que[front]; Visit (q); if(q->lchild!=0) {Rear= (rear+1)%maxSize; Que[rear]=q->Lchild; } if(q->rchild!=0) {Rear= (rear+1)%maxSize; Que[rear]=q->Rchild; } } }}intMainintargcChar*argv[]) {Btnode*node=NewBtnode; Btnode*p=init (node); cout<<"first-order traversal:" ; Preorder (p); cout<<Endl; cout<<"Middle sequence Traversal:" ; Inorder (P); cout<<Endl; cout<<"Post-post traversal:" ; Postorder (P); cout<<Endl; cout<<"Hierarchical traversal:" ; Level (P); cout<<Endl; return 0;}
C + + hierarchy traversal binary tree