答案很给力,请大家参考:
自定义二叉树类:
- package io;
- import java.util.LinkedList;
- import java.util.List;
- /**
- * 二叉树实现
- *
- * @author x11
- *
- * @param <E>
- * 实现了Comparable接口的对象。
- */
- public class BinaryTree<E extends Comparable<E>> {
- private Node<E> root; // 根节点
- private int size; // Tree的大小
- /**
- * 往二叉树中添加数据
- *
- * @param e
- */
- public void add(E e) {
- // e为null,抛出非法参数异常
- if (e == null) {
- throw new IllegalArgumentException("data can't be null!");
- }
- // 如果不存在根节点,创建根节点
- if (root == null) {
- root = new Node<E>(null, e);
- size++;
- } else {
- Node<E> current = root;
- Node<E> parent;
- while (true) {
- parent = current;
- if (e.compareTo(parent.getData()) == 0) {
- // do nothing.
- // 节点已经存在,不储存。
- return;
- }
- // 节点的值比当前节点的值小,存储到当前节点的左子节点上。
- else if (e.compareTo(parent.getData()) < 0) {
- current = current.getLeftChild();
- if (current == null) {
- Node<E> newNode = new Node<E>(parent, e);
- parent.setLeftChild(newNode);
- size++;
- return;
- }
- }
- // 节点的值比当前节点的值大,存储到当前节点的右子节点上。
- else {
- current = current.getRightChild();
- if (current == null) {
- Node<E> newNode = new Node<E>(parent, e);
- parent.setRightChild(newNode);
- size++;
- return;
- }
- }
- }
- }
- }
- public void add(E[] array) {
- for (E e : array ) {
- add(e);
- }
- }
- /**
- * 清空二叉树
- */
- public void clear() {
- root = null;
- size = 0;
- }
- /**
- * 二叉树中是否包含e元素
- *
- * @param e
- * @return
- */
- public boolean contains(E e) {
- if (e == null || root == null) {
- return false;
- }
- Node<E> retNode = get(e);
- return !(retNode == null);
- }
- /**
- * 删除节点node及以node为父节点的子二叉树
- * @param node
- */
- public void remove(Node<E> node) {
- remove(node.getData());
- }
- public void remove(E e) {
- Node<E> retNode = get(e);
- if (retNode == root) { //如果要删除的节点是根节点,则清空整个二叉树
- clear();
- return;
- }
- if (retNode != null) {
- if (e.compareTo(retNode.getParent().getData()) < 0 ) {
- retNode.getParent().setLeftChild(null); // 该节点为父节点的左子节点,删除该左节点
- } else {
- retNode.getParent().setRightChild(null);
- }
- }
- }
- /**
- * 查找二叉树中是否存在某元素
- *
- * @param e
- * @return 如果节点存在,返回该节点,否则返回null.
- */
- public Node<E> get(E e) {
- if (root == null)
- return null;
- Node<E> current = root;
- while (!e.equals(current.getData())) {
- if (e.compareTo(current.getData()) < 0) {
- current = current.getLeftChild();
- } else {
- current = current.getRightChild();
- }
- if (current == null) {
- return null;
- }
- }
- return current;
- }
- /**
- *
- * @return 二叉树的大小
- */
- public int size() {
- return this.size;
- }
- /**
- * 中序遍历(LDR: leftChild->root->rightChild)
- * @return
- */
- public List<E> inOrder() {
- if (root == null)
- return null;
- List<E> list = new LinkedList<E>();
- inOrderTraverse(root.getLeftChild(),list);
- list.add(root.getData());
- inOrderTraverse(root.getRightChild(), list);
- return list;
- }
- private void inOrderTraverse(Node<E> child, List<E> list) {
- // TODO Auto-generated method stub
- if (child != null) {
- inOrderTraverse(child.getLeftChild(), list);
- list.add(child.getData());
- inOrderTraverse(child.getRightChild(), list);
- }
- }
- /**
- * 前序遍历(DRL)
- * @return
- */
- public List<E> preOrder() {
- if (root == null)
- return null;
- List<E> list = new LinkedList<E>();
- list.add(root.getData());
- preOrderTraverse(root.getRightChild(), list);
- preOrderTraverse(root.getLeftChild(),list);
- return list;
- }
- private void preOrderTraverse(Node<E> child, List<E> list) {
- // TODO Auto-generated method stub
- if (child != null) {
- list.add(child.getData());
- preOrderTraverse(child.getRightChild(), list);
- preOrderTraverse(child.getLeftChild(),list);
- }
- }
- /**
- * 后序遍历(LRD)
- * @return
- */
- public List<E> postOrder() {
- if (root == null)
- return null;
- List<E> list = new LinkedList<E>();
- postOrderTraverse(root.getLeftChild(),list);
- postOrderTraverse(root.getRightChild(), list);
- list.add(root.getData());
- return list;
- }
- private void postOrderTraverse(Node<E> child, List<E> list) {
- // TODO Auto-generated method stub
- if (child != null) {
- postOrderTraverse(child.getLeftChild(),list);
- postOrderTraverse(child.getRightChild(), list);
- list.add(child.getData());
- }
- }
- public String printTree() {
- if (inOrder() == null)
- return null;
- else
- return inOrder().toString();
- }
- }
- /**
- * 二叉树节点
- *
- * @author x11
- *
- * @param <E>
- * 实现了Comparable接口的对象。
- */
- class Node<E> {
- private Node<E> parent; // 父节点
- private Node<E> leftChild; // 左子节点
- private Node<E> rightChild; // 右子节点
- private E data; // 节点内数据
- public Node(Node<E> parent, E data) {
- super();
- this.parent = parent;
- this.data = data;
- }
- public Node<E> getParent() {
- return parent;
- }
- public void setParent(Node<E> parent) {
- this.parent = parent;
- }
- public Node<E> getLeftChild() {
- return leftChild;
- }
- public void setLeftChild(Node<E> leftChild) {
- this.leftChild = leftChild;
- }
- public Node<E> getRightChild() {
- return rightChild;
- }
- public void setRightChild(Node<E> rightChild) {
- this.rightChild = rightChild;
- }
- public E getData() {
- return data;
- }
- public void setData(E data) {
- this.data = data;
- }
- @Override
- public String toString() {
- return "Node [data=" + data + "]";
- }
-
- }
复制代码
Test类:
- package io;
- import static org.junit.Assert.*;
- import org.junit.Before;
- import org.junit.Test;
- public class BinaryTreeTest {
- BinaryTree<Integer> intBTree = null;
- BinaryTree<String> strBTree = null;
- Integer[] intArray;
- String[] strArray;
- @Before
- public void before() {
- intBTree = new BinaryTree<Integer>();
- strBTree = new BinaryTree<String>();
- intArray = new Integer[] { 6, 2, 7, 11, 8, 9, 25 };
- strArray = new String[] { "h", "f", "b", "c", "d", "a" };
- intBTree.add(intArray);
- strBTree.add(strArray);
- }
- @Test
- public void testAdd() {
- intBTree.add(2); // 元素相同,无法加入
- strBTree.add("a"); // 元素相同,无法加入
- System.out.println(intBTree.printTree());
- System.out.println(strBTree.printTree());
- }
- @Test
- public void testClear() {
- // fail("Not yet implemented");
- intBTree.clear();
- strBTree.clear();
- System.out.println(intBTree.printTree());
- System.out.println(strBTree.printTree());
- }
- @Test
- public void testContains() {
- System.out.println(intBTree.contains(6));
- System.out.println(intBTree.contains(1));
- System.out.println(strBTree.contains("a"));
- System.out.println(strBTree.contains("y"));
- }
- @Test
- public void testRemoveNodeOfE() {
- // fail("Not yet implemented");
- intBTree.remove(2);
- strBTree.remove("c");
- System.out.println(intBTree.printTree());
- System.out.println(strBTree.printTree());
- }
- @Test
- public void testRemoveE() {
- fail("Not yet implemented");
- }
- @Test
- public void testGet() {
- // fail("Not yet implemented");
- Node<Integer> val1 = intBTree.get(1);
- Node<Integer> val2 = intBTree.get(6);
- Node<String> val3 = strBTree.get("a");
- Node<String> val4 = strBTree.get("z");
-
- System.out.println(val1);
- System.out.println(val2);
- System.out.println(val3);
- System.out.println(val4);
- }
- @Test
- public void testSize() {
- // fail("Not yet implemented");
- System.out.println(intBTree.size());
- System.out.println(strBTree.size());
- }
- @Test
- public void testInOrder() {
- // fail("Not yet implemented");
- System.out.println(intBTree.inOrder().toString());
- System.out.println(strBTree.inOrder().toString());
- }
- @Test
- public void testPreOrder() {
- // fail("Not yet implemented");
- System.out.println(intBTree.preOrder().toString());
- System.out.println(strBTree.preOrder().toString());
- }
- @Test
- public void testPostOrder() {
- // fail("Not yet implemented");
- System.out.println(intBTree.postOrder().toString());
- System.out.println(strBTree.postOrder().toString());
- }
- @Test
- public void testPrintTree() {
- fail("Not yet implemented");
- }
- }
复制代码 |