@@ -11,33 +11,33 @@ private void printNode(Node node) {
1111 }
1212
1313 /**
14- * 先序遍历---递归
14+ * 先序遍历---递归
1515 */
1616 public void theFirstTraversal (Node root ) {
17- printNode (root ); // 根
18- if (root .getLeftNode () != null ) { // 左
17+ printNode (root ); // 根
18+ if (root .getLeftNode () != null ) { // 左
1919 theFirstTraversal (root .getLeftNode ());
2020 }
21- if (root .getRightNode () != null ) { // 右
21+ if (root .getRightNode () != null ) { // 右
2222 theFirstTraversal (root .getRightNode ());
2323 }
2424 }
2525
2626 /**
27- * 中序遍历---递归
27+ * 中序遍历---递归
2828 */
2929 public void theInOrderTraversal (Node root ) {
30- if (root .getLeftNode () != null ) { // 左
30+ if (root .getLeftNode () != null ) { // 左
3131 theInOrderTraversal (root .getLeftNode ());
3232 }
33- printNode (root ); // 中
33+ printNode (root ); // 中
3434 if (root .getRightNode () != null ) {
35- theInOrderTraversal (root .getRightNode ());// 右
35+ theInOrderTraversal (root .getRightNode ());// 右
3636 }
3737 }
3838
3939 /**
40- * 后序遍历---递归
40+ * 后序遍历---递归
4141 */
4242 public void thePostOrderTraversal (Node root ) {
4343 if (root .getLeftNode () != null ) {
@@ -49,45 +49,45 @@ public void thePostOrderTraversal(Node root) {
4949 printNode (root );
5050 }
5151 /**
52- * 前序遍历---非递归算法
52+ * 前序遍历---非递归算法
5353 */
5454 public void theFirstTraversal_Stack (Node root ) {
55- Stack <Node > stack = new Stack <Node >();//初始化栈
56- Node node = root ; //遍历过程中移动此节点
57- while (node != null || stack .size () > 0 ) { // 将所有左孩子压栈
58- if (node != null ) { //节点不为空,
59- printNode (node );// 输出该节点
60- stack .push (node );//节点入栈
61- node = node .getLeftNode ();//指向左孩子
62- } else { //节点为空
63- node = stack .pop ();//出栈返回,去遍历右孩子
55+ Stack <Node > stack = new Stack <Node >();//初始化栈
56+ Node node = root ; //遍历过程中移动此节点
57+ while (node != null || stack .size () > 0 ) { // 将所有左孩子压栈
58+ if (node != null ) { //节点不为空,
59+ printNode (node );// 输出该节点
60+ stack .push (node );//节点入栈
61+ node = node .getLeftNode ();//指向左孩子
62+ } else { //节点为空
63+ node = stack .pop ();//出栈返回,去遍历右孩子
6464 node = node .getRightNode ();
6565 }
6666 }
6767 }
6868 /**
69- * 中序遍历---非递归算法
69+ * 中序遍历---非递归算法
7070 */
7171 public void theInOrderTraversal_Stack (Node root ) {
72- Stack <Node > stack = new Stack <Node >();//初始化栈
73- Node node = root ; //临时指针节点
74- while (node != null || stack .size () > 0 ) {//栈不空或者节点不空
75- if (node != null ) { //节点不空的情况
72+ Stack <Node > stack = new Stack <Node >();//初始化栈
73+ Node node = root ; //临时指针节点
74+ while (node != null || stack .size () > 0 ) {//栈不空或者节点不空
75+ if (node != null ) { //节点不空的情况
7676 stack .push (node );
7777 node = node .getLeftNode ();
78- } else { //栈不空的情况
78+ } else { //栈不空的情况
7979 node = stack .pop ();
8080 printNode (node );
8181 node = node .getRightNode ();
8282 }
8383 }
8484 }
8585 /**
86- * 后序遍历---非递归算法
86+ * 后序遍历---非递归算法
8787 */
8888 public void thePostOrderTraversal_Stack (Node root ) {
8989 Stack <Node > stack = new Stack <Node >();
90- Stack <Node > output = new Stack <Node >();// 构造一个中间栈来存储逆后序遍历的结果
90+ Stack <Node > output = new Stack <Node >();// 构造一个中间栈来存储逆后序遍历的结果
9191 Node node = root ;
9292 while (node != null || stack .size () > 0 ) {
9393 if (node != null ) {
@@ -106,27 +106,27 @@ public void thePostOrderTraversal_Stack(Node root) {
106106 }
107107
108108 /**
109- * 深度优先遍历二叉树
110- * @param root 根节点
109+ * 深度优先遍历二叉树
110+ * @param root 根节点
111111 */
112112 public void depthFirstSearch (Node root ){
113113 Stack <Node > stack = new Stack <Node >();
114114 stack .push (root );
115115 Node node = root ;
116116 while (!stack .empty ()){
117117 node = stack .pop ();
118- printNode (node ); //遍历根结点
118+ printNode (node ); //遍历根结点
119119 if (node .getRightNode () != null ){
120- stack .push (node .getRightNode ()); //先将右子树压栈
120+ stack .push (node .getRightNode ()); //先将右子树压栈
121121 }
122122 if (node .getLeftNode () != null ){
123- stack .push (node .getLeftNode ()); //再将左子树压栈
123+ stack .push (node .getLeftNode ()); //再将左子树压栈
124124 }
125125 }
126126 }
127127 /**
128- * 广度优先遍历二叉树
129- * @param node 根节点
128+ * 广度优先遍历二叉树
129+ * @param node 根节点
130130 */
131131 public void breadthFirst (Node node ) {
132132 Deque <Node > nodeDeque = new ArrayDeque ();
@@ -147,18 +147,18 @@ public void breadthFirst(Node node) {
147147 public static void main (String [] args ) {
148148 TraversalBinaryTree tree = new TraversalBinaryTree ();
149149 Node root = tree .init ();
150- System .out .println ("先序遍历" );
150+ System .out .println ("先序遍历" );
151151 tree .theFirstTraversal (root );
152152 System .out .println ("" );
153- System .out .println ("中序遍历" );
153+ System .out .println ("中序遍历" );
154154 tree .theInOrderTraversal (root );
155155 System .out .println ("" );
156- System .out .println ("后序遍历" );
156+ System .out .println ("后序遍历" );
157157 tree .thePostOrderTraversal (root );
158158 System .out .println ("" );
159159 }
160160
161- public Node init () {// 注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
161+ public Node init () {// 注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
162162 Node J = new Node (8 , null , null );
163163 Node H = new Node (4 , null , null );
164164 Node G = new Node (2 , null , null );
@@ -168,7 +168,7 @@ public static void main(String[] args) {
168168 Node C = new Node (9 , F , null );
169169 Node B = new Node (3 , D , E );
170170 Node A = new Node (6 , B , C );
171- return A ; // 返回根节点
171+ return A ; // 返回根节点
172172 }
173173
174174 public class Node {
0 commit comments