|
| 1 | +package com.shekhargulati.ninetynine_problems.java8._00_random.tadm.ch03; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +public class Problem3_8 { |
| 6 | + |
| 7 | + public static void main(String[] args) { |
| 8 | + Tree<Integer> numbers = new Tree<>(40); |
| 9 | + numbers.insert(21); |
| 10 | + numbers.insert(10); |
| 11 | + numbers.insert(30); |
| 12 | + numbers.insert(50); |
| 13 | + |
| 14 | + System.out.println(numbers); |
| 15 | + |
| 16 | + System.out.println(numbers.find(6)); |
| 17 | + System.out.println(numbers.find(2)); |
| 18 | + System.out.println(numbers.find(5)); |
| 19 | + } |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +class Tree<T extends Comparable<? super T>> { |
| 24 | + |
| 25 | + Node<T> node; |
| 26 | + Tree<T> left; |
| 27 | + Tree<T> right; |
| 28 | + |
| 29 | + public Tree(T t) { |
| 30 | + this.node = new Node<>(t); |
| 31 | + } |
| 32 | + |
| 33 | + public void insert(T t) { |
| 34 | + if (t.compareTo(this.node.data) < 0) { |
| 35 | + this.node.leftCount++; |
| 36 | + if (this.left == null) { |
| 37 | + this.left = new Tree<>(t); |
| 38 | + } else { |
| 39 | + this.left.insert(t); |
| 40 | + } |
| 41 | + } else if (t.compareTo(this.node.data) > 0) { |
| 42 | + this.node.rightCount++; |
| 43 | + // save to right tree |
| 44 | + if (this.right == null) { |
| 45 | + this.right = new Tree<>(t); |
| 46 | + } else { |
| 47 | + this.right.insert(t); |
| 48 | + } |
| 49 | + } else { |
| 50 | + throw new IllegalArgumentException(String.format("%s is already present in the binary tree", t)); |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + public boolean member(T t) { |
| 56 | + if (t.compareTo(this.node.data) < 0) { |
| 57 | + return this.left != null && this.left.member(t); |
| 58 | + } else if (t.compareTo(this.node.data) > 0) { |
| 59 | + return this.right != null && this.right.member(t); |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + public Optional<T> find(int k) { |
| 65 | + if (k == this.node.leftCount + 1) { |
| 66 | + return Optional.of(this.node.data); |
| 67 | + } else if (k > this.node.leftCount) { |
| 68 | + k = k - (this.node.leftCount + 1); |
| 69 | + return this.right == null ? Optional.empty() : this.right.find(k); |
| 70 | + } else { |
| 71 | + return this.left == null ? Optional.empty() : this.left.find(k); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() { |
| 77 | + return "Tree{" + |
| 78 | + "node=" + node + |
| 79 | + ", left=" + left + |
| 80 | + ", right=" + right + |
| 81 | + '}'; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class Node<T> { |
| 86 | + |
| 87 | + T data; |
| 88 | + int leftCount; |
| 89 | + int rightCount; |
| 90 | + |
| 91 | + public Node(T data) { |
| 92 | + this.data = data; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String toString() { |
| 97 | + return "Node{" + |
| 98 | + "data=" + data + |
| 99 | + ", leftCount=" + leftCount + |
| 100 | + ", rightCount=" + rightCount + |
| 101 | + '}'; |
| 102 | + } |
| 103 | +} |
0 commit comments