-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertBinaryTreeIntoBinarySearchTree.java
More file actions
94 lines (70 loc) · 2.26 KB
/
Copy pathConvertBinaryTreeIntoBinarySearchTree.java
File metadata and controls
94 lines (70 loc) · 2.26 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ConvertBinaryTreeIntoBinarySearchTree {
Node root;
public ConvertBinaryTreeIntoBinarySearchTree(Node root) {
this.root = root;
}
public ConvertBinaryTreeIntoBinarySearchTree() {
root = null;
}
static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = right = null;
}
}
public static void printInOrder(Node root) {
if (root == null) {
return;
}
printInOrder(root.left);
System.out.print(root.data + " ");
printInOrder(root.right);
}
private static void storeValues(Node root, List<Integer> values) {
if (root == null) {
return;
}
storeValues(root.left, values);
values.add(root.data);
storeValues(root.right, values);
}
private static void rebuildTree(Node root, List<Integer> values, int[] index) {
if (root == null) {
return;
}
rebuildTree(root.left, values, index);
root.data = values.get(index[0]);
index[0]++;
rebuildTree(root.right, values, index);
}
public static void convertToBinarySearchTree(Node root) {
List<Integer> values = new ArrayList<>();
storeValues(root, values);
Collections.sort(values);
int[] index = {0};
rebuildTree(root, values, index);
}
public static void main(String[] args) {
Node root = new Node(2);
root.left = new Node(4);
root.right = new Node(6);
root.left.left = new Node(8);
root.left.right = new Node(11);
root.right.right = new Node(20);
root.right.left = new Node(13);
root.right.left.left = new Node(9);
System.out.println("Binary Tree's InOrder Traversal");
printInOrder(root);
System.out.println();
convertToBinarySearchTree(root);
System.out.println("Binary Search Tree's InOrder Traversal");
printInOrder(root);
System.out.println();
}
}