二叉樹非遞迴:中序,先序,後序,統一格式

來源:互聯網
上載者:User

整理二叉樹的一些常見的問題

#include <stack>#include <iostream>using namespace std;struct node//結點{int key;node* p;node* left;node* right;node(){}node(int k):key(k),p(NULL),left(NULL),right(NULL){}};struct TREE//樹{node* root;TREE():root(NULL){}};void tree_insert(TREE* T,node* z)//插入節點{node* y=NULL;node* x=T->root;//管理兩個指標,父指標y,y的子樹指標xwhile(x!=NULL)//一直向下遍曆到z應該插入的位置{y=x;if(x->key < z->key)x=x->right;else x=x->left;}z->p=y;//先將z的父指標p指向yif(y==NULL)//若樹為空白,樹根即為zT->root=z;else if(z->key < y->key)//否則分插入左邊還是右邊y->left=z;else y->right=z;}void in_walk(node* x)//遞迴中序遍曆{if(x!=NULL){in_walk(x->left);cout<<x->key<<' ';in_walk(x->right);}}void prec_walk(node* x)//遞迴先序遍曆{if(x!=NULL){cout<<x->key<<' ';prec_walk(x->left);prec_walk(x->right);}}void post_walk(node* x)//遞迴後序遍曆{if(x!=NULL){post_walk(x->left);post_walk(x->right);cout<<x->key<<' ';}}void itera_in_walk(node *root)//迭代中序遍曆{node* p=root;stack<node*> st;//利用棧if(!p)//if(p)表示“如果p不為空白”,if(!p)表示:如果p為空白return ;while(p || !st.empty())//當p不為空白且st不為空白{while(p)//沿著左孩子方向走到最左下。同時壓棧{st.push(p);p=p->left;}p=st.top();//擷取棧頂元素cout<<p->key<<" ";st.pop();p=p->right;}}void itera_pre_walk(node* root)//迭代先序遍曆{node* p=root;stack<node* > st;if(!p) return ;while(p || !st.empty()){while(p){cout<<p->key<<" ";st.push(p);p=p->left;}p=st.top();st.pop();p=p->right;}}void itera_post_walk(node* root){node* p=root;stack<node* > st;node* pre=NULL;//pre表示最近一次訪問的結點if(!p) return ;while(p || !st.empty()){while(p)//沿著左孩子方向走到最左下{st.push(p);p=p->left;}p=st.top();//擷取棧頂元素if( !p->right || p->right ==pre)//如果p沒有右孩子或者其右孩子剛剛被訪問過.小竅門if (!p)表示如果p為空白{st.pop();cout<<p->key<<' ';pre=p;//最近一次訪問的節點是pp=NULL;//使下一次迴圈不再執行p節點以下的壓棧操作}elsep=p->right;}}void main(){int A[]={15,6,18,3,7,17,20,2,4,13,9};int len=sizeof(A)/sizeof(A[0]);TREE* T=new TREE;node* z;for(int i=0;i<len;i++){z=new node(A[i]);tree_insert(T,z);}cout<<"__Recursion_in_walk__"<<endl;in_walk(T->root);cout<<endl;cout<<"iterater_Postwalk:"<<endl;itera_in_walk(T->root);cout<<endl;cout<<"__prec_walk__"<<endl;prec_walk(T->root);cout<<endl;cout<<"iterater_pre_walk"<<endl;itera_pre_walk(T->root);cout<<endl;cout<<"__post_walk__"<<endl;post_walk(T->root);cout<<endl;cout<<"itera_post_walk"<<endl;itera_post_walk(T->root);}

二叉搜尋樹的遞迴插入:

node* insert(node* root,node* z){if(!root)root=z;else if(z->key < root->key)root->left=insert(root->left,z);else root->right=insert(root->right,z);return root;}

記住一些小竅門:

if(!x)等價於if x 為空白。等價於if (x==NULL)

if(x)等價於if x 不為空白。等價於if(x!=NULL)

最主要是的是if(!x)的記憶法,我經常都搞不清楚,或者是用的時候不熟練。

我覺得可以這麼理解 ! 可以理解為“非”,“無”,“沒有",“不”,“否”等等意思,於是if(!x)表示的就是“如果沒有x,如果無x”等意思,"如果沒有x"即“如果x為空白”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.