-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProblem_9.java
More file actions
120 lines (95 loc) · 3.07 KB
/
Copy pathProblem_9.java
File metadata and controls
120 lines (95 loc) · 3.07 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
package trees.binarySearchTree;
// Problem Title -> Construct bt to bst
public class Problem_9 {
// A binary tree node
static class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
Node root;
// Function to store the inorder traversal of tree
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);
}
// Function to copy array elements back to tree
static void arrayToBST(int inorder[], Node node, Index index) {
if (node == null) {
return;
}
// Traverse the left subtree
arrayToBST(inorder, node.left, index);
// Copy the data to node
node.data = inorder[index.index];
index.index++;
// Traverse the right subtree
arrayToBST(inorder, node.right, index);
}
// This function converts a given binary tree to BST
static void binaryTreeToBST(Node root) {
// Base case
if (root == null) {
return;
}
// Count the number of nodes in Binary Tree
int n = countNodes(root);
// Create an array to store inorder traversal
int inorder[] = new int[n];
Index index = new Index();
index.index = 0;
// Store inorder traversal of tree in array
storeInorder(root, inorder, index);
// Sort the array
java.util.Arrays.sort(inorder);
// Copy array elements back to tree
index.index = 0;
arrayToBST(inorder, root, index);
}
// Function to count number of nodes in 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[]) {
Problem_9 tree = new Problem_9();
tree.root = new Node(10);
tree.root.left = new Node(30);
tree.root.right = new Node(20);
tree.root.left.left = new Node(5);
tree.root.left.right = new Node(15);
System.out.println("Inorder Traversal of given tree");
printInorder(tree.root);
// Convert binary tree to BST
binaryTreeToBST(tree.root);
System.out.println("\nInorder Traversal of converted BST");
printInorder(tree.root);
}
}