-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSortedLinkedListToBalancedBST.cs
153 lines (124 loc) · 3.71 KB
/
SortedLinkedListToBalancedBST.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SortedLinkedListToBalancedBST
{
public class LNode
{
public LNode next;
public int data;
public LNode(int n)
{
this.data = n;
}
public static int countLNode(LNode header)
{
if (header == null)
return 0;
int count = 0;
LNode temp = header;
while (temp != null)
{
temp = temp.next;
count++;
}
return count;
}
public static void push(ref LNode head, int data)
{
LNode newNode = new LNode(data);
newNode.next = head;
head = newNode;
}
public static void pushF(LNode head, int data)
{
LNode newNode = new LNode(data);
newNode.next = head;
head = newNode;
}
public static void printList(LNode node)
{
while (node != null)
{
System.Console.WriteLine(" "+node.data);
node = node.next;
}
}
}
public class TNode
{
TNode left;
TNode right;
public int data;
public TNode(int data)
{
this.data = data;
}
public static TNode listToTree(LNode header)
{
int n = LNode.countLNode(header);
return listToTreeRecursiv(ref header, n);
}
public static TNode listToTreeRecursiv(ref LNode header, int n)
{
//Base Case
if (n <= 0)
return null;
TNode left = listToTreeRecursiv(ref header, n/2);
TNode root = new TNode(header.data);
root.left = left;
header = header.next;
root.right = listToTreeRecursiv(ref header, n - n / 2 - 1);
return root;
}
public static TNode listToT(int[] array, int i, int n)
{
if (i > n)
return null;
TNode root = new TNode(array[i + (n - i) / 2]);
root.left = listToT(array, i, i + (n - i) / 2-1);
root.right = listToT(array, i + (n - i) / 2 + 1,n);
return root;
}
public static void preOrder(TNode node)
{
if (node == null)
return;
preOrder(node.left);
Console.Write(" " + node.data);
preOrder(node.right);
}
}
class SortedLinkedListToBalancedBST
{
static void Main(string[] args)
{
LNode head = null;
LNode.push(ref head, 7);
LNode.push(ref head, 6);
LNode.push(ref head, 5);
LNode.push(ref head, 4);
LNode.push(ref head, 3);
LNode.push(ref head, 2);
LNode.push(ref head, 1);
/*
LNode.pushF( head, 7);
LNode.pushF( head, 6);
LNode.pushF( head, 5);
LNode.pushF( head, 4);
LNode.pushF( head, 3);
LNode.pushF( head, 2);
LNode.pushF( head, 1);
*/
Console.WriteLine("Given Linked List");
// LNode.printList(head);
// TNode root = TNode.listToTree(head);
Int32 [] array = { 1, 2, 3, 4, 5, 6, 7 };
TNode root2 = TNode.listToT(array, 0,6);
Console.WriteLine("Preorder Traversal of constructed BST");
TNode.preOrder(root2);
}
}
}