Buckets:
| class TreeNode { | |
| public int val; | |
| public TreeNode left; | |
| public TreeNode right; | |
| public TreeNode(int x) { | |
| val = x; | |
| left = null; | |
| right = null; | |
| } | |
| } | |
| public class BinarySearchTree { | |
| private TreeNode root; | |
| // Adds a new node with key to the binary search tree. | |
| public void insert(int key){ | |
| // Base case: if no tree, then create one | |
| if (root == null) { | |
| this.root = new TreeNode(key); | |
| } | |
| else { | |
| // find and return root where our key should be inserted. This is called the "search" method. | |
| Node current = search(this.root, key); | |
| int leftVal = (int)(current.left.val); | |
| if ((leftVal == 0) && !key.equals(leftVal)) { | |
| this.root.left = new TreeNode(key); | |
| } | |
| else{ | |
| // If not inserted yet or a duplicate is found | |
| Node current2 = search(current.right, key); | |
| int rightVal = (int)(current2.right.val); | |
| if ((rightVal == 0) && !key.equals(rightVal)) { | |
| this.root.left = new TreeNode(key); | |
| } | |
| } | |
| } | |
| } | |
| } |
Xet Storage Details
- Size:
- 1.33 kB
- Xet hash:
- 16850ca5407f976b66261a34d7d4940f47114343d47bbb0a1ee74f0ec7530730
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.