赞
踩
#include <bits/stdc++.h> using namespace std; const int N=1e5+5; struct Node{ char value; int lson, rson; }tree[N]; int index=1; int newNode(char val) { tree[index].value=val; tree[index].lson=tree[index].rson=0; return index++; } void insert(int &father,int child,int l_r){ if(l_r==0) tree[father].lson=child; else tree[father].rson=child; } int dfn[N]={0}; int dfn_timer=0; void dfn_order(int father) { if(father!=0){ dfn[father]=++dfn_timer; printf("dfn[%c]=%d;",tree[father].value,dfn[father]); dfn_order(tree[father].lson); dfn_order(tree[father].rson); } } int visit_timer=0; void visit_order(int father) { if(father!=0){ printf("visit[%c]=%d;",tree[father].value,++visit_timer); visit_order(tree[father].lson); visit_order(tree[father].rson); printf("visit[%c]=%d;",tree[father].value,++visit_timer); } } int deep[N]={0}; int deep_timer=0; void deep_node(int father) { if(father!=0) { deep[father]=++deep_timer; printf("deep[%c]=%d;",tree[father].value,deep[father]); deep_node(tree[father].lson); deep_node(tree[father].rson); deep_timer--; } } int num[N]={0}; int num_node(int father){ if(father==0) return 0; else { num[father]=num_node(tree[father].lson)+num_node(tree[father].rson)+1; printf("num[%c]=%d;",tree[father].value,num[father]); return num[father]; } } void preorder(int father) { if(father!=0) { cout << tree[father].value << " "; preorder(tree[father].lson); preorder(tree[father].rson); } } void inorder(int father) { if(father!=0) { inorder(tree[father].lson); cout << tree[father].value << " "; inorder(tree[father].rson); } } void postorder(int father) { if(father!=0) { postorder(tree[father].lson); postorder(tree[father].rson); cout << tree[father].value << " "; } } int buildtree() { int A=newNode('A'); int B=newNode('B'); int C=newNode('C'); int D=newNode('D'); int E=newNode('E'); int F=newNode('F'); int G=newNode('G'); int H=newNode('H'); int I=newNode('I'); insert(E,B,0); insert(E,G,1); insert(B,A,0); insert(B,D,1); insert(G,F,0); insert(G,I,1); insert(D,C,0); insert(I,H,0); int root=E; return root; } int main() { int root=buildtree(); cout<<"dfn_order:"; dfn_order(root); cout<<endl; cout<<"visit_order:"; visit_order(root); cout<<endl; cout<<"deep_node:"; deep_node(root); cout<<endl; cout<<"num_node:"; num_node(root); cout<<endl; cout<<"preorder:"; preorder(root); cout<<endl; cout<<"inorder:"; inorder(root); cout<<endl; cout<<"postorder:"; postorder(root); cout<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node{ char value; node* left; node* right; node(char value='#',node *left=NULL,node* right=NULL):value(value),left(left),right(right){} }; void preorder(node *root){ if(root!=NULL) { cout<<root->value<<" "; preorder(root->left); preorder(root->right); } } void inorder(node *root){ if(root!=NULL) { preorder(root->left); cout<<root->value<<" "; preorder(root->right); } } void postorder(node *root){ if(root!=NULL) { preorder(root->left); preorder(root->right); cout<<root->value<<" "; } } void remove_tree(node* root){ if(root==NULL) return; remove_tree(root->left); remove_tree(root->right); delete root; } int main() { node *A=new node('A'); node *B=new node('B'); node *C=new node('C'); node *D=new node('D'); node *E=new node('E'); node *F=new node('F'); node *G=new node('G'); node *H=new node('H'); node *I=new node('I'); E->left=B;E->right=G; B->left=A;B->right=D; G->left=F;G->right=I; D->left=C;I->left=H; cout<<"Preorder: ";preorder(E);cout<<endl; cout<<"Inorder: ";inorder(E);cout<<endl; cout<<"Postorder: ";postorder(E);cout<<endl; remove_tree(E); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。