|
| 1 | +package com.coding.basic; |
| 2 | + |
| 3 | +public class BinaryTree<E extends Comparable<? super E>> { |
| 4 | + BinaryTreeNode root = null; |
| 5 | + |
| 6 | + public BinaryTree() { |
| 7 | + } |
| 8 | + |
| 9 | + public boolean isEmpty() { |
| 10 | + return root == null; |
| 11 | + } |
| 12 | + |
| 13 | + public void insert(E e) { |
| 14 | + root = insert(root, e); |
| 15 | + } |
| 16 | + |
| 17 | + private BinaryTreeNode<E> insert(BinaryTreeNode<E> node, E e) { |
| 18 | + if (node == null) { |
| 19 | + node = new BinaryTreeNode(e, null, null); |
| 20 | + } |
| 21 | + int compareResult = ((Comparable) e).compareTo(node.data); |
| 22 | + if (compareResult < 0) { |
| 23 | + node.left = insert(node.left, e); |
| 24 | + } else if (compareResult > 0) { |
| 25 | + node.right = insert(node.right, e); |
| 26 | + } |
| 27 | + return node; |
| 28 | + } |
| 29 | + |
| 30 | + public void clear() { |
| 31 | + root = null; |
| 32 | + } |
| 33 | + |
| 34 | + public boolean contains(E e) { |
| 35 | + return contains(root, e); |
| 36 | + } |
| 37 | + |
| 38 | + private boolean contains(BinaryTreeNode<E> node, E e) { |
| 39 | + if (node == null) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + int compareResult = ((Comparable) e).compareTo(node.data); |
| 43 | + if (compareResult < 0) { |
| 44 | + return contains(node.left, e); |
| 45 | + } else if (compareResult > 0) { |
| 46 | + return contains(node.right, e); |
| 47 | + } |
| 48 | + // matching |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + private BinaryTreeNode<E> findMin(BinaryTreeNode<E> node) { |
| 53 | + if (node != null) { |
| 54 | + while (node.left != null) { |
| 55 | + node = node.right; |
| 56 | + } |
| 57 | + } |
| 58 | + return node; |
| 59 | + } |
| 60 | + |
| 61 | + private BinaryTreeNode<E> findMax(BinaryTreeNode<E> node) { |
| 62 | + if (node != null) { |
| 63 | + while (node.right != null) { |
| 64 | + node = node.right; |
| 65 | + } |
| 66 | + } |
| 67 | + return node; |
| 68 | + } |
| 69 | + |
| 70 | + public void remove(E e) { |
| 71 | + root = remove(root, e); |
| 72 | + } |
| 73 | + |
| 74 | + private BinaryTreeNode<E> remove(BinaryTreeNode<E> node, E e) { |
| 75 | + if (node == null) { |
| 76 | + return node; |
| 77 | + } |
| 78 | + int compareResult = ((Comparable) e).compareTo(node.data); |
| 79 | + if (compareResult < 0) { |
| 80 | + node.left = remove(node.left, e); |
| 81 | + } else if (compareResult > 0) { |
| 82 | + node.right = remove(node.right, e); |
| 83 | + } |
| 84 | + // matching |
| 85 | + if (node.left != null && node.right != null) { // two children |
| 86 | + node.data = (E) findMax(node.right).data; |
| 87 | + node.right = remove(node.right, node.data); |
| 88 | + } else { // one child |
| 89 | + node = (node.left != null) ? node.left : node.right; |
| 90 | + } |
| 91 | + return node; |
| 92 | + } |
| 93 | + |
| 94 | + private class BinaryTreeNode<E> { |
| 95 | + E data; |
| 96 | + BinaryTreeNode left; |
| 97 | + BinaryTreeNode right; |
| 98 | + |
| 99 | + public BinaryTreeNode(E e, BinaryTreeNode<E> l, BinaryTreeNode<E> r) { |
| 100 | + data = e; |
| 101 | + left = l; |
| 102 | + right = r; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments