第六章例題二叉樹層次遍歷
阿新 • • 發佈:2017-07-29
ear 指針 內存 寬度優先 def delete back blog value
1.指針實現
#include <iostream> #include <vector> #include <queue> #include <cstdio> #include <cstring> using namespace std; #define maxn 100 struct Node { bool have_value; int value; /*節點結構體*/ Node *left,*right; Node():have_value(false),left(NULL),right(NULL){} };/*全局變量寫起來更方便*/ char s[maxn]; Node* root=NULL; bool faile; /*用來防止內存泄漏*/ void remove_tree(Node* tree) { if(tree==NULL) return; remove_tree(tree->left); remove_tree(tree->right); delete tree; } /*創建新節點封裝成函數*/ Node* newnode() { return newNode();} /*添加新節點的函數*/ void addnode(int v,char* s) { int n=strlen(s); Node* u=root; for(int i=0;i<n;i++) { if(s[i]==‘L‘) { if(u->left==NULL) u->left=newnode(); u=u->left; } else if(s[i]==‘R‘) { if(u->right==NULL) u->right=newnode(); u=u->right; } } if(u->have_value) faile=true; u->value=v; u->have_value=true; } /*讀入數據並創建樹,成功返回true讀到文件結尾則返回false*/ bool read_input() { faile=false; remove_tree(root); root=newnode(); for(;;) { if(scanf("%s",s)!=1) return false; if(!strcmp(s,"()")) break; int v; sscanf(&s[1],"%d",&v); addnode(v,strchr(s,‘,‘)+1); } return true; } /*寬度優先算法,用隊列實現將結果存在向量中*/ bool bfs(vector<int>& ans) { queue<Node*> q; ans.clear(); q.push(root); while(!q.empty()) { Node* u=q.front(); q.pop(); if(!u->have_value) return false; ans.push_back(u->value); if(u->left!=NULL) q.push(u->left); if(u->right!=NULL) q.push(u->right); } return true; } int main() { vector<int> v; while(read_input()) { if(!bfs(v) || faile==true) printf("%d\n",-1); else for(vector<int>::iterator i = v.begin(); i != v.end(); ++i) printf("%d ",*i); cout<<endl; } }
2.數組實現
#include <iostream> #include <vector> #include <queue> #include <cstdio> #include <cstring> using namespace std; #define maxn 1000 bool have_value[maxn]; int tleft[maxn]; int tright[maxn]; int value[maxn]; char s[100]; bool faile; const int root=1; int cnt; void newtree() { tleft[root]=0; tright[root]=0; have_value[root]=false; cnt=root; } int newnode() { int u=++cnt; tleft[u]=0; tright[u]=0; have_value[u]=false; return u; } void addnode(int v,char* s) { int n=strlen(s); int u=root; for(int i=0;i<n;i++) { if(s[i]==‘L‘) { if(tleft[u]==0) tleft[u]=newnode(); u=tleft[u]; } else if(s[i]==‘R‘) { if(tright[u]==0) tright[u]=newnode(); u=tright[u]; } } if(have_value[u]) faile=true; value[u]=v; have_value[u]=true; } bool read_input() { faile=false; newtree(); for(;;) { if(scanf("%s",s)!=1) return false; if(!strcmp(s,"()")) break; int v; sscanf(&s[1],"%d",&v); addnode(v,strchr(s,‘,‘)+1); } return true; } bool bfs(vector<int>& ans) { queue<int> q; ans.clear(); q.push(root); while(!q.empty()) { int u=q.front(); q.pop(); if(!have_value[u]) return false; ans.push_back(value[u]); if(tleft[u]!=0) q.push(tleft[u]); if(tright[u]!=0) q.push(tright[u]); } return true; } int main() { vector<int> v; while(read_input()) { if(!bfs(v) || faile==true) printf("%d\n",-1); else for(vector<int>::iterator i = v.begin(); i != v.end(); ++i) printf("%d ",*i); cout<<endl; } }
書上的接口寫的太棒了,換了種實現方式,代碼基本上沒改,比我自己寫的接口不知道高到哪裏去了.
第六章例題二叉樹層次遍歷