RSS

Binary Tree Implementation in C#

Sat, Dec 12, 2009

Inverview Tips, Programming, Tutorials

This is simple implementation of Binary Tree in C#. It includes all common methods for binary tree.

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace Interview
{
public class BinaryTree
{
	public TreeNode root;
 
	public BinaryTree()
	{
		root = null;
	}
 
	public void print()
	{
		if (root == null)
			Console.WriteLine("Empty Tree");
		else
			printTree(root);
		Console.WriteLine();
	}
	private void printTree(TreeNode n)
	{
		if (n != null)
		{
			printTree(n.left);
			Console.Write(n.data + " ");
			printTree(n.right);
		}
	}
 
	public void insert(int x)
	{
		root = insert(root, x);
	}
	private TreeNode insert(TreeNode TreeNode, int data)
	{
		if (TreeNode == null)
		{
			TreeNode = new TreeNode();
			TreeNode.data = data;
		}
		else
		{
			if (data <= TreeNode.data)
			{
				TreeNode.left = insert(TreeNode.left, data);
			}
			else
			{
				TreeNode.right = insert(TreeNode.right, data);
			}
		}
		return (TreeNode);
	}
 
	public int Size()
	{
		return (Size(root));
	}
	private int Size(TreeNode TreeNode)
	{
		if (TreeNode == null) return 0;
		else
		{
			return Size(TreeNode.left) + 1 + Size(TreeNode.right);
		}
	}
 
	public int MaxDepth()
	{
		return MaxDepth(root);
	}
	private int MaxDepth(TreeNode TreeNode)
	{
		if (TreeNode == null) return 0;
		int lDepth = MaxDepth(TreeNode.left);
		int rDepth = MaxDepth(TreeNode.right);
 
		return Math.Max(lDepth, rDepth) + 1;
	}
 
	public int MinValue()
	{
		return MinValue(root);
	}
	private int MinValue(TreeNode TreeNode)
	{
		if (TreeNode.left == null) return TreeNode.data;
		else return MinValue(TreeNode.left);
	}
 
	public bool HasPathSum(int sum)
	{
		return HasPathSum(root, sum);
	}
	private bool HasPathSum(TreeNode TreeNode, int sum)
	{
		if (TreeNode == null)
		{
			return (sum == 0);
		}
		else
		{
			int temp = sum - TreeNode.data;
			return HasPathSum(TreeNode.left, temp) || HasPathSum(TreeNode.right, temp);
		}
	}
 
	public void PrintPaths()
	{
		int[] path = new int[1000];
		PrintPaths(root, path, 0);
	}
	private void PrintPaths(TreeNode TreeNode, int[] path, int pathLen)
	{
		if (TreeNode == null) return;
		path[pathLen] = TreeNode.data;
		pathLen++;
		if (TreeNode.left == null && TreeNode.right == null)
		{
			PrintPaths(path, pathLen);
		}
		else
		{
			PrintPaths(TreeNode.left, path, pathLen);
			PrintPaths(TreeNode.right, path, pathLen);
		}
	}
	private void PrintPaths(int[] ints, int len)
	{
		for (int i = 0; i < len; i++)
		{
			Console.Write(ints[i] + " ");
		}
		Console.WriteLine();
	}
 
	public void mirror()
	{
		mirror(root);
	}
	private void mirror(TreeNode TreeNode)
	{
		if (TreeNode != null)
		{
			mirror(TreeNode.left);
			mirror(TreeNode.right);
 
			TreeNode temp = TreeNode.left;
			TreeNode.left = TreeNode.right;
			TreeNode.right = temp;
		}
	}
 
	public void DoubleTree()
	{
		DoubleTree(root);
	}
	public void DoubleTree(TreeNode TreeNode)
	{
		if (TreeNode == null) return;
		DoubleTree(TreeNode.left);
		DoubleTree(TreeNode.right);
 
		TreeNode temp = TreeNode.left;
		TreeNode newNode = new TreeNode();
		newNode.data = TreeNode.data;
		TreeNode.left = newNode;
		TreeNode.left.left = temp;
	}
 
	public bool IsSameTree(BinaryTree tree)
	{
		return IsSameTree(root, tree.root);
	}
	private bool IsSameTree(TreeNode t1, TreeNode t2)
	{
		if (t1 == null && t2 == null) return (true);
		else if (t1 != null && t2 != null)
		{
			return (
				t1.data == t2.data &&
				IsSameTree(t1.left, t2.left) &&
				IsSameTree(t1.right, t2.right)
				);
		}
		else return (false);
	}
 
	public void PrintTreeLevelByLevel()
	{
		PrintTreeLevelByLevel(root);
	}
	private void PrintTreeLevelByLevel(TreeNode root)
	{
		if (root == null) return;
		Queue q = new Queue();
		q.Enqueue(root);
		while (q.Count > 0)
		{
			TreeNode b = (TreeNode)q.Dequeue();
			Console.Write(b.data + " ");
			if (b.left != null)
				q.Enqueue(b.left);
			if (b.right != null)
				q.Enqueue(b.right);
		}
	}
 
	public bool IsBST()
	{
		return (IsBST(root, Int32.MinValue, Int32.MinValue));
	}
	private bool IsBST(TreeNode TreeNode, int min, int max)
	{
		if (TreeNode == null)
		{
			return (true);
		}
		else
		{
			bool leftOk = IsBST(TreeNode.left, min, TreeNode.data);
			if (!leftOk) return (false);
			bool rightOk = IsBST(TreeNode.right, TreeNode.data + 1, max);
			return (rightOk);
		}
	}
}
 
public class TreeNode
{
	private int _data;
	private TreeNode _left;
	private TreeNode _right;
 
	public int data
	{
		get
		{
			return this._data;
		}
		set
		{
			this._data = value;
		}
	}
	public TreeNode left
	{
		get
		{
			return this._left;
		}
		set
		{
			this._left = value;
		}
	}
	public TreeNode right
	{
		get
		{
			return this._right;
		}
		set
		{
			this._right = value;
		}
	}
}
 
}
Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Other Posts:

This post was written by:

eXclusiveMinds - who has written 498 posts on eXclusiveMinds.


Contact the author

1 Comments For This Post

  1. chota Says:

    cool man!! likes it!!

Leave a Reply

You must be logged in to post a comment.