线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
来源:互联网 发布:淘宝卖家怎么申请直播 编辑:程序博客网 时间:2024/05/18 12:38
线索二叉树 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;}
- 线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
- 数据结构:线索二叉树(Threaded Binary Tree)
- 线索二叉树(Threaded Binary Tree)
- 二叉线索树(Threaded binary tree)
- 线索二叉树(threaded binary search tree) C#
- 数据结构《9》----Threaded Binary Tree 线索二叉树
- 03. 线索二叉树(Threaded Binary Tree)
- Morris Traversal (线索二叉树 Threaded binary tree)
- 不用栈和递归,实现线索化二叉树的遍历(还没人看的话就沉了吧)
- 线索二叉树(Threaded BinaryTree)
- 不用栈实现二叉树非递归中序遍历
- 不用栈实现二叉树非递归中序遍历
- 二叉搜索树(遍历不递归,不用栈)
- parent指针非递归不用栈中序遍历二叉树
- 不用递归和辅助空间对二叉树进行遍历
- 线索二叉树(Threaded BinaryTree)
- 线索二叉树(Thread-Binary-Tree)
- 线索二叉树(Binary Thread Tree)
- ASCII码文件和二进制码文件
- Java栈与堆
- weblogic与apache的整合与调优 --架构师第八天
- jQuery网格插件 ParamQuery
- 一次谷歌面试趣事
- 线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
- PAT 1020. Tree Traversals (25) 根据树的中序与后序,求层序
- 跟着老毕学Java-集合框架篇之Collection
- 快速排序实践
- datacolumn如何保留两位小数
- javascript基础二JavaScript DOM编程
- 【黑马程序员】- StringBulider详解
- 解决乱码问题 帖子看到的 留存一下
- 二叉搜索树