研究了一上午被进栈出栈搞晕了 终于弄出来了 直接上代码

package com.wyy.test;import java.util.Stack;public class TransverseTreeWithoutDGTest {public static void main(String[] args){Tree tree = new Tree();tree.insert("A", 30);tree.insert("B", 42);tree.insert("C", 19);tree.insert("D", 22);tree.insert("E", 33);tree.insert("F", 45);tree.insert("G", 20);tree.preOrder(tree.root);System.out.println();tree.midOrder(tree.root);System.out.println();tree.lastOrder(tree.root);System.out.println();System.out.println("----------------------------");Tree.postOrderWithStack(tree.root);}
}
class Node{public String name;public int count;public Node leftChild;public Node rightChild;public Node(String name,int count){this.name = name;this.count = count;}
}
class Tree{public Node root;public Tree(){root = null;}public void insert(String name,int count){Node newNode = new Node(name,count);if(root==null)root = newNode;else{Node current = root;Node parent;while(true){parent = current;if(count<current.count){current = current.leftChild;if(current==null){parent.leftChild = newNode;return;}}else if(count>current.count){current = current.rightChild;if(current==null){parent.rightChild = newNode;return;}}}}}public Node find(int key){Node current = root;while(true){if(current==null)return null;if(key==current.count)return current;else if(key<current.count){current = current.leftChild;}else if(key>current.count){current = current.rightChild;}}}public void preOrder(Node node){if(node!=null){System.out.print(node.name);preOrder(node.leftChild);preOrder(node.rightChild);}}public void midOrder(Node node){if(node!=null){midOrder(node.leftChild);System.out.print(node.name);midOrder(node.rightChild);}}public void lastOrder(Node node){if(node!=null){lastOrder(node.leftChild);lastOrder(node.rightChild);System.out.print(node.name);}}public void preOrderWithStack(Node node){Stack<Node> s = new Stack<Node>();Node current = node;while(current!=null||!s.isEmpty()){while(current!=null){System.out.print(current.name+" ");if(current.rightChild!=null){s.push(current.rightChild);}current = current.leftChild;}if(!s.isEmpty()){current = s.pop();}}}public void inOrderWithStack(Node node){Node current = node;Stack<Node> s = new Stack<Node>();while(current!=null||!s.isEmpty()){while(current!=null){s.push(current);current = current.leftChild;}if(!s.isEmpty()){current = s.pop();System.out.print(current.name+" ");current = current.rightChild;}}}public static void postOrderWithStack(Node node){Node current;Node pre = null;Stack<Node> s= new Stack<Node>();s.push(node);while(!s.isEmpty()){current = s.peek();if((current.leftChild==null&&current.rightChild==null)||(pre!=null&&(pre==current.leftChild||pre==current.rightChild))){System.out.print(s.pop().name);//如果节点的左右子节点都为null 或者都被遍历过则可以访问pre = current;}else{if(current.rightChild!=null)s.push(current.rightChild);if(current.leftChild!=null)s.push(current.leftChild);}}}}

ok 大功告成

转载于:https://www.cnblogs.com/yaoboyyao/p/3603366.html

java实现二叉树的非递归遍历相关推荐

  1. 二叉树的非递归遍历(java版)

    二叉树的递归遍历比较简单,这里就不聊了.今天主要聊聊二叉树的非递归遍历,主要借助于"栈"后进先出的特性来保存节点的顺序,先序遍历和中序遍历相对来说比较简单,重点理解后序遍历. 1. ...

  2. 刷题:二叉树的非递归遍历方式

    二叉树的非递归的遍历方式 上篇博客记录了二叉树的递归遍历方式以及根据二叉树的遍历结果还原二叉树的内容. 本篇博客记录二叉树的非递归的遍历方式. 二叉树的非递归遍历需要借助栈来实现,而且三种遍历的方式的 ...

  3. 二叉树的非递归遍历(c/c++)

