// Insert a new node in the binary search tree. // If the root node is null, create the root node here. // Otherwise, call the insert method of class TreeNode. public void InsertNode( IComparable insertValue ) { if ( root == null ) root = new TreeNode( insertValue ); else root.Insert( insertValue ); }
// recursive method to perform inorder traversal private void InorderHelper( TreeNode node ) { if ( node != null ) { // traverse left subtree InorderHelper( node.LeftNode ); // output node data Console.Write( node.Data + " " ); // traverse right subtree InorderHelper( node.RightNode ); } // end if }
// construct an empty Tree of IComparable public Tree() { root = null; }
// insert TreeNode into Tree that contains nodes; // ignore duplicate values public void Insert( IComparable insertValue ) { if ( insertValue.CompareTo(Data) < 0 ) // insert in left subtree { // insert new TreeNode if ( LeftNode == null ) LeftNode = new TreeNode( insertValue ); else // continue traversing left subtree LeftNode.Insert( insertValue ); } // end if else if ( insertValue.CompareTo( Data ) > 0 ) // insert in right { // insert new TreeNode if ( RightNode == null ) RightNode = new TreeNode( insertValue ); else // continue traversing right subtree RightNode.Insert( insertValue ); } // end else if }