前序遍历:
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树


中序遍历:
1.中序遍历左子树
2.访问根节点
3.中序遍历右子树


后序遍历:
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点
---------------------

package design;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack; public class BinTree {
char data;
BinTree leftChild;
BinTree rightChild;
public BinTree(char c) {
data = c;
}
public static void preSearch(BinTree root){
if(root !=null){
System.out.print(root.data);
preSearch(root.leftChild);
preSearch(root.rightChild);
}
}
public static void midSearch(BinTree root){
if(root !=null){
midSearch(root.leftChild);
System.out.print(root.data);
midSearch(root.rightChild);
}else{
return;
}
}
public static void postSearch(BinTree root){
if(root !=null){
postSearch(root.leftChild);
postSearch(root.rightChild);
System.out.print(root.data);
}
}
// 先序遍历非递归
public static void preOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root !=null || !s.empty()){
while(root!=null){
System.out.print(root.data);
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root = s.pop();
root = root.rightChild;
}
}
}
// 中序遍历非递归
public static void midOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root =s.pop();
System.out.print(root.data);
root = root.rightChild;
}
}
}
// 后序遍历非递归
public static void postOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
Stack<Integer> s2 = new Stack<Integer>();
Integer i = new Integer();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
s2.push(new Integer());
root = root.leftChild;
}
while(!s.empty() && s2.peek().equals(i)){
s2.pop();
System.out.print(s.pop().data);
}
if(!s.empty()){
s2.pop();
s2.push(new Integer());
root =s.peek();
root = root.rightChild;
}
}
}
//计算二叉树的深度
public static int level(BinTree root){
if(root == null){
return ;
}
return level(root.leftChild)+>level(root.rightChild)+?level(root.leftChild)+:level(root.rightChild)+; }
//层序遍历二叉树
public static void levelTrav(BinTree root) {
if (root == null)
return;
Queue<BinTree> q = new ArrayDeque<BinTree>();
q.add(root);
BinTree cur;
while (!q.isEmpty()) {
cur = q.peek();
System.out.print(cur.data + " ");
if (cur.leftChild != null)
q.add(cur.leftChild);
if (cur.rightChild != null)
q.add(cur.rightChild);
q.poll();
}
}
public static void main(String[] args) {
BinTree b1 = new BinTree('a');
BinTree b2 = new BinTree('b');
BinTree b3 = new BinTree('c');
BinTree b4 = new BinTree('d');
BinTree b5 = new BinTree('e'); /**
* a
* / \
* b c
* / \
* d e
*/
b1.leftChild = b2;
b1.rightChild = b3;
b2.leftChild = b4;
b2.rightChild = b5; BinTree.preSearch(b1);
System.out.println();
BinTree.preOrder(b1);
System.out.println("========================");
BinTree.midSearch(b1);
System.out.println("");
BinTree.midOrder(b1);
System.out.println("========================");
BinTree.postSearch(b1);
System.out.println();
BinTree.postOrder(b1);
System.out.println("========================");
System.out.println(BinTree.level(b1));
System.out.println("========================");
BinTree.levelTrav(b1);
}
}

最新文章

  1. 常见博客API
  2. 不使用border-radius,实现一个可复用的高度和宽度都自适应的圆角矩形
  3. 20151225jquery学习笔记---选项卡UI
  4. Asp.Net MVC Ajax
  5. JVM基础和调优(六)
  6. 开发该选择Blocks还是Delegates(转)
  7. crm管理系统
  8. 安装CentOS7精简版后的配置工作
  9. UGUI背包系统
  10. 第2章 Java基本语法(上): 变量与运算符
  11. 自动化测试中CSS SELECTOR选择器的一些写法
  12. 【转】全Javascript的Web开发架构:MEAN和Yeoman【译】
  13. 转载 vsftpd安装
  14. Android studio jni
  15. js delete删除对象属性,delete删除不了变量及原型链中的变量
  16. ab网站压力测试命令的参数、输出结果的中文注解
  17. vue.js指令v-model实现方法
  18. 在where子句中经常使用的运算符
  19. html-文本处理
  20. base64文件上传的问题

热门文章

  1. subprocess.Popen stdout重定向内容实时获取
  2. go语言学习资料
  3. numpy(一)
  4. ZJNU 1067 - 约瑟夫——中级
  5. console.log和alert的区别
  6. uboot对Flash和DDR的分区管理
  7. windows下使用vs code调试简单的C程序
  8. Flink(五) —— DataStream API
  9. TimeHelper
  10. A. Coffee Break(思维题,类似于邻接表的head数组用法)