线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
2013-02-21 13:08
375 查看
线索二叉树 Threaded Binary Tree
不用递归,不用栈,遍历二叉树Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In
this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.
1. Initialize current as root 2. While current is not NULL If current does not have left child a) Print current’s data b) Go to the right, i.e., current = current->right Else a) Make current as right child of the rightmost node in current's left subtree b) Go to this left child, i.e., current = current->left
Although the tree is modified through the traversal, it is reverted back to its original shape after the completion. Unlike Stack based traversal, no extra space
is required for this traversal.
#include<stdio.h> #include<stdlib.h> /* A binary tree tNode has data, pointer to left child and a pointer to right child */ struct tNode { int data; struct tNode* left; struct tNode* right; }; /* Function to traverse binary tree without recursion and without stack */ void MorrisTraversal(struct tNode *root) { struct tNode *current,*pre; if(root == NULL) return; current = root; while(current != NULL) { if(current->left == NULL) { printf(" %d ", current->data); current = current->right; } else { /* Find the inorder predecessor of current */ pre = current->left; while(pre->right != NULL && pre->right != current) pre = pre->right; /* Make current as right child of its inorder predecessor */ if(pre->right == NULL) { pre->right = current; current = current->left; } /* Revert the changes made in if part to restore the original tree i.e., fix the right child of predecssor */ else { pre->right = NULL; printf(" %d ",current->data); current = current->right; } /* End of if condition pre->right == NULL */ } /* End of if condition current->left == NULL*/ } /* End of while */ } /* UTILITY FUNCTIONS */ /* Helper function that allocates a new tNode with the given data and NULL left and right pointers. */ struct tNode* newtNode(int data) { struct tNode* tNode = (struct tNode*) malloc(sizeof(struct tNode)); tNode->data = data; tNode->left = NULL; tNode->right = NULL; return(tNode); } /* Driver program to test above functions*/ int main() { /* Constructed binary tree is 1 / \ 2 3 / \ 4 5 */ struct tNode *root = newtNode(1); root->left = newtNode(2); root->right = newtNode(3); root->left->left = newtNode(4); root->left->right = newtNode(5); MorrisTraversal(root); getchar(); return 0; }
相关文章推荐
- 二叉树非递归遍历算法Binary Tree Traverse methods
- Binary Tree Postorder Traversal 二叉树的后序遍历,使用堆栈,非递归
- 二叉树前/中/后序遍历非递归实现(用栈实现)LeetCode Binary Tree Traversal
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归
- C++实现二叉树 前序遍历, 后序遍历, 中序遍历, 层序遍历(不用递归)
- [leetcode]Minimum Depth of Binary Tree--二叉树层序遍历的应用
- Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- [转载]Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
- LeetCode OJ 之 Binary Tree Level Order Traversal II (二叉树的层次遍历-二)
- 5.1.7—二叉树的遍历—Recover Binary Sear Tree
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- 5.4.1—二叉树的递归—Minimum Depth of Binary Tree
- 不用递归和辅助空间对二叉树进行遍历
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- 二叉树前序遍历Binary Tree Preorder Traversal (Java)