二叉树遍历的一些代码
1 // binary_tree.cpp : Defines the entry point for the console application. 2 // 3 #include<iostream> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 class binary_node 8 { 9 public: 10 int value; 11 binary_node *left,*right; 12 13 binary_node(){}; 14 binary_node(int v,binary_node*l ,binary_node* r) 15 { 16 value = v; 17 left = l; 18 right = r; 19 } 20 void visit() 21 { 22 cout<<value<<' '; 23 } 24 }; 25 26 void pre_order_r(binary_node* root) 27 { 28 static int count = 0; 29 count++; 30 //cout<<count<<' '; 31 //if(root == NULL) 32 // return; 33 root->visit(); 34 if(root->left) 35 pre_order_r(root->left); 36 if(root->right) 37 pre_order_r(root->right); 38 } 39 40 void in_order_r(binary_node* root) 41 { 42 //if(root == NULL) 43 // return; 44 if(root->left) 45 in_order_r(root->left); 46 root->visit(); 47 if(root->right) 48 in_order_r(root->right); 49 } 50 51 void post_order_r(binary_node* root) 52 { 53 //if(root == NULL) 54 // return; 55 if(root->left) 56 post_order_r(root->left); 57 if(root->right) 58 post_order_r(root->right); 59 root->visit(); 60 } 61 62 void pre_order_nr(binary_node * root) 63 { 64 stack<binary_node*> bin_stack; 65 int count = 0; 66 while(root != NULL || !bin_stack.empty()) 67 { 68 count++; 69 if(root == NULL) 70 { 71 root = bin_stack.top(); 72 bin_stack.pop(); 73 } 74 else 75 { 76 root->visit(); 77 bin_stack.push(root->right); 78 root = root->left; 79 } 80 } 81 cout<<"count = "<<count; 82 } 83 84 void pre_order_nr2(binary_node *root) 85 { 86 stack<binary_node*> bin_stack; 87 int count = 0; 88 bin_stack.push(NULL); 89 while(root != NULL) 90 { 91 count++; 92 { 93 root->visit(); 94 if(root->right) 95 bin_stack.push(root->right); 96 if(root->left) 97 root = root->left; 98 else{ 99 root = bin_stack.top(); 100 bin_stack.pop(); 101 } 102 } 103 } 104 cout<<"count = "<<count; 105 } 106 107 void in_order_nr(binary_node* root) 108 { 109 stack<binary_node*> bin_stack; 110 int count = 0; 111 while(root != NULL || !bin_stack.empty()) 112 { 113 count++; 114 if(root == NULL) 115 { 116 root = bin_stack.top(); 117 bin_stack.pop(); 118 root->visit(); 119 root = root->right; 120 } 121 else 122 { 123 bin_stack.push(root); 124 root = root->left; 125 } 126 } 127 cout<<"count = "<<count; 128 } 129 130 void in_order_nr2(binary_node* root)//这样好像不行 131 { 132 stack<binary_node*> bin_stack; 133 int count = 0; 134 while(root != NULL || !bin_stack.empty()) 135 { 136 count++; 137 if(root == NULL) 138 { 139 root = bin_stack.top(); 140 bin_stack.pop(); 141 root->visit(); 142 if(root->right) 143 root = root->right; 144 } 145 else 146 { 147 bin_stack.push(root); 148 if(root->left) 149 root = root->left; 150 } 151 } 152 cout<<"count = "<<count; 153 } 154 155 void level_order(binary_node *root) 156 { 157 queue<binary_node*> bin_queue; 158 bin_queue.push(root); 159 while(!bin_queue.empty()) 160 { 161 root = bin_queue.front(); 162 bin_queue.pop(); 163 root->visit(); 164 if(root->left) 165 bin_queue.push(root->left); 166 if(root->right) 167 bin_queue.push(root->right); 168 } 169 } 170 171 void level_order2(binary_node *root)//这个方法明显不如上一种简洁,有点软件流水线的感觉。。。 172 { 173 queue<binary_node*> bin_queue; 174 do 175 { 176 if(root->left) 177 bin_queue.push(root->left); 178 if(root->right) 179 bin_queue.push(root->right); 180 root->visit(); 181 if(!bin_queue.empty()) 182 { 183 root = bin_queue.front(); 184 bin_queue.pop(); 185 } 186 }while(!bin_queue.empty()); 187 root->visit(); 188 } 189 190 void level_order3(binary_node *root)//还是第一种最简洁 191 { 192 queue<binary_node*> bin_queue; 193 while(!bin_queue.empty() || root) 194 { 195 if(root == NULL) 196 { 197 root = bin_queue.front(); 198 bin_queue.pop(); 199 } 200 else{ 201 root->visit(); 202 bin_queue.push(root->left); 203 bin_queue.push(root->right); 204 } 205 if(!bin_queue.empty()) 206 { 207 root = bin_queue.front(); 208 bin_queue.pop(); 209 } 210 } 211 } 212 213 int main() 214 { 215 const int size = 7; 216 binary_node *node_array = new binary_node[size]();//写成node_array[]就不行 217 int i; 218 for(i = 0; i < size/2; i++) 219 { 220 node_array[i].value = i; 221 node_array[i].left = &node_array[2 * i + 1]; 222 node_array[i].right = &node_array[2 * i + 2]; 223 } 224 for(i;i<size;i++) 225 { 226 node_array[i].value = i; 227 node_array[i].left = node_array[i].right = NULL; 228 } 229 cout<<"pre_r: "; 230 pre_order_r(&node_array[0]); 231 cout<<endl; 232 233 cout<<"pre_nr: "; 234 pre_order_nr(&node_array[0]); 235 cout<<endl; 236 237 cout<<"pre_nr2: "; 238 pre_order_nr2(&node_array[0]); 239 cout<<endl; 240 241 cout<<"in_r: "; 242 in_order_r(&node_array[0]); 243 cout<<endl; 244 245 cout<<"in_nr: "; 246 in_order_nr(&node_array[0]); 247 cout<<endl; 248 249 cout<<"post_r: "; 250 post_order_r(&node_array[0]); 251 cout<<endl; 252 253 cout<<"level:"; 254 level_order(&node_array[0]); 255 cout<<endl; 256 257 cout<<"level2:"; 258 level_order2(&node_array[0]); 259 cout<<endl; 260 261 cout<<"level3:"; 262 level_order3(&node_array[0]); 263 cout<<endl; 264 265 return 0; 266 }
posted on 2013-09-30 20:36 zcranberry 阅读(177) 评论(0) 编辑 收藏 举报