-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProblem_10.java
More file actions
110 lines (88 loc) · 3.12 KB
/
Copy pathProblem_10.java
File metadata and controls
110 lines (88 loc) · 3.12 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
package trees.binarySearchTree;
// Problem Title -> Convert a normal bst into a balanced bst
public class Problem_10 {
// A binary tree node
static class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
Node root;
// A utility function to store inorder traversal of BST
static void storeInorder(Node node, int inorder[], Index index) {
if (node == null) {
return;
}
// Traverse the left subtree
storeInorder(node.left, inorder, index);
// Store the data of node
inorder[index.index] = node.data;
index.index++;
// Traverse the right subtree
storeInorder(node.right, inorder, index);
}
// A utility function to construct balanced BST from sorted array
static Node sortedArrayToBST(int arr[], int start, int end) {
// Base Case
if (start > end) {
return null;
}
// Get the middle element and make it root
int mid = (start + end) / 2;
Node node = new Node(arr[mid]);
// Recursively construct the left subtree and make it left child of root
node.left = sortedArrayToBST(arr, start, mid - 1);
// Recursively construct the right subtree and make it right child of root
node.right = sortedArrayToBST(arr, mid + 1, end);
return node;
}
// This function converts an unbalanced BST to a balanced BST
static Node buildBalancedTree(Node root) {
// Store nodes of given BST in sorted order
int n = countNodes(root);
int inorder[] = new int[n];
Index index = new Index();
storeInorder(root, inorder, index);
// Construct balanced BST from sorted nodes
return sortedArrayToBST(inorder, 0, n - 1);
}
// A utility function to count nodes in a binary tree
static int countNodes(Node root) {
if (root == null) {
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
// Utility class to hold index value
static class Index {
int index;
}
// A utility function to do inorder traversal of BST
static void printInorder(Node node) {
if (node == null) {
return;
}
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
// Driver program to test above functions
public static void main(String args[]) {
// Let us create an unbalanced BST
Problem_10 tree = new Problem_10();
tree.root = new Node(10);
tree.root.right = new Node(20);
tree.root.right.right = new Node(30);
tree.root.right.right.right = new Node(40);
tree.root.right.right.right.right = new Node(50);
System.out.println("Inorder traversal of the original tree:");
printInorder(tree.root);
// Convert to balanced BST
tree.root = buildBalancedTree(tree.root);
System.out.println("\nInorder traversal of the balanced tree:");
printInorder(tree.root);
}
}