常見的二元樹中序走法版本為 Recursive 版本 (參考這裡), 這裡介紹 Iterative 版本.
範例代碼 :
- Pseudo code :
- traverse(node) {
- if(!node) return;
- while (!empty(stack) && node->right==null) {
- /*Remember the left nodes in stack*/
- while (node->left && flag) {
- push(stack, node);
- node = node->left;
- }
- /*Process the node*/
- printf("%d", node->data);
- /*Do the tail recursion*/
- if(node->right) {
- node = node->right
- flag = true;
- } else {
- node = pop(stack); /*New Node will be from previous*/
- flag = false;
- }
- }
- }
- Implementation in C :
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- struct Node {
- int value;
- Node *left;
- Node *right;
- };
- Node* addNewNode(Node *root, int val) {
- if(root==NULL) {
- cout << "Build root(" << val << ")..." << endl;
- root = new Node();
- root->value = val;
- root->left = root->right = NULL;
- return root;
- } else {
- Node *tmp = root;
- bool flag = false;
- while(tmp!=NULL) {
- if(tmp->value>val) { // 往左子樹移動
- if(tmp->left!=NULL) tmp = tmp->left;
- else {
- cout << "Build new lnode(" << val << ")..." << endl;
- tmp->left = new Node();
- tmp->left->value = val;
- tmp->left->left = tmp->left->right = NULL;
- flag = true;
- break;
- }
- } else if(tmp->value
// 往右子樹移動 - if(tmp->right!=NULL) tmp = tmp->right;
- else {
- cout << "Build new rnode(" << val << ")..." << endl;
- tmp->right = new Node();
- tmp->right->value = val;
- tmp->right->left = tmp->right->right = NULL;
- flag = true;
- break;
- }
- } else {
- break; // The value already exist
- }
- }
- return root;
- }
- }
- void inorderVisit(Node *root) {
- if(root!=NULL) {
- inorderVisit(root->left);
- cout << root->value << " ";
- inorderVisit(root->right);
- }
- }
- // Iterative Inorder Visit : http://datastructures.itgo.com/trees/nrtraversal.htm
- // root 為一個h的根, n 為中序追蹤的第幾n個節點。
- // 函數最後要回傳要回傳中序追蹤第n個節點內的value。
- // 補充說明: int findValue()就是要找這個binary tree第n大的數
- int findValue(Node *root, int n) {
- Node *tmp = root;
- stack
mystack; - //mystack.push(tmp);
- int cnt = n, rst = -1;
- bool flag = true;
- while(cnt>0) {
- /*Remember the left nodes in stack*/
- while(tmp->left && flag) {
- mystack.push(tmp);
- tmp = tmp->left;
- }
- /*Process the node*/
- rst = tmp->value;
- printf("Count %d (%d)...\n", cnt, rst);
- /*Do the tail recursion*/
- if(tmp->right) {
- tmp = tmp->right;
- flag = true;
- } else {
- tmp = mystack.top();
- mystack.pop(); // http://www.cplusplus.com/reference/stl/stack/pop/
- flag = false;
- }
- cnt--;
- if(mystack.empty() && tmp->right==NULL) break;
- }
- if(cnt==0 && n>0) return rst;
- return -1;
- }
- void main(){
- Node *root = NULL;
- int n = 5;
- int data[] = {2,3,1,5,7,8,6,4,9,10};
- /*Build Binary Search Tree*/
- for(int i=0; i<10; i++) root = addNewNode(root, data[i]);
- /*Inorder Traversal*/
- inorderVisit(root);
- cout << endl;
- /*Find value in location=n*/
- cout << "Location(" << n << ")=" << findValue(root, n) << endl;
- }
Output :
Build root(2)...
Build new rnode(3)...
Build new lnode(1)...
Build new rnode(5)...
Build new rnode(7)...
Build new rnode(8)...
Build new lnode(6)...
Build new lnode(4)...
Build new rnode(9)...
Build new rnode(10)...
1 2 3 4 5 6 7 8 9 10
Count 5 (1)...
Count 4 (2)...
Count 3 (3)...
Count 2 (4)...
Count 1 (5)...
Location(5)=5
補充說明 :
* Non-recursive traversal using a Threaded Binary Tree
* Help me understand Inorder Traversal without using recursion
沒有留言:
張貼留言