-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
122 lines (92 loc) · 3.51 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
122 lines (92 loc) · 3.51 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
public class BinarySearchTree {
BinaryTreeNode<Integer> root;
int size = 0;
public static void helper(){
int[] arr = new int[]{1,2,3,4,5,6,7};
// BinaryTreeNode<Integer> root = arrayToBST(arr, 0, arr.length-1);
BinaryTreeNode<Integer> root = BinaryTreeHelper.takeInput();
BinaryTreeHelper.printUsingQ(root);
System.out.print("inOrder: "+" ");
inOrder(root);
System.out.println();
System.out.print("preOrder: "+" ");
preOrder(root);
// System.out.println("Printing reverse InOrder of Binary Search Tree");
// reverseInOrder(root);
// System.out.println();
// System.out.println("Height: "+height(root, 0 ));
// System.out.println(isBST(root,-1));
}
//constructing a balance binary tree from array to BinarySearchTree using sorted array
public static BinaryTreeNode<Integer> arrayToBST(int[] arr, int start, int end){
if (arr.length == 0 || start > end) return null;
int middle = (start + end)/2;
int data = arr[middle];
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(data);
int leftStart = start;
int leftEnd = middle - 1;
int rightStart = middle + 1;
int rightEnd = end;
root.left = arrayToBST(arr, leftStart, leftEnd);
root.right = arrayToBST(arr, rightStart, rightEnd);
return root;
}
public static void reverseInOrder(BinaryTreeNode<Integer> root){
if (root == null) return;
reverseInOrder(root.right);
System.out.print(root.data+" ");
reverseInOrder(root.left);
}
public static Node<Integer> BSTtoLinkedList(BinaryTreeNode<Integer> root, Node<Integer> head){
if (root == null) return null;
Node<Integer> ansOne = BSTtoLinkedList(root.right, head);
head = new Node<>(root.data);
head.next = ansOne;
Node<Integer> ansTwo = BSTtoLinkedList(root.left,head);
if (ansTwo != null) {
ansTwo.next = head;
return head;
}
return head;
}
public static int height (BinaryTreeNode<Integer> root, int tempHeight){
if (root == null) return tempHeight-1;
int ansOne = height(root.left, tempHeight + 1);
int ansTwo = height(root.right, tempHeight +1);
return (ansTwo > ansOne)? ansTwo : ansOne;
}
public static boolean isBST(BinaryTreeNode<Integer> root, int prev){
if (root == null) return true;
boolean oneAns = isBST(root.left, prev);
if (prev != -1) {
if (root.data < prev) {
oneAns = false;
}
}
prev = root.data;
if (oneAns){
oneAns = isBST(root.right, prev);
}
return oneAns;
}
public static void inOrder(BinaryTreeNode<Integer> root){
if (root == null){
return;
}
inOrder(root.left);
System.out.print(root.data+", ");
inOrder(root.right);
}
public static void postOrder(BinaryTreeNode<Integer> root){
if (root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+", ");
}
public static void preOrder(BinaryTreeNode<Integer> root){
if (root == null) return;
System.out.print(root.data+", ");
preOrder(root.left);
preOrder(root.right);
}
}