Java实现二叉树的遍历

简介:

    Java实现二叉树的遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
public  class  BinaryTreeNode {
     int  value;
     BinaryTreeNode left;
     BinaryTreeNode right;
 
     // 前序遍历
     public  static  void  preOrder(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         System.out.print(tree.value +  " " );
         preOrder(tree.left);
         preOrder(tree.right);
     }
 
     /**
      * 二叉树前序遍历的循环实现方式
     
      * @param tree
      */
     public  static  void  preOrderLoop(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         Stack<BinaryTreeNode> stack =  new  Stack<BinaryTreeNode>();
         BinaryTreeNode node = tree;
 
         while  (node !=  null  || !stack.isEmpty()) {
             if  (node !=  null ) {
                 stack.push(node);
                 System.out.print(node.value +  " " );
                 node = node.left;
             else  {
                 BinaryTreeNode item = stack.pop();
                 node = item.right;
             }
         }
     }
 
     // 中序遍历
     public  static  void  inOrder(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         inOrder(tree.left);
         System.out.print(tree.value +  " " );
         inOrder(tree.right);
     }
 
     /**
      * 二叉树中序遍历的循环实现
     
      * @param tree
      */
     public  static  void  inOrderLoop(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         Stack<BinaryTreeNode> stack =  new  Stack<BinaryTreeNode>();
         BinaryTreeNode node = tree;
 
         while  (node !=  null  || !stack.empty()) {
             if  (node !=  null ) {
                 stack.push(node);
                 node = node.left;
             else  {
                 BinaryTreeNode item = stack.pop();
                 System.out.print(item.value +  " " );
                 node = item.right;
             }
         }
     }
 
     // 后序遍历
     public  static  void  postOrder(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         postOrder(tree.left);
         postOrder(tree.right);
         System.out.print(tree.value +  " " );
     }
 
     /**
      * 二叉树后序遍历的循环实现
     
      * @param tree
      */
     public  static  void  postOrderLoop(BinaryTreeNode tree) {
         if  (tree ==  null )
             return ;
         HashSet<BinaryTreeNode> visited =  new  HashSet<BinaryTreeNode>();
         Stack<BinaryTreeNode> stack =  new  Stack<BinaryTreeNode>();
         BinaryTreeNode node = tree;
 
         while  (node !=  null  || !stack.isEmpty()) {
             if  (node !=  null ) {
                 stack.push(node);
                 node = node.left;
             else  {
                 BinaryTreeNode item = stack.peek();
                 if  (item.right !=  null  && !visited.contains(item.right))
                     node = item.right;
                 else  {
                     System.out.print(item.value +  " " );
                     visited.add(item);
                     stack.pop();
                 }
             }
         }
     }
 
     public  static  void  main(String[] args) {
         List<Integer> list1 =  new  ArrayList<Integer>();
         List<Integer> list2 =  new  ArrayList<Integer>();
 
         list1.add( 1 );
         list1.add( 2 );
         list1.add( 4 );
         list1.add( 7 );
         list1.add( 3 );
         list1.add( 5 );
         list1.add( 6 );
         list1.add( 8 );
 
         list2.add( 4 );
         list2.add( 7 );
         list2.add( 2 );
         list2.add( 1 );
         list2.add( 5 );
         list2.add( 3 );
         list2.add( 8 );
         list2.add( 6 );
 
         BinaryTreeNode tree = BinaryTreeNode.Construct(list1, list2);
         BinaryTreeNode.preOrderLoop(tree);
         System.out.println();
         BinaryTreeNode.inOrderLoop(tree);
         System.out.println();
         BinaryTreeNode.postOrderLoop(tree);
     }
 
}


本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1968112,如需转载请自行联系原作者
相关文章
|
5月前
|
存储 Java
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
|
2月前
|
存储 Java 开发者
在 Java 中,如何遍历一个 Set 集合?
【10月更文挑战第30天】开发者可以根据具体的需求和代码风格选择合适的遍历方式。增强for循环简洁直观,适用于大多数简单的遍历场景;迭代器则更加灵活,可在遍历过程中进行更多复杂的操作;而Lambda表达式和`forEach`方法则提供了一种更简洁的函数式编程风格的遍历方式。
|
3月前
|
Java 程序员 编译器
Java|如何正确地在遍历 List 时删除元素
从源码分析如何正确地在遍历 List 时删除元素。为什么有的写法会导致异常,而另一些不会。
77 3
|
3月前
|
前端开发 小程序 Java
java基础:map遍历使用;java使用 Patten 和Matches 进行正则匹配;后端传到前端展示图片三种情况,并保存到手机
这篇文章介绍了Java中Map的遍历方法、使用Pattern和matches进行正则表达式匹配,以及后端向前端传输图片并保存到手机的三种情况。
41 1
|
3月前
|
存储 算法 Java
Java一分钟之-数组的创建与遍历
数组作为Java中存储和操作一组相同类型数据的基本结构,其创建和遍历是编程基础中的基础。通过不同的创建方式,可以根据实际需求灵活地初始化数组。而选择合适的遍历方法,则可以提高代码的可读性和效率。掌握这些基本技能,对于深入学习Java乃至其他编程语言的数据结构和算法都是至关重要的。
35 6
|
3月前
|
Java
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
38 1
|
3月前
|
算法 Java C语言
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
34 1
|
2月前
|
算法 Java
JAVA 二叉树面试题
JAVA 二叉树面试题
29 0
|
4月前
|
域名解析 分布式计算 网络协议
java遍历hdfs路径信息,报错EOFException
java遍历hdfs路径信息,报错EOFException
50 3
|
5月前
|
Java 容器
07 Java数组与数组操作(定义+遍历+排序+增删改查)(上)
07 Java数组与数组操作(定义+遍历+排序+增删改查)
70 8