Skip to content

Commit d0308ce

Browse files
authored
Add Trees implementation.
1 parent d43499d commit d0308ce

File tree

6 files changed

+492
-0
lines changed

6 files changed

+492
-0
lines changed

Trees/AVL.java

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package Trees;
2+
3+
public class AVL {
4+
5+
class Node {
6+
int key, height;
7+
Node left, right;
8+
9+
Node(int key) {
10+
this.key = key;
11+
this.height = 1;
12+
}
13+
}
14+
15+
Node root;
16+
17+
// Get the height of a node
18+
int height(Node node) {
19+
if (node == null)
20+
return 0;
21+
return node.height;
22+
}
23+
24+
// Get the balance factor of a node
25+
int getBalance(Node node) {
26+
if (node == null)
27+
return 0;
28+
return height(node.left) - height(node.right);
29+
}
30+
31+
// Rotate right sub-tree rooted with y
32+
Node rightRotate(Node y) {
33+
Node x = y.left;
34+
Node T2 = x.right;
35+
36+
// Perform rotation
37+
x.right = y;
38+
y.left = T2;
39+
40+
// Update heights
41+
y.height = Math.max(height(y.left), height(y.right)) + 1;
42+
x.height = Math.max(height(x.left), height(x.right)) + 1;
43+
44+
// Return new root
45+
return x;
46+
}
47+
48+
// Rotate left sub-tree rooted with x
49+
Node leftRotate(Node x) {
50+
Node y = x.right;
51+
Node T2 = y.left;
52+
53+
// Perform rotation
54+
y.left = x;
55+
x.right = T2;
56+
57+
// Update heights
58+
x.height = Math.max(height(x.left), height(x.right)) + 1;
59+
y.height = Math.max(height(y.left), height(y.right)) + 1;
60+
61+
// Return new root
62+
return y;
63+
}
64+
65+
// Insert a key into the AVL tree
66+
Node insert(Node node, int key) {
67+
// Perform standard BST insertion
68+
if (node == null)
69+
return new Node(key);
70+
71+
if (key < node.key)
72+
node.left = insert(node.left, key);
73+
else if (key > node.key)
74+
node.right = insert(node.right, key);
75+
else // Duplicate keys not allowed
76+
return node;
77+
78+
// Update height of current node
79+
node.height = 1 + Math.max(height(node.left), height(node.right));
80+
81+
// Get the balance factor to check if this node became unbalanced
82+
int balance = getBalance(node);
83+
84+
// Left Left Case
85+
if (balance > 1 && key < node.left.key)
86+
return rightRotate(node);
87+
88+
// Right Right Case
89+
if (balance < -1 && key > node.right.key)
90+
return leftRotate(node);
91+
92+
// Left Right Case
93+
if (balance > 1 && key > node.left.key) {
94+
node.left = leftRotate(node.left);
95+
return rightRotate(node);
96+
}
97+
98+
// Right Left Case
99+
if (balance < -1 && key < node.right.key) {
100+
node.right = rightRotate(node.right);
101+
return leftRotate(node);
102+
}
103+
104+
// No balancing needed, return the unchanged node
105+
return node;
106+
}
107+
108+
// Print the tree in-order
109+
void inOrder(Node root) {
110+
if (root != null) {
111+
inOrder(root.left);
112+
System.out.print(root.key + " ");
113+
inOrder(root.right);
114+
}
115+
}
116+
117+
// public static void main(String[] args) {
118+
// AVLTree tree = new AVLTree();
119+
120+
// /* Constructing the AVL tree */
121+
// tree.root = tree.insert(tree.root, 10);
122+
// tree.root = tree.insert(tree.root, 20);
123+
// tree.root = tree.insert(tree.root, 30);
124+
// tree.root = tree.insert(tree.root, 40);
125+
// tree.root = tree.insert(tree.root, 50);
126+
// tree.root = tree.insert(tree.root, 25);
127+
128+
// /* Print in-order traversal of the AVL tree */
129+
// System.out.println("In-order traversal of the AVL tree:");
130+
// tree.inOrder(tree.root);
131+
// }
132+
}

Trees/AVLTree.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package Trees;
2+
3+
public class AVLTree {
4+
private int value;
5+
private AVLTree node;
6+
private int height;
7+
private int balanceFactor;
8+
private AVLTree leftChild;
9+
private AVLTree rightChild;
10+
11+
public AVLTree() {
12+
}
13+
14+
public AVLTree(int value) {
15+
this.value = value;
16+
}
17+
18+
public void insert(int value) {
19+
node = insert(node, value);
20+
}
21+
22+
public AVLTree insert(AVLTree node, int value) {
23+
24+
if (node == null) {
25+
return new AVLTree(value);
26+
}
27+
28+
if (value < node.value) {
29+
node.leftChild = insert(node.leftChild, value);
30+
} else {
31+
node.rightChild = insert(node.rightChild, value);
32+
}
33+
34+
height = calculateHeight(node);
35+
36+
return node;
37+
}
38+
39+
public int calculateHeight() {
40+
return calculateHeight(node);
41+
}
42+
43+
public int calculateHeight(AVLTree node) {
44+
45+
if (node == null) {
46+
return -1;
47+
}
48+
return Math.max(calculateHeight(node.leftChild), calculateHeight(node.rightChild)) + 1;
49+
}
50+
51+
public int balanceFactor() {
52+
return balanceFactor(node);
53+
}
54+
55+
public int balanceFactor(AVLTree node) {
56+
return calculateHeight(node.leftChild) - calculateHeight(node.rightChild);
57+
58+
// if (balanceFactor > 1) {
59+
// return "left skewed";
60+
// } else if (balanceFactor < -1) {
61+
// return "right skewed";
62+
// } else {
63+
// return "balanced";
64+
// }
65+
}
66+
67+
public void autoBalance() {
68+
autoBalance(node);
69+
}
70+
71+
public void autoBalance(AVLTree node) {
72+
balanceFactor = balanceFactor(node);
73+
74+
if (balanceFactor > 1) {
75+
76+
} else if (balanceFactor < -1) {
77+
int balanceRight = balanceFactor(node.rightChild);
78+
if (balanceRight > 0) {
79+
System.out.println("do rightrotate at: " + node.rightChild.value + "and leftrotate at:"
80+
+ node.value);
81+
}
82+
}
83+
}
84+
85+
private AVLTree rotateLeft(AVLTree root) {
86+
AVLTree newRoot = root.rightChild;
87+
88+
}
89+
}

Trees/BinaryTree.java

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package Trees;
2+
3+
public class BinaryTree {
4+
5+
private class Node {
6+
private int value;
7+
private Node leftChild;
8+
private Node rightChild;
9+
10+
public Node(int value) {
11+
this.value = value;
12+
}
13+
14+
}
15+
16+
public Node root;
17+
18+
public void insert(int value) {
19+
20+
if (root == null) {
21+
root = new Node(value);
22+
return;
23+
}
24+
25+
Node current = root;
26+
27+
while (true) {
28+
if (value < current.value) {
29+
if (current.leftChild == null) {
30+
current.leftChild = new Node(value);
31+
break;
32+
}
33+
34+
current = current.leftChild;
35+
36+
} else {
37+
if (current.rightChild == null) {
38+
current.rightChild = new Node(value);
39+
break;
40+
}
41+
42+
current = current.rightChild;
43+
44+
}
45+
46+
}
47+
48+
}
49+
50+
public void traversePreOrder() {
51+
traversePreOrder(root);
52+
}
53+
54+
public void traversePreOrder(Node node) {
55+
56+
if (node == null) {
57+
return;
58+
}
59+
System.out.println(node.value);
60+
61+
traversePreOrder(node.leftChild);
62+
traversePreOrder(node.rightChild);
63+
}
64+
65+
public Boolean equals(BinaryTree tree) {
66+
return equals(root, tree.root);
67+
}
68+
69+
// Uses Depth first search (Pre-order traversal)
70+
public Boolean equals(Node first, Node second) {
71+
return first.value == second.value && equals(first.leftChild, second.leftChild)
72+
&& equals(first.rightChild, second.rightChild);
73+
}
74+
75+
public Boolean isValid() {
76+
return isValid(root);
77+
}
78+
79+
public Boolean isValid(Node node) {
80+
if (node == null) {
81+
return true; // or false depending on how you define validity for an empty tree
82+
}
83+
84+
boolean leftValid = (node.leftChild == null || (node.leftChild.value < node.value && isValid(node.leftChild)));
85+
boolean rightValid = (node.rightChild == null
86+
|| (node.value < node.rightChild.value && isValid(node.rightChild)));
87+
88+
return leftValid && rightValid;
89+
}
90+
91+
public Boolean isBST(Node root, int min, int max) {
92+
if (root == null) {
93+
return true;
94+
}
95+
96+
if (root.value < min || root.value > max) {
97+
return false;
98+
}
99+
100+
return isBST(root.leftChild, min, root.value - 1) && isBST(root.rightChild, root.value + 1, max);
101+
}
102+
103+
// public Boolean isValid(Node node) {
104+
// if (node.leftChild == null) {
105+
// node.leftChild = new Node(0);
106+
107+
// }
108+
109+
// if (node.rightChild == null) {
110+
// node.rightChild = new Node(0);
111+
// }
112+
113+
// if (node.value == 0) {
114+
// return true;
115+
// }
116+
117+
// return (node.leftChild.value < node.value && node.value <
118+
// node.rightChild.value) && isValid(node.leftChild)
119+
// && isValid(node.rightChild);
120+
// }
121+
122+
}

0 commit comments

Comments
 (0)