数据结构--树的三种遍历(递归与非递归实现)
来源:互联网 发布:mill9.1编程教程清角 编辑:程序博客网 时间:2024/06/05 00:55
转自:http://blog.csdn.net/speedme/article/details/21659899
1:深度优先
1.1:前序遍历
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
伪代码:
preorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right)
iterativePreorder(node) parentStack = empty stack parentStack.push(null) top = node while ( top != null ) visit( top ) if ( top.right != null ) parentStack.push(top.right) if ( top.left != null ) parentStack.push(top.left) top = parentStack.top(); parentStack.pop();
1.2:中序遍历
- Traverse the left subtree.
- Visit root.
- Traverse the right subtree.
1.3:后序遍历
1.4:java代码实现三种遍历
树的结构是
10
/ \
5 12
/ \
4 7
/ \
5 12
/ \
4 7
代码中我主要解释下让我一直纠结的递归法了,总是对递归法执行的顺序有疑惑。详细可以看《Thinking In Algorithm》09.彻底理解递归
- public class Preorder{
- /*******************************递归法****************************/
- //递归前序遍历 print:10,5,4,7,12
- public static void preorder(TNode node){
- if(node!=null){
- System.out.println(node.getValue()) ;
- preorder(node.getLeft()) ;
- preorder(node.getRight()) ;
- }
- }
- //递归中序遍历 print: 4,5,7,10,12
- public static void inorder(TNode node){
- if(node!=null){
- inorder(node.getLeft()) ;
- System.out.println(node.getValue()) ;
- inorder(node.getRight()) ;
- }
- }
- //递归后序遍历 print: 4,7,5,12,10
- public static void postorder(TNode node){
- if(node!=null){
- postorder(node.getLeft()) ;
- postorder(node.getRight()) ;
- System.out.println(node.getValue()) ;
- }
- }
- /*******************************非递归*****************************/
- //非递归前序遍历 print:10,5,4,7,12
- public static void iterativePreorder(TNode node){
- MyStack parentStack = new MyStack() ;
- parentStack.push(node) ;
- while(!parentStack.isEmpty()){
- TNode temp = parentStack.getFirst().getValue() ;
- System.out.println(temp.getValue()) ;
- parentStack.pop() ;
- if(temp.getRight()!=null){ parentStack.push(temp.getRight()) ;}
- if(temp.getLeft()!=null){ parentStack.push(temp.getLeft()) ;}
- }
- }
- //非递归中序遍历 print: 4,5,7,10,12
- public static void iterativeInorder(TNode node){
- MyStack mystack = new MyStack() ;
- while(!mystack.isEmpty()||node!=null){
- if(node!=null){
- mystack.push(node) ;
- node = node.getLeft() ;
- }else{
- node = mystack.pop().getValue() ;
- System.out.println(node.getValue()) ;
- node = node.getRight() ;
- }
- }
- }
- //非递归后序遍历 print: 4,7,5,12,10
- public static void iterativePostorder(TNode node){
- MyStack mystack = new MyStack() ;
- TNode temp = null ;
- TNode peeknode = null ;
- while(!mystack.isEmpty()||node!=null){
- if(node!=null){
- mystack.push(node) ;
- node = node.getLeft() ;
- }else{
- peeknode = mystack.getFirst().getValue() ;
- if(peeknode.getRight()!=null&&temp!=peeknode.getRight()){
- node = peeknode.getRight() ;
- }else{
- mystack.pop() ;
- System.out.println(peeknode.getValue()) ;
- temp = peeknode ;
- }
- }
- }
- }
- public static void main(String args[]){
- MyTree mytree = new MyTree() ;
- mytree.insert(10) ;
- mytree.insert(5);
- mytree.insert(12);
- mytree.insert(4);
- mytree.insert(7);
- preorder(mytree.getRoot()) ;
- inorder(mytree.getRoot()) ;
- postorder(mytree.getRoot()) ;
- iterativePreorder(mytree.getRoot()) ;
- iterativeInorder(mytree.getRoot()) ;
- iterativePostorder(mytree.getRoot()) ;
- }
- }
- /*****************二叉树的实现****************/
- class TNode{
- private int value ;
- private TNode left ;
- private TNode right ;
- public TNode(int value){ this.value = value ;}
- public void setLeft(int value){ this.left = new TNode(value) ;}
- public void setRight(int value){ this.right = new TNode(value) ;}
- public TNode getLeft(){ return this.left ;}
- public TNode getRight(){ return this.right ;}
- public int getValue(){ return this.value ;}
- }
- class MyTree{
- private TNode root ;
- public void setRoot(int value){ this.root = new TNode(value) ;}
- public TNode getRoot(){ return this.root ;}
- public void insert(int value){
- TNode x = this.root ;
- TNode y = null ;
- while(x!=null){
- y = x ;
- if(value<x.getValue()){ x = x.getLeft() ;}
- else{ x = x.getRight() ;}
- }
- if(y == null){ setRoot(value) ;}
- else if(value<y.getValue())
- { y.setLeft(value) ;}
- else{ y.setRight(value) ;}
- }
- }
- /*****************栈的实现****************/
- class StackNode{
- public TNode item ;
- public StackNode next ;
- public StackNode(){ ;}
- public StackNode(TNode item){ this.item = item ;}
- public TNode getValue(){ return item ;}
- }
- class MyStack{
- int N ; //size of myStack
- StackNode first ; // the top of stack
- //size of mystack
- public int size(){ return N ;}
- //empty or not
- public boolean isEmpty(){ return N==0 ;}
- public StackNode getFirst(){ return this.first ;}
- public void push(TNode item){
- if(isEmpty()){
- first = new StackNode() ;
- first.item = item;
- N++;
- }else{
- StackNode oldfirst = first;
- first = new StackNode() ;
- first.item = item;
- first.next = oldfirst;
- N++;
- }
- }
- public StackNode pop(){
- StackNode top = first ;
- first = first.next; // delete first node
- N--;
- return top ;
- }
- }
2.广度遍历
广度遍历比较简单,就是层次遍历
伪代码
levelorder(root) q = empty queue q.enqueue(root) while not q.empty do node := q.dequeue() visit(node) if node.left ≠ null then q.enqueue(node.left) if node.right ≠ null then q.enqueue(node.right)
0 0
- 数据结构--树的三种遍历(递归与非递归实现)
- 《Thinking In Algorithm》10.树的三种遍历(递归与非递归实现)
- 树的三种遍历(递归与非递归实现)
- 二叉树的三种遍历方式(递归与非递归详细实现)
- 【数据结构】二叉树的遍历(递归与非递归)
- 数据结构BinaryTree实例(三):二叉树的后序遍历(递归与非递归)
- 二叉树的三种递归遍历与非递归遍历-C++实现
- 数据结构基础5.1:树的遍历(递归与非递归实现)
- 【数据结构】二叉树的三种遍历--(递归+非递归)
- 【数据结构与算法】二叉树的遍历(递归遍历、非递归遍历、层序遍历)
- 递归与非递归实现树的遍历(java)
- 二叉树的三种遍历方式的递归与非递归实现
- 二叉树的三种遍历(递归+非递归)
- 树的三种遍历(递归和非递归)
- 数据结构与算法学习记录--二叉树的创建,递归遍历,非递归遍历的实现
- [数据结构] 二叉树的递归与非递归遍历
- C语言数据结构实现二叉树递归与非递归遍历(数据结构第四次实验)
- 二叉树的三种遍历的非递归实现
- jquery在form表单取值的简单应用
- 【OpenCV入门教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
- Android 软键盘盖住输入框的问题
- android加载网络图片并保留缓存,随时点击打开
- 【facebook】程序猿提高工作效率的15点技巧
- 数据结构--树的三种遍历(递归与非递归实现)
- 【OpenCV入门教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑
- 对于locationprovider中的onLocationChanged不执行的分析
- Makefile文件生成 GNU Autotools的使用方法
- 在Mac系统上安装Tomcat
- SwipeRefreshLayout组件 实现ListView下拉刷新
- 年轻?大有所为!——问题集一网页中文乱码
- 出来创业,不知道有多难,在这和大家说一下吧
- WPF 由于其他线程拥有此对象,因此调用线程无法对其进行访问