    由于递归算法相对于非递归算法来说效率通常都会更低,递归算法会有更多的资源需要压栈和出栈操作(不仅仅是参数,还有函数地址等)由于编译器对附加的一些栈保护机制会导致递归执行的更加低效,使用循环代替递归算法 ...

  4. 二叉树的非递归遍历(统一的模板)

    二叉树的非递归遍历 前言 树的存储结构 先序遍历 先序的递归遍历 先序的非递归遍历 中序遍历 中序的递归遍历 中序遍历的非递归算法 后序遍历 后序的递归遍历 后序的非递归遍历 层次遍历 层次遍历获得每 ...

  5. 数据结构-二叉树的非递归遍历

    前面的章节我们实现了二叉树最基本的遍历方式:递归遍历,代码是如此的简洁:辣么我们为什么还要去学习二叉树的非递归遍历方式呢?众所周知,递归优点是将可以将复杂的问题简单化即大问题拆分成一个个小问题,那么它 ...

  6. c语言以顺序结构存储的二叉树的非递归遍历,C语言二叉树的非递归遍历实例分析...

    本文以实例形式讲述了C语言实现二叉树的非递归遍历方法.是数据结构与算法设计中常用的技巧.分享给大家供大家参考.具体方法如下: 先序遍历: void preOrder(Node *p) //非递归 { ...

  7. 树:二叉树的非递归遍历算法

    二叉树的递归遍历 二叉树的递归遍历算法,写法很简单,比如说前序遍历树,如下: //前序遍历 void PreOrderTraverse(BiTree tree) {if (NULL != tree){ ...

  8. 6-9 二叉树的非递归遍历 (20 分)

    ** 6-9 二叉树的非递归遍历 (20 分) ** 本题要求用非递归的方法实现对给定二叉树的 3 种遍历. 函数接口定义: void InorderTraversal( BinTree BT ); ...

  9. C/C++ 二叉树的非递归遍历(前序、中序、后序非递归遍历)

     二叉树的非递归遍历C/C++实现:   非递归先序遍历代码: void PreOrderTraversal (struct tree* root) { //非递归先序遍历struct tree* t ...

最新文章

  1. bootstrap bssuggest
  2. 阿里云安全运营中心:DDoS攻击趁虚而入,通过代理攻击已成常态
  3. 数据库系统的体系结构知识笔记
  4. 【微机原理与接口技术】多功能可编程芯片 与 多功能电饭煲
  5. SVG 教程 (五)文本,Stroke 属性,SVG 滤镜,SVG 模糊效果
  6. Onedark风格配色方案
  7. 根号x_【深情攻X自卑受】糖与盐by根号三
  8. Linux音频驱动-AOSC之Codec
  9. Linux设备驱动模型-Bus
  10. 7款流程图制作软件大盘点!轻松绘制流程图
  11. Python实现求矩阵的伴随矩阵
  12. 爱的能力(徐博客写的-收藏下)
  13. win8服务器备份在哪个文件夹,iPhone备份文件在哪里?Win8系统路径介绍
  14. 微信公众号获取AppID和AppSecret
  15. c语言side输出空心正方形,请帮忙完成这个c#语言打印正方形的程序
  16. 14款S400升级20款S450外观套件
  17. 【GITEE】解决 Push rejected
  18. 计算机教师格言座右铭,教师个人格言座右铭集锦
  19. 令人炸毛儿的MySQL隐式转换 - 无形之刃,最为致命
  20. 银河麒麟V10操作命令

热门文章

  1. 雷林鹏分享:jQuery EasyUI 数据网格 - 创建页脚摘要
  2. swust oj 962
  3. Unknown lifecycle phase mvn
  4. 多线程端点服务发布程序(摘)
  5. jquerynbsp;easyuinbsp;dateboxnbsp;的使用nbsp;.
  6. Groovy正则表达式复杂逻辑判断实例
  7. java concurrent包的学习(转)
  8. (转译)用FFmpeg和SDL写播放器--01视频帧提取
  9. c#_MessageBox 消息对话框
  10. python多久能上手_小白学习Python,怎样能够快速入门上手