-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinaryTree.java
More file actions
148 lines (123 loc) · 3.81 KB
/
BinaryTree.java
File metadata and controls
148 lines (123 loc) · 3.81 KB
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
139
140
141
142
143
144
145
146
147
148
package dataStructures;
public class BinaryTree {
// In-order traversal
public static void traverse(Node root) {
if(root != null) {
traverse(root.left);
System.out.print(root.value + " ");
traverse(root.right);
}
}
public static int getHeight(Node root) {
if(root == null)
return 0;
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
public static int getDepth(Node root, int val, int level) {
if(root == null)
return 0;
if(root.value == val) {
return level;
}
// searching left subtree
int retLevel = getDepth(root.left, val, level++);
if(retLevel != 0)
return retLevel;
retLevel = getDepth(root.right, val, level++);
return retLevel;
}
public static boolean isPerfect(Node root) {
if(root == null)
return true;
if( (root.left != null && root.right == null) || (root.left == null && root.right != null))
return false;
return true && isPerfect(root.left) && isPerfect(root.right);
}
public static void main(String[] args) {
Node root = new Node(5, new Node(3), new Node(6));
// testing in-order traversal
traverse(root);
System.out.println("\n");
// testing height of tree
root = new Node(6, new Node(4, new Node(3), new Node(5)), new Node(10, null, new Node(12, new Node(11), null)));
System.out.println("Height of tree: " + getHeight(root));
System.out.println();
// testing depth for Node 5
System.out.println("Depth of node 5: " + getDepth(null, 5, 1));
System.out.println();
//testing if tree is perfect
System.out.println("Is tree perfect? " + isPerfect(root));
System.out.println();
// testing BST
BinarySearchTree bst = new BinarySearchTree();
bst.insert(bst.getRoot(), new Node(5));
bst.insert(bst.getRoot(), new Node(4));
bst.insert(bst.getRoot(), new Node(6));
bst.insert(bst.getRoot(), new Node(1));
bst.insert(bst.getRoot(), new Node(2));
bst.insert(bst.getRoot(), new Node(3));
bst.insert(bst.getRoot(), new Node(0));
root = bst.getRoot();
System.out.print("BST: ");
traverse(root);
System.out.println("\n");
System.out.print("After deletion of 1, BST: ");
bst.delete(1);
traverse(root);
System.out.println("\n");
Node search = bst.getNode(5);
System.out.println("Node 5 has address " + (search == null ? "NULL" : search)
+ " and value: " + (search == null ? "NULL" : search.value));
search = bst.getNode(15);
System.out.println("Node 15 has address " + (search == null ? "NULL" : search)
+ " and value: " + (search == null ? "NULL" : search.value) + "\n");
AVLTree avl = new AVLTree();
avl.insert(10);
avl.insert(5);
avl.insert(15);
avl.insert(2);
avl.insert(4);
System.out.print("AVL: ");
traverse(avl.getRoot());
System.out.println("\nRoot of AVL Tree: " + avl.getRoot().value);
System.out.println("\n");
avl.delete(15);
System.out.print("AVL after deletion of 15: ");
traverse(avl.getRoot());
System.out.println("\nRoot of AVL Tree: " + avl.getRoot().value);
System.out.println("\n");
avl = new AVLTree();
avl.insert(6);
avl.insert(3);
avl.insert(7);
avl.insert(1);
System.out.print("AVL: ");
traverse(avl.getRoot());
System.out.println("\nRoot of AVL Tree: " + avl.getRoot().value);
System.out.println("\n");
avl.delete(7);
System.out.print("AVL after deletion of 7: ");
traverse(avl.getRoot());
System.out.println("\nRoot of AVL Tree: " + avl.getRoot().value);
System.out.println("\n");
}
public static class Node {
public int value;
public Node left;
public Node right;
public int height;
public int size;
public Node(int value) {
this(value, null, null);
}
public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
this.height = 1;
}
public String toString() {
return "" + this.value;
}
}
}