Skip to content

Commit e4d7c2a

Browse files
author
mission-peace
committed
adding binary tree questions
1 parent d75ce8b commit e4d7c2a

30 files changed

Lines changed: 2223 additions & 0 deletions
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.interview.tree;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/avl-tree-set-1-insertion/
5+
*/
6+
public class AVLTree {
7+
8+
private Node leftRotate(Node root){
9+
Node newRoot = root.right;
10+
root.right = root.right.left;
11+
newRoot.left = root;
12+
root.height = setHeight(root);
13+
root.size = setSize(root);
14+
newRoot.height = setHeight(newRoot);
15+
newRoot.size = setSize(newRoot);
16+
return newRoot;
17+
}
18+
19+
private Node rightRotate(Node root){
20+
Node newRoot = root.left;
21+
root.left = root.left.right;
22+
newRoot.right = root;
23+
root.height = setHeight(root);
24+
root.size = setSize(root);
25+
newRoot.height = setHeight(newRoot);
26+
newRoot.size = setSize(newRoot);
27+
return newRoot;
28+
}
29+
30+
private int setHeight(Node root){
31+
if(root == null){
32+
return 0;
33+
}
34+
return 1 + Math.max((root.left != null ? root.left.height : 0), (root.right != null ? root.right.height : 0));
35+
}
36+
37+
private int height(Node root){
38+
if(root == null){
39+
return 0;
40+
}else {
41+
return root.height;
42+
}
43+
}
44+
45+
private int setSize(Node root){
46+
if(root == null){
47+
return 0;
48+
}
49+
return 1 + Math.max((root.left != null ? root.left.size : 0), (root.right != null ? root.right.size : 0));
50+
}
51+
52+
public Node insert(Node root, int data){
53+
if(root == null){
54+
return Node.newNode(data);
55+
}
56+
if(root.data <= data){
57+
root.right = insert(root.right,data);
58+
}
59+
else{
60+
root.left = insert(root.left,data);
61+
}
62+
if(height(root.left) - height(root.right) > 1){
63+
if(height(root.left.left) >= height(root.left.right)){
64+
root = rightRotate(root);
65+
}else{
66+
root.left = leftRotate(root.left);
67+
root = rightRotate(root);
68+
}
69+
}else if(height(root.right) - height(root.left) > 1){
70+
if(height(root.right.right) >= height(root.right.left)){
71+
root = leftRotate(root);
72+
}else{
73+
root.right = rightRotate(root.right);
74+
root = leftRotate(root);
75+
}
76+
}
77+
else{
78+
root.height = setHeight(root);
79+
root.size = setSize(root);
80+
}
81+
return root;
82+
}
83+
84+
public static void main(String args[]){
85+
AVLTree avlTree = new AVLTree();
86+
Node root = null;
87+
root = avlTree.insert(root, -10);
88+
root = avlTree.insert(root, 2);
89+
root = avlTree.insert(root, 13);
90+
root = avlTree.insert(root, -13);
91+
root = avlTree.insert(root, -15);
92+
root = avlTree.insert(root, 15);
93+
root = avlTree.insert(root, 17);
94+
root = avlTree.insert(root, 20);
95+
96+
TreeTraversals tt = new TreeTraversals();
97+
tt.inOrder(root);
98+
System.out.println();
99+
tt.preOrder(root);
100+
}
101+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.interview.tree;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-property/
5+
* Test case
6+
* Root greater than children
7+
* Root less than children
8+
* Root equal to children
9+
*/
10+
public class ArbitaryTreeToChildSumTree {
11+
12+
public void childSumTree(Node root){
13+
toChildSumTree(root);
14+
}
15+
16+
private void incrementChild(Node root,int increment){
17+
if(root == null || (root.left ==null && root.right == null)){
18+
return;
19+
}
20+
if(root.left != null){
21+
root.left.data = root.left.data + increment;
22+
incrementChild(root.left,increment);
23+
}else{
24+
root.right.data = root.right.data + increment;
25+
incrementChild(root.right,increment);
26+
}
27+
}
28+
29+
private int toChildSumTree(Node root){
30+
if(root == null){
31+
return 0;
32+
}
33+
34+
if(root.left == null && root.right == null){
35+
return root.data;
36+
}
37+
38+
int sum1 = toChildSumTree(root.left);
39+
int sum2 = toChildSumTree(root.right);
40+
if(root.data < sum1 + sum2){
41+
root.data = sum1 + sum2;
42+
}else if(root.data > sum1 + sum2){
43+
incrementChild(root,root.data - sum1 - sum2);
44+
}
45+
return root.data;
46+
}
47+
48+
public static void main(String args[]){
49+
ArbitaryTreeToChildSumTree att = new ArbitaryTreeToChildSumTree();
50+
BinaryTree bt = new BinaryTree();
51+
Node head = null;
52+
head = bt.addNode(10, head);
53+
head = bt.addNode(15, head);
54+
head = bt.addNode(5, head);
55+
head = bt.addNode(7, head);
56+
head = bt.addNode(19, head);
57+
head = bt.addNode(20, head);
58+
head = bt.addNode(-1, head);
59+
att.childSumTree(head);
60+
TreeTraversals tt = new TreeTraversals();
61+
tt.inOrder(head);
62+
}
63+
64+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.interview.tree;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/check-if-each-internal-node-of-a-bst-has-exactly-one-child/
5+
*/
6+
public class BSTOneChildPreOrderTraversal {
7+
8+
public boolean isBST(int input[]){
9+
int max = 10000;
10+
int min = -100000;
11+
for(int i = 0; i < input.length-1; i++){
12+
if(input[i] > min && input[i] < max){
13+
if(input[i+1] < input[i]){
14+
max = input[i];
15+
}else{
16+
min = input[i];
17+
}
18+
}else{
19+
return false;
20+
}
21+
}
22+
if(input[input.length-1] < max && input[input.length-1] > min){
23+
return true;
24+
}else{
25+
return false;
26+
}
27+
}
28+
29+
public static void main(String args[]){
30+
int input[] = {20,10,14,15,17};
31+
BSTOneChildPreOrderTraversal boc = new BSTOneChildPreOrderTraversal();
32+
System.out.println(boc.isBST(input));
33+
}
34+
}

0 commit comments

Comments
 (0)