Binary Search tree is a collection of nodes where each node has a left node and right node. To Satisfy binary search tree all the left nodes data must be less than root nodes and all the right nodes data must be greater than root nodes.
Here is the code to create BST in C#:
class Program
{
static void Main(string[] args)
{
BSTnode n = new BSTnode(5);
n.add(2);
n.add(7);
n.add(3);
n.add(1);
n.add(6);
n.add(8);
}
}
public class BSTnode
{
public int data { get; set; }
public BSTnode LeftNode { get; set; }
public BSTnode rightNode { get; set; }
public BSTnode(int _data)
{
data = _data;
}
public void add(int _data)
{
BSTnode newN = new BSTnode(_data);
if (_data < data)
{
if (LeftNode != null)
{
LeftNode.add(_data);
}
else
{
LeftNode = newN;
}
}
else
{
if (rightNode != null)
{
rightNode.add(_data);
}
else
{
rightNode = newN;
}
}
}
}
This Solution is written by me and is not completely tested. Please comment if you found any alternative solutions or any enhancement to my solution.
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment