1 struct TreeNode { 2 char val; 3 TreeNode *left; 4 TreeNode *right; 5 int visitCount; //节点访问次数,后序遍历会用到 6 TreeNode(char c) : val(c), left(NULL), right(NULL), visitCount(0) {} 7 }; 8 9 //先序遍历_非递归 10 void PreOrderTraverse(TreeNode *root) 11 { 12 if (root == NULL) 13 return; 14 15 stack<TreeNode*> node_stack; 16 TreeNode *p = root; 17 while (p || !node_stack.empty()) { 18 if (p) { 19 cout << p->val << ' '; 20 node_stack.push(p); 21 p = p->left; 22 } else { 23 p = node_stack.top(); 24 node_stack.pop(); 25 p = p->right; 26 } 27 } 28 } 29 30 //先序遍历_递归 31 void PreOrderTraverse_1(TreeNode *root) 32 { 33 if (root == NULL) 34 return; 35 cout << root->val << ' '; 36 PreOrderTraverse_1(root->left); 37 PreOrderTraverse_1(root->right); 38 } 39 40 //中序遍历_非递归 41 void InOrderTraverse(TreeNode *root) 42 { 43 if (root == NULL) 44 return; 45 46 stack<TreeNode*> node_stack; 47 TreeNode *p = root; 48 while (p || !node_stack.empty()) { 49 if (p) { 50 node_stack.push(p); 51 p = p->left; 52 } else { 53 p = node_stack.top(); 54 node_stack.pop(); 55 cout << p->val << ' '; 56 p = p->right; 57 } 58 } 59 } 60 61 //中序遍历_递归 62 void InOrderTraverse_1(TreeNode *root) 63 { 64 if (root == NULL) 65 return; 66 InOrderTraverse_1(root->left); 67 cout << root->val << ' '; 68 InOrderTraverse_1(root->right); 69 } 70 71 //后序遍历_非递归 72 void PostOrderTraverse(TreeNode *root) 73 { 74 if (root == NULL) 75 return; 76 77 stack<TreeNode*> node_stack; 78 TreeNode *p = root; 79 while (p || !node_stack.empty()) { 80 if (p) { 81 p->visitCount = 1; 82 node_stack.push(p); 83 p = p->left; 84 } else { 85 p = node_stack.top(); 86 node_stack.pop(); 87 if (p->visitCount == 2) { 88 cout << p->val << ' '; 89 p = NULL; 90 } else { 91 p->visitCount++; 92 node_stack.push(p); 93 p = p->right; 94 } 95 } 96 } 97 } 98 99 //后序遍历_递归 100 void PostOrderTraverse_1(TreeNode *root) 101 { 102 if (root == NULL) 103 return; 104 PostOrderTraverse_1(root->left); 105 PostOrderTraverse_1(root->right); 106 cout << root->val << ' '; 107 } 108 109 //层序遍历 110 void LevelOrderTraverse(TreeNode *root) 111 { 112 if (root == NULL) 113 return; 114 115 queue<TreeNode*> node_queue; 116 node_queue.push(root); 117 while (!node_queue.empty()) { 118 TreeNode *p = node_queue.front(); 119 node_queue.pop(); 120 cout << p->val << ' '; 121 122 if (p->left) 123 node_queue.push(p->left); 124 if (p->right) 125 node_queue.push(p->right); 126 } 127 }
以下图二叉树为例:
1 void CreateBiTree(TreeNode* &T) 2 { 3 char ch; 4 cin >> ch; 5 if (ch == '#') { 6 T = NULL; 7 } else { 8 T = new TreeNode(ch); 9 CreateBiTree(T->left); 10 CreateBiTree(T->right); 11 } 12 } 13 14 int main() 15 { 16 TreeNode *root; 17 CreateBiTree(root); 18 19 cout << "先序遍历_非递归: " << endl; 20 PreOrderTraverse(root); 21 cout << endl; 22 cout << "先序遍历_递归: " << endl; 23 PreOrderTraverse_1(root); 24 cout << endl; 25 26 cout << "中序遍历_非递归: " << endl; 27 InOrderTraverse(root); 28 cout << endl; 29 cout << "中序遍历_递归: " << endl; 30 InOrderTraverse_1(root); 31 cout << endl; 32 33 cout << "后序遍历_非递归: " << endl; 34 PostOrderTraverse(root); 35 cout << endl; 36 cout << "后序遍历_递归: " << endl; 37 PostOrderTraverse_1(root); 38 cout << endl; 39 40 cout << "层序遍历: " << endl; 41 LevelOrderTraverse(root); 42 cout << endl; 43 44 return 0; 45 }
结果如下: