-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTree.java
More file actions
59 lines (49 loc) · 946 Bytes
/
Copy pathBinaryTree.java
File metadata and controls
59 lines (49 loc) · 946 Bytes
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
public class BinaryTree{
public static void main(String[] args){
new BinaryTree().run();
}
static class Node{
Node left;
Node right;
int data;
public Node(int data){
this.data = data;
}
}
public void run(){
Node root = new Node(5);
System.out.println(root.data + " ");
insert(root, 1);
insert(root, 8);
insert(root, 6);
insert(root, 3);
insert(root, 9);
System.out.println("Traversing Tree InOrder");
inOrder(root);
}
public void insert(Node node, int data){
if(data < node.data){
if(node.left != null){
insert(node.left, data);
}
else{
node.left = new Node(data);
}
}
else if(data > node.data){
if(node.right != null){
insert(node.right, data);
}
else{
node.right = new Node(data);
}
}
}
public void inOrder(Node node){
if(node != null){
inOrder(node.left);
System.out.println(node.data);
inOrder(node.right);
}
}
}