public void Insert(int data) { // the data should go where there is already a node, delegate it the insertion to the existing node if (data < Data && Left != null) { Left.Insert(data); // node already exists at left, so it should handle the insertion } else if (data < Data) { Left = new BinarySearchTreeNode(data); } else if (data > Data && Right != null) { Right.Insert(data); // node already exists at right, so it should handle the insertion } else if (data > Data) { Right = new BinarySearchTreeNode(data); } }
public BinarySearchTreeNode(int data) { Data = data; Left = null; Right = null; }