public interface Tree<E> { boolean isEmpty(); E getRoot(); E getParent(int nodeNum); E getLeftSibling(int nodeNum); E getRightSibling(int nodeNum); TreeNode<E> createNode(int headNum,E l,E r); TreeNode<E> createHead(int headNum,E l,E r); void breadFirstOrder();//广度优先 void preOrder();//先序遍历 void inOrder();//中序遍历 void postORrder();//后续遍历 void clear();//删除整个树 }
class TreeNode<E> { private E item; private E leftSibling; private E rightSibling; TreeNode(E item,E leftSibling,E rightSibling){ this.setItem(item); this.setLeftSibling(leftSibling); this.setRightSibling(rightSibling); } public E getItem() { return item; } public void setItem(E item) { this.item = item; } public E getLeftSibling() { return leftSibling; } public void setLeftSibling(E leftSibling) { this.leftSibling = leftSibling; } public E getRightSibling() { return rightSibling; } public void setRightSibling(E rightSibling) { this.rightSibling = rightSibling; } }
public class ArrayTree<E> implements Tree<E> { public Object[] elementData;//存储元素的数组 public int level;//层数 最高层位0层 public TreeNode<E> root;//跟 public final static int DEFAULT_LEVEL = 10; public ArrayTree(int level) { elementData = new Object[(2 << level) - 1]; this.level = level; } public ArrayTree() { this(DEFAULT_LEVEL); } @Override public boolean isEmpty() { return elementData[0] == null; } @Override public E getRoot() { return (E) elementData[0]; } @Override//获得父节点 通过位运算 已知某个节点的位置 得到父节点的位置 public E getParent(int nodeNum) { if (nodeNum % 2 == 0) { return checkIndex((nodeNum >> 1) - 1) ? (E) elementData[(nodeNum - 1) - 1] : null; } else { return checkIndex((nodeNum - 1) >> 1) ? (E) elementData[(nodeNum - 1) >> 1] : null; } } @Override public E getLeftSibling(int nodeNum) { return (checkIndex(nodeNum) && checkIndex(nodeNum << 1 + 1)) ? (E) elementData[nodeNum << 2 + 1] : null; } @Override public E getRightSibling(int nodeNum) { return (checkIndex(nodeNum) && checkIndex((nodeNum + 1) << 1)) ? (E) elementData[(nodeNum + 1) << 1] : null; } @Override public TreeNode<E> createNode(int headNum, E l, E r) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TreeNode<E> createHead(int headNum, E l, E r) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void breadFirstOrder() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void preOrder() { Stack<Integer> dataStack = new Stack<Integer>();//用栈来存放节点编号 int currentNum = 0; String str = ""; while (!dataStack.isEmpty() || elementData[currentNum] != null) { if (checkIndex(currentNum) && elementData[currentNum] != null) { str += (elementData[currentNum] + " "); if (checkIndex((currentNum + 1) << 1) && elementData[(currentNum + 1) << 1] != null) { dataStack.push((currentNum + 1) << 1);//右孩子索引放入栈中 } currentNum = (currentNum << 1) + 1; } else { currentNum = dataStack.pop(); } } System.out.println(str); } @Override public void inOrder() { Stack<Integer> dataStack = new Stack<Integer>(); int currentNum = 0; String str = " "; while (!dataStack.isEmpty() || elementData[currentNum] != null) { if (checkIndex(currentNum) && elementData[currentNum] != null) { dataStack.push(currentNum); currentNum = (currentNum << 1) + 1; } else { currentNum = dataStack.pop(); str += (elementData[currentNum] + ""); currentNum = (currentNum + 1) << 1; } } System.out.println(str); } @Override public void postORrder() { Set<Integer> visitedSet = new HashSet<Integer>(); Stack<Integer> dataStack = new Stack<Integer>(); int currentNum = 0; String str = " "; while (checkIndex(currentNum) && elementData[currentNum] != null) { while (checkIndex((currentNum << 1) + 1) && elementData[(currentNum << 1) + 1] != null) { dataStack.push(currentNum);//持续向左搜索 一旦遇到左结点为空 就停止搜索 currentNum = (currentNum << 1) + 1; } //(当前结点不为空)且(没有右孩子或者右孩子被访问过了) 则访问该节点 while (checkIndex(currentNum) && elementData[currentNum] != null && (!checkIndex(((currentNum + 1) >> 1)) || elementData[(currentNum + 1) << 1] == null || visitedSet.contains(elementData[(currentNum + 1) << 1]))) { str += elementData[currentNum]; str += " "; visitedSet.add(currentNum);//添加进被访问过的集合 if (dataStack.isEmpty()) {//栈空直接结束 System.out.println(str); return; } currentNum = dataStack.pop(); } dataStack.push(currentNum); currentNum = (currentNum + 1) << 1;//转向右子树 } } @Override public void clear() { root = null; elementData = null; level = 0; } public boolean checkIndex(int index) { return index >= 0 && index <= ((1 << (level + 1)) - 2); } }