Advertisement
MeliDragon

Untitled

May 12th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public void Insert(T element)
  2. {
  3. IBinarySearchTree <T> node = new BinarySearchTree <T>(element);
  4. if(this.left == null && this.right == null)
  5. {
  6. if(element.CompareTo(this.value) < 0)
  7. {
  8. this.left = node;
  9. }
  10. else if(element.CompareTo(this.value) > 0)
  11. {
  12. this.right = node;
  13. }
  14. }
  15. else
  16. {
  17. IBinarySearchTree<T> current = this;
  18. while(current != null)
  19. {
  20. if(element.CompareTo(current.Value) < 0)
  21. {
  22. if(current.Left != null)
  23. {
  24. current = current.Left;
  25. continue;
  26. }
  27. current.Left = node;
  28. break;
  29. }
  30. if(element.CompareTo(current.Value) > 0)
  31. {
  32. if(current.Right != null)
  33. {
  34. current = current.Right;
  35. continue;
  36. }
  37. current.Right = node;
  38. break;
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement