add tree code
parent
d785348b1c
commit
1d6441d4b0
|
@ -1,2 +1,2 @@
|
|||
.idea
|
||||
*.iml
|
||||
*.iml
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>bit.edu.bitcarmanlee</groupId>
|
||||
<groupId>edu.bit.bitcarmanlee</groupId>
|
||||
<artifactId>easy-algorithm-interview</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package edu.bit.carmanlee.datastruct.tree;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Traverse {
|
||||
|
||||
public static void preorder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
System.out.print(root.data + " ");
|
||||
preorder(root.left);
|
||||
preorder(root.right);
|
||||
}
|
||||
|
||||
public static void preordernorecursive(TreeNode root) {
|
||||
if (root == null) return;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode tmp = stack.pop();
|
||||
System.out.print(tmp.data + " ");
|
||||
if (tmp.right != null) stack.push(tmp.right);
|
||||
if (tmp.left != null) stack.push(tmp.left);
|
||||
}
|
||||
}
|
||||
|
||||
public static void inorder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
inorder(root.left);
|
||||
System.out.print(root.data + " ");
|
||||
inorder(root.right);
|
||||
}
|
||||
|
||||
public static void inorernorecursive(TreeNode root) {
|
||||
if (root == null) return;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
while (!stack.isEmpty() || root != null) {
|
||||
while (root != null) {
|
||||
stack.push(root);
|
||||
root = root.left;
|
||||
}
|
||||
if (!stack.isEmpty()) {
|
||||
root = stack.pop();
|
||||
System.out.print(root.data + " ");
|
||||
root = root.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void postorder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
postorder(root.left);
|
||||
postorder(root.right);
|
||||
System.out.print(root.data + " ");
|
||||
}
|
||||
|
||||
public static void postordernorecursive(TreeNode root) {
|
||||
if (root == null) return;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode pre = root, cur = root;
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
|
||||
while (! stack.isEmpty()) {
|
||||
cur = stack.pop();
|
||||
if (cur.right != null && cur.right != pre) {
|
||||
stack.push(cur);
|
||||
cur = cur.right;
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
} else {
|
||||
System.out.print(cur.data + " ");
|
||||
pre = cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode root = TreeNodeInit.init();
|
||||
preorder(root);
|
||||
System.out.println();
|
||||
preordernorecursive(root);
|
||||
System.out.println();
|
||||
inorder(root);
|
||||
System.out.println();
|
||||
inorernorecursive(root);
|
||||
System.out.println();
|
||||
postorder(root);
|
||||
System.out.println();
|
||||
postordernorecursive(root);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package edu.bit.carmanlee.datastruct.tree;
|
||||
|
||||
public class TreeNode {
|
||||
|
||||
int data;
|
||||
TreeNode left, right;
|
||||
|
||||
public TreeNode(int data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package edu.bit.carmanlee.datastruct.tree;
|
||||
|
||||
public class TreeNodeInit {
|
||||
|
||||
|
||||
/*
|
||||
1
|
||||
2 3
|
||||
4 5 6 7
|
||||
8
|
||||
*/
|
||||
public static TreeNode init() {
|
||||
TreeNode t1 = new TreeNode(1);
|
||||
TreeNode t2 = new TreeNode(2);
|
||||
TreeNode t3 = new TreeNode(3);
|
||||
TreeNode t4 = new TreeNode(4);
|
||||
TreeNode t5 = new TreeNode(5);
|
||||
TreeNode t6 = new TreeNode(6);
|
||||
TreeNode t7 = new TreeNode(7);
|
||||
TreeNode t8 = new TreeNode(8);
|
||||
t1.left = t2;
|
||||
t1.right = t3;
|
||||
t2.left = t4;
|
||||
t2.right = t5;
|
||||
t3.left = t6;
|
||||
t3.right = t7;
|
||||
t4.right = t8;
|
||||
return t1;
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue