-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeToBST(CSHARP)
168 lines (138 loc) · 5.03 KB
/
BinaryTreeToBST(CSHARP)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gt
{
public class Program
{
//Represent a node of binary tree
public class Node<T>
{
public T data;
public Node<T> left;
public Node<T> right;
public Node(T data)
{
//Assign data to the new node, set left and right children to null
this.data = data;
this.left = null;
this.right = null;
}
}
public class ConvertBTtoBST<T>
{
//Represent the root of binary tree
public Node<T> root;
T[] treeArray;
int index = 0;
public ConvertBTtoBST()
{
root = null;
}
//convertBTBST() will convert a binary tree to binary search tree
public Node<T> convertBTBST(Node<T> node)
{
//Variable treeSize will hold size of tree
int treeSize = calculateSize(node);
treeArray = new T[treeSize];
//Converts binary tree to array
convertBTtoArray(node);
//Sort treeArray
Array.Sort(treeArray);
//Converts array to binary search tree
Node<T> d = createBST(0, treeArray.Length - 1);
return d;
}
//calculateSize() will calculate size of tree
int calculateSize(Node<T> node)
{
int size = 0;
if (node == null)
return 0;
else
{
size = calculateSize(node.left) + calculateSize(node.right) + 1;
return size;
}
}
//convertBTtoArray() will convert the given binary tree to its corresponding array representation
public void convertBTtoArray(Node<T> node)
{
//Check whether tree is empty
if (root == null)
{
Console.WriteLine("Tree is empty");
return;
}
else
{
if (node.left != null)
convertBTtoArray(node.left);
//Adds nodes of binary tree to treeArray
treeArray[index] = node.data;
index++;
if (node.right != null)
convertBTtoArray(node.right);
}
}
//createBST() will convert array to binary search tree
public Node<T> createBST(int start, int end)
{
//It will avoid overflow
if (start > end)
{
return null;
}
//Variable will store middle element of array and make it root of binary search tree
int mid = (start + end) / 2;
Node<T> node = new Node<T>(treeArray[mid]);
//Construct left subtree
node.left = createBST(start, mid - 1);
//Construct right subtree
node.right = createBST(mid + 1, end);
return node;
}
//inorder() will perform inorder traversal on binary search tree
public void inorderTraversal(Node<T> node)
{
//Check whether tree is empty
if (root == null)
{
Console.WriteLine("Tree is empty");
return;
}
else
{
if (node.left != null)
inorderTraversal(node.left);
Console.Write(node.data + " ");
if (node.right != null)
inorderTraversal(node.right);
}
}
}
public static void Main()
{
ConvertBTtoBST<int> bt = new ConvertBTtoBST<int>();
//Add nodes to the binary tree
bt.root = new Node<int>(1);
bt.root.left = new Node<int>(2);
bt.root.right = new Node<int>(3);
bt.root.left.left = new Node<int>(4);
bt.root.left.right = new Node<int>(5);
bt.root.right.left = new Node<int>(6);
bt.root.right.right = new Node<int>(7);
//Display given binary tree
Console.WriteLine("Inorder representation of binary tree: ");
bt.inorderTraversal(bt.root);
//Converts binary tree to corresponding binary search tree
Node<int> bst = bt.convertBTBST(bt.root);
//Display corresponding binary search tree
Console.WriteLine("\nInorder representation of resulting binary search tree: ");
bt.inorderTraversal(bst);
Console.ReadLine();
}
}
}