/** * 定义二叉树节点 * @author TMAC-J * */ public class Node { private Node parent; private Node leftSonNode; private Node rightSonNode; private Object data; private BinaryTree binaryTree; public Node() {} public Node(BinaryTree binaryTree){ this.binaryTree = binaryTree; } public Node(Object data, BinaryTree binaryTree) { this.data = data; this.binaryTree = binaryTree; } public Node(Node parent,Node leftSonNode,Node rightSonNode,Object data) { this.parent = parent; this.leftSonNode = leftSonNode; this.rightSonNode = rightSonNode; this.data = data; } public Node getParent() { return parent; } public void setParent(Node parent) { binaryTree.setSize(binaryTree.getSize()+1); this.parent = parent; } public Node getLeftSonNode() { return leftSonNode; } public void setLeftSonNode(Node leftSonNode) { binaryTree.setSize(binaryTree.getSize()+1); this.leftSonNode = leftSonNode; } public Node getRightSonNode() { return rightSonNode; } public void setRightSonNode(Node rightSonNode) { binaryTree.setSize(binaryTree.getSize()+1); this.rightSonNode = rightSonNode; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public BinaryTree getBinaryTree() { return binaryTree; } public void setBinaryTree(BinaryTree binaryTree) { this.binaryTree = binaryTree; } } /** * @Description 这是一个数组实现的栈 * @author TMAC-J * */ public class Stack { /** * 储存元素的数组 */ private Object[] array = null; /** * 定义数组大小 */ private int size = 0; /** * 定义当前数组大小 */ private int currentSize = 0; /** * 定义指针 */ private int point = -1; /** * 构造方法 */ public Stack(int size){ this.size = size; array = new Object[size]; } /** * 入栈 */ public void push(Object data){ if(isFull()){ System.out.println("当前栈已满!"); return; } point++;//指针上移 array[point] = data; currentSize++; System.out.println("入栈"); } /** * 出栈 */ public void pop(){ if(isEmpty()){ System.out.println("当前栈为空!"); return; } array[point] = null; point--;//指针下移 currentSize--; System.out.println("出栈"); } /** * 判断是否为满 */ public boolean isFull(){ if(currentSize>=size)return true; return false; } /** * 判断是否为空 */ public boolean isEmpty(){ if(currentSize<=0)return true; return false; } public Object getStackTop(){ return array[point]; } } /** * 二叉树(我觉得一个实体类应当仅仅充当一个储存数据的bean,也就是说仅仅有set和get方法,而把其他操作抽象出去) * @author TMAC-J * */ public class BinaryTree { private Node root; private int size; private int depth; public BinaryTree(Node root) { this.root = root; } public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getDepth() { return depth; } public void setDepth(int depth) { this.depth = depth; } } /** * 管理器一般用单例 * @author TMAC-J * */ public class BinaryTreeHelper { private static BinaryTreeHelper instance; private int depth; private BinaryTreeHelper(){} public static BinaryTreeHelper getInstance(){ if(instance == null){ synchronized (BinaryTreeHelper.class) { if(instance == null){ instance = new BinaryTreeHelper(); } } } return instance; } /* A B C D E F G pre:ABDECFG*/ /** * dfs(深度优先遍历,等同于先序遍历,非递归形式) */ public void dfs(BinaryTree binaryTree,Stack stack){ if(binaryTree!=null){ stack.push(binaryTree.getRoot()); while(!stack.isEmpty()){ Node node = (Node)stack.getStackTop(); System.out.println((String)((Node)stack.getStackTop()).getData()); stack.pop(); if(node.getRightSonNode()!=null){ stack.push(node.getRightSonNode()); } if(node.getLeftSonNode()!=null){ stack.push(node.getLeftSonNode()); } } } }
/* * A B C D E F G * * DBEAFCG */ /** * 中序遍历(inorder traversal) */ public void inorderTraversal(BinaryTree binaryTree, Stack stack) { if (binaryTree != null) { Node root = binaryTree.getRoot(); while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root);// 先访问再入栈 root = root.getLeftSonNode(); } root = (Node) stack.getStackTop(); stack.pop(); System.out.println((String) root.getData()); root = root.getRightSonNode();// 如果是null,出栈并处理右子树 } } } /* * A B C D E F G * * DEBFGCA */ /** * 后序遍历(postorder traversal) 后序遍历不同于先序和中序,它是要先处理完左右子树,然后再处理根(回溯), * 所以需要一个记录哪些节点已经被访问的结构(可以在树结构里面加一个标记),这里可以用map实现 * 思路是先让左右出栈,最后在根出栈,然后回到上一层继续循环(因为栈中有上一层的结点) */ public void postorderTraversal(BinaryTree binaryTree, Stack stack) { if (binaryTree != null) { Node root = binaryTree.getRoot(); Map<Node, Boolean> map = new HashMap<Node, Boolean>(); stack.push(root); while (!stack.isEmpty()) { Node temp = (Node) stack.getStackTop(); if (temp.getLeftSonNode() != null && !map.containsKey(temp.getLeftSonNode())) { temp = temp.getLeftSonNode(); while (temp != null) { if (map.containsKey(temp)) break; else stack.push(temp); temp = temp.getLeftSonNode(); } continue; } if (temp.getRightSonNode() != null && !map.containsKey(temp.getRightSonNode())) { stack.push(temp.getRightSonNode()); continue; } Node t = (Node) stack.getStackTop(); stack.pop(); map.put(t, true); System.out.println(t.getData()); } } }
/* A B C D E F G ABCDEFG*/ /** * bfs(广度优先遍历) */ public void bfs(BinaryTree binaryTree,Queun queun){ if(binaryTree!=null){ queun.write(binaryTree.getRoot()); while(!queun.isEmpty()){ Node node = queun.getQueunHead(); System.out.println((String)node.getData()); queun.read(); if(node.getLeftSonNode()!=null){ queun.write(node.getLeftSonNode()); } if(node.getRightSonNode()!=null){ queun.write(node.getRightSonNode()); } } } } /** * 递归创建子树 */ public void createSonNode(Node parent,BinaryTree binaryTree){ Node leftSonNode = new Node(binaryTree); Node rightSonNode = new Node(binaryTree); parent.setLeftSonNode(leftSonNode); parent.setRightSonNode(rightSonNode); depth--; if(depth>1){ createSonNode(leftSonNode,binaryTree); createSonNode(rightSonNode,binaryTree); } } /** * 创建满二叉树 * @param depth,binaryTree * @return binaryTree */ public BinaryTree createFullBinaryTree(BinaryTree binaryTree,int depth){ this.depth = depth; Node root = binaryTree.getRoot(); createSonNode(root,binaryTree); return binaryTree; } } /** * @Decription 环形队列 * @author TMAC-J * */ public class Queun { /** * 初始化队列尺寸 */ private int queunSize = 0; /** * 初始化头指针 */ private int front = -1; /** * 初始化尾指针 */ private int rear = 0; /** * 声明数组 */ private Node[] array; /** * 当前大小 */ private int curentSize = 0; /** * 构造方法 * @param queunSize */ public Queun(int queunSize){ this.queunSize = queunSize; array = new Node[this.queunSize]; } /** * 读操作 */ public synchronized void read(){ if(!isEmpty()){ front = (front+1)%queunSize; array[front] = null; curentSize--; } else{ System.out.println("当前队列为空!"); /** * 优化CPU时间片的利用率,若当前队列为空会切换到另外的线程,不会继续执行此线程浪费时间和空间 */ try { this.notifyAll();//唤醒其他所有线程 this.wait();//释放对象锁,将当前线程置为阻塞 this.notify();//唤醒当前线程 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("front"+front); } /** * 写操作 * @param data */ public synchronized void write(Node data){ if(!isFull()){ array[rear] = data; rear = (rear+1)%queunSize; curentSize++; } else{ System.out.println("当前队列已满"); try { this.notifyAll(); this.wait(); this.notify(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("rear"+rear); } /** * 判断是否是满 */ public boolean isFull(){ if(curentSize == queunSize){ return true; } else{ return false; } } /** * 判断是否是空 */ public boolean isEmpty(){ if(curentSize == 0){ return true; } else{ return false; } } public Node getQueunHead(){ return array[(front+1)%queunSize]; } } public class Test { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(null); Node root = new Node("A",binaryTree); /*Node b = new Node("B",binaryTree); Node c = new Node("C",binaryTree); Node d = new Node("D",binaryTree); Node e = new Node("E",binaryTree); Node f = new Node("F",binaryTree); Node g = new Node("G",binaryTree); binaryTree.setRoot(root); root.setLeftSonNode(b); root.setRightSonNode(c); b.setLeftSonNode(d); b.setRightSonNode(e); c.setLeftSonNode(f); c.setRightSonNode(g); BinaryTreeHelper binaryTreeHelper = BinaryTreeHelper.getInstance(); binaryTreeHelper.bfs(binaryTree, new Queun(10));*/ binaryTree.setRoot(root); BinaryTreeHelper binaryTreeHelper = BinaryTreeHelper.getInstance(); binaryTreeHelper.createFullBinaryTree(binaryTree, 3); } }
以上就是二叉树相关