Skip to content

Commit a00fcc8

Browse files
committed
binary tree creation and basic traversal
1 parent aa7b5f5 commit a00fcc8

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

tree/BinaryTree/CreateBT.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tree.BinaryTree;
2+
3+
public class CreateBT {
4+
Node root;
5+
6+
int index = -1;
7+
8+
// create Binary treee from array
9+
// return root of the tree
10+
public Node createTreeFromArr(int[] nodes) {
11+
index++;
12+
if (nodes[index] == -1) {
13+
return null;
14+
}
15+
16+
Node newNode = new Node(nodes[index]);
17+
newNode.left = createTreeFromArr(nodes);
18+
newNode.right = createTreeFromArr(nodes);
19+
20+
return newNode;
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] nodes = { 9, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1 };
25+
26+
CreateBT tree = new CreateBT();
27+
Node root = tree.createTreeFromArr(nodes);
28+
29+
System.out.println(root.data);
30+
}
31+
}

tree/BinaryTree/Node.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tree.BinaryTree;
2+
3+
public class Node {
4+
int data;
5+
Node left;
6+
Node right;
7+
8+
Node(int data) {
9+
this.data = data;
10+
this.left = null;
11+
this.right = null;
12+
}
13+
}

tree/BinaryTree/Traversal.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package tree.BinaryTree;
2+
3+
public class Traversal {
4+
public static void preOrderTravers(Node root) {
5+
if (root == null) {
6+
System.out.print(-1 + " ");
7+
return;
8+
}
9+
10+
System.out.print(root.data + " ");
11+
preOrderTravers(root.left);
12+
preOrderTravers(root.right);
13+
}
14+
15+
public static void inOrderTravers(Node root) {
16+
if (root == null) {
17+
System.out.print(-1 + " ");
18+
return;
19+
}
20+
21+
inOrderTravers(root.left);
22+
System.out.print(root.data + " ");
23+
inOrderTravers(root.right);
24+
}
25+
26+
public static void postOrderTravers(Node root) {
27+
if (root == null) {
28+
System.out.print(-1 + " ");
29+
return;
30+
}
31+
32+
postOrderTravers(root.left);
33+
postOrderTravers(root.right);
34+
System.out.print(root.data + " ");
35+
}
36+
37+
public static void main(String[] args) {
38+
int[] nodes = { 9, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1 };
39+
40+
CreateBT tree = new CreateBT();
41+
Node root = tree.createTreeFromArr(nodes);
42+
43+
preOrderTravers(root);
44+
System.out.println();
45+
inOrderTravers(root);
46+
System.out.println();
47+
postOrderTravers(root);
48+
}
49+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package tree.collectionFramework;
2-
32
import java.util.TreeSet;
4-
53
public class TreeS {
64
public static void main(String[] args) {
75
TreeSet<String> set = new TreeSet<String>();
@@ -11,7 +9,7 @@ public static void main(String[] args) {
119
set.add("c");
1210
set.add("d");
1311
set.add("e");
14-
12+
1513
System.out.println(set);
1614
}
1715
}

0 commit comments

Comments
 (0)