diff --git a/searchBinaryTree.java b/searchBinaryTree.java new file mode 100644 index 0000000000000000000000000000000000000000..aa2237ab272f126e0b9f9b26c38c808d083ebb35 --- /dev/null +++ b/searchBinaryTree.java @@ -0,0 +1,201 @@ +public class searchBinaryTree { + + private Node root; + + private int size; + + private static class Node { + E element; + Node lChild; + Node rChild; + + public Node(E element) { + this(element, null, null); + } + + public Node(E element, Node lChild, Node rChild) { + this.element = element; + this.lChild = lChild; + this.rChild = rChild; + } + } + + public searchBinaryTree(List elements) { + for (E e : elements) { + add(e); + } + } + + public searchBinaryTree(E[] elements) { + for (E e : elements) { + add(e); + } + } + + public searchBinaryTree() { + } + + public boolean contains(E element) { + return search(root, element); + } + + @SuppressWarnings("unchecked") + private boolean search(Node curRoot, E element) { + if (curRoot == null) + return false; + Comparable e = (Comparable) element; + int cmp = e.compareTo(curRoot.element); + if (cmp > 0) { + return search(curRoot.rChild, element); + } else if (cmp < 0) { + return search(curRoot.lChild, element); + } else { + return true; + } + } + + private boolean find(Node curRoot, E element, Node[] target) { + if (curRoot == null) + return false; + Node tmp = curRoot; + Comparable e = (Comparable) element; + while (tmp != null) { + int cmp = e.compareTo(tmp.element); + target[0] = tmp; + if (cmp > 0) { + tmp = tmp.rChild; + } else if (cmp < 0) { + tmp = tmp.lChild; + } else { + return true; + } + } + return false; + } + + public boolean add(E element) { + if (root == null) { + root = new Node(element); + size++; + return true; + } + Node[] target = new Node[1]; + if (!find(root, element, target)) { + Comparable e = (Comparable) element; + int cmp = e.compareTo(target[0].element); + Node newNode = new Node(element); + if (cmp > 0) { + target[0].rChild = newNode; + } else { + target[0].lChild = newNode; + } + size++; + return true; + } + return false; + } + + public boolean remove(E element) { + Node[] target = new Node[1]; + if (find(root, element, target)) { + remove(target[0]); + return true; + } + return false; + } + + private void free(Node node) { + node.element = null; + node.lChild = null; + node.rChild = null; + node = null; + } + + private void remove(Node node) { + Node tmp; + if (node.lChild == null && node.rChild == null) { + node = null; + } else if (node.lChild == null && node.rChild != null) { + tmp = node; + node = node.rChild; + free(tmp); + } else if (node.lChild != null && node.rChild == null) { + tmp = node; + node = node.lChild; + free(tmp); + } else { + tmp = node; + Node target = node.lChild; + while (target.rChild != null) { + tmp = target; + target = target.rChild; + } + node.element = target.element; // node.elementÔªËØÌ滻Ϊtarget.element + if (tmp == node) { + tmp.lChild = target.lChild; + } else { + tmp.rChild = target.lChild; + } + free(target); + } + size--; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size() == 0; + } + + public List preOrderTraverse() { + List list = new ArrayList(); + preOrderTraverse(root, list); + return list; + } + + private void preOrderTraverse(Node curRoot, List list) { + if (curRoot == null) + return; + E e = curRoot.element; + list.add(e); + preOrderTraverse(curRoot.lChild, list); + preOrderTraverse(curRoot.rChild, list); + } + + public List inOrderTraverse() { + List list = new ArrayList(); + inOrderTraverse(root, list); + return list; + } + + private void inOrderTraverse(Node curRoot, List list) { + if (curRoot == null) + return; + inOrderTraverse(curRoot.lChild, list); + list.add(curRoot.element); + inOrderTraverse(curRoot.rChild, list); + } + + public List postOrderTraverse() { + List list = new ArrayList(); + postOrderTraverse(root, list); + return list; + } + + private void postOrderTraverse(Node curRoot, List list) { + if (curRoot == null) + return; + inOrderTraverse(curRoot.lChild, list); + inOrderTraverse(curRoot.rChild, list); + list.add(curRoot.element); + } + + public String toString() { + return inOrderTraverse().toString(); + } + + + +} \ No newline at end of file