public bool Search(T element, ImplementBST <T> node) { if (node == null) { return(false); } if (node.nodeData.Equals(nodeData)) { Console.WriteLine("Element present in BST" + node.nodeData); return(true); } else { Console.WriteLine("Current element: " + node.nodeData); } if (element.CompareTo(node.nodeData) < 0) { Search(element, node.leftTree); } else { Search(element, node.rightTree); } return(false); }
public void InsertData(T data) { T currentNodeValue = this.nodeData; if (currentNodeValue.CompareTo(data) > 0) { if (this.leftTree == null) { this.leftTree = new ImplementBST <T>(data); } else { this.leftTree.InsertData(data); } } else { if (this.rightTree == null) { this.rightTree = new ImplementBST <T>(data); } else { this.leftTree.InsertData(data); } } }
static void Main(string[] args) { Console.WriteLine("Binary Search Tree Implementation"); ImplementBST <int> bST = new ImplementBST <int>(56); bST.InsertData(30); bST.InsertData(70); bST.InsertData(22); bST.InsertData(40); bST.InsertData(60); bST.InsertData(95); bST.InsertData(11); bST.InsertData(65); bST.InsertData(3); bST.InsertData(16); bST.InsertData(63); bST.InsertData(67); bST.Display(); bool result = bST.Search(30, bST); Console.WriteLine(result); }
public ImplementBST(T data) { this.nodeData = data; this.leftTree = null; this.rightTree = null; }