Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Insert(T element)
- {
- IBinarySearchTree <T> node = new BinarySearchTree <T>(element);
- if(this.left == null && this.right == null)
- {
- if(element.CompareTo(this.value) < 0)
- {
- this.left = node;
- }
- else if(element.CompareTo(this.value) > 0)
- {
- this.right = node;
- }
- }
- else
- {
- IBinarySearchTree<T> current = this;
- while(current != null)
- {
- if(element.CompareTo(current.Value) < 0)
- {
- if(current.Left != null)
- {
- current = current.Left;
- continue;
- }
- current.Left = node;
- break;
- }
- if(element.CompareTo(current.Value) > 0)
- {
- if(current.Right != null)
- {
- current = current.Right;
- continue;
- }
- current.Right = node;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement