public void Search(T data) { if (Parent.data.CompareTo(data) == 0) { Console.WriteLine("Found"); return; } if (Parent.data.CompareTo(data) < 0) { Parent = Parent.RightChild; Search(data); } else { Parent = Parent.leftChild; Search(data); } }
public void Display(MyBinaryNode <T> parent) { Console.WriteLine("Parent : {0}", parent.data); if (parent.leftChild != null) { Console.WriteLine("Left :" + parent.leftChild.data); } if (parent.RightChild != null) { Console.WriteLine("Right :" + parent.RightChild.data); } if (parent.leftChild != null) { Display(parent.leftChild); } if (parent.RightChild != null) { Display(parent.RightChild); } }
/// <summary> /// UC 1 Adds the specified data. /// </summary> /// <param name="data">The data.</param> public void Add(T data) { // If the tree is empty if (rootNode == null) { rootNode = new MyBinaryNode <T>(data); return; } MyBinaryNode <T> temp = rootNode; // If the given element is less than compared then traverse left if (data.CompareTo(temp.data) < 0) { if (temp.leftTree == null) { temp.leftTree = new BinarySearchTree <T>(); temp.leftTree.rootNode = new MyBinaryNode <T>(data); return; } else { temp.leftTree.Add(data); } } // If the given element is greater than compared then traverse right else if (data.CompareTo(temp.data) > 0) { if (temp.rightTree == null) { temp.rightTree = new BinarySearchTree <T>(); temp.rightTree.rootNode = new MyBinaryNode <T>(data); return; } else { temp.rightTree.Add(data); } } }
public void Add(T data) { if (root == null) { root = new MyBinaryNode <T>(data); } else { var node = new MyBinaryNode <T>(data); var temp = Parent; if (data.CompareTo(Parent.data) < 0) { if (temp.leftChild == null) { temp.leftChild = node; } else { Parent = temp.leftChild; Add(data); } } else { if (temp.RightChild == null) { temp.RightChild = node; } else { Parent = temp.RightChild; Add(data); } } } Parent = root; }
public MyBinaryNode(T data) { this.data = data; leftChild = null; RightChild = null; }
public BinarySearchTree() { root = null; Parent = null; }