【数据结构和算法】树相关知识
一、遍历
深度优先遍历和广度优先遍历(递归和栈)
import java.util.Stack; import java.util.concurrent.ConcurrentLinkedQueue; /** * **/ public class IterationTest { public static void main(String[] args) { Node node = getNode(); //深度优先遍历->前序遍历(递归) // testDepthFirstTraversalBefore(node); //深度优先遍历-中序遍历(递归) //testDepthFirstTraversalMind(node); //深度优先遍历-后续遍历(递归) //testDepthFirstTraversalAfter(node); //深度优先遍历->前序遍历(栈) //testDepthFirstTraversalBeforeByStack(node); //深度优先遍历->中序遍历(栈) //testDepthFirstTraversalMindByStack(node); //深度优先遍历->后续遍历(栈) //testDepthFirstTraversalAfterByStack(node); //广度优先遍历->广度优先遍历(队列) testBreadthFirstTraversal(node); } /** * ===========1 * =======2 3 * ====4 5 6 7 * 8 9 * * @return */ public static Node getNode() { Node node1 = new Node(); node1.setData("1"); Node node2 = new Node(); node2.setData("2"); Node node3 = new Node(); node3.setData("3"); Node node4 = new Node(); node4.setData("4"); Node node5 = new Node(); node5.setData("5"); Node node6 = new Node(); node6.setData("6"); Node node7 = new Node(); node7.setData("7"); Node node8 = new Node(); node8.setData("8"); Node node9 = new Node(); node9.setData("9"); node1.setLeft(node2); node1.setRight(node3); node2.setLeft(node4); node2.setRight(node5); node3.setLeft(node6); node3.setRight(node7); node4.setLeft(node8); node4.setRight(node9); return node1; } /** * 深度优先遍历-前序遍历:中,左,右 * <p> * 打印的顺序为:1,2,4,8,9,5,3,6,7 */ public static void testDepthFirstTraversalBefore(Node node) { if (node == null) { return; } //step1:遍历当前节点数据 System.out.println("data=>" + node.getData()); //step2:遍历左节点 testDepthFirstTraversalBefore(node.getLeft()); //step3:遍历右节点 testDepthFirstTraversalBefore(node.getRight()); } /** * 深度优先遍历-前序遍历:中,左,右 * <p> * 打印的顺序为:1,2,4,8,9,5,3,6,7 */ public static void testDepthFirstTraversalBeforeByStack(Node node) { Stack<Node> stack = new Stack<>(); Node currentNode = node; //遍历当前节点&并将当前节点的左节点压入栈中 while (currentNode != null || !stack.isEmpty()) { //遍历左节点 while (currentNode != null) { System.out.println("data=>" + currentNode.getData()); stack.push(currentNode); currentNode = currentNode.getLeft(); } //回溯栈顶元素,遍历右节点 if (!stack.isEmpty()) { currentNode = stack.pop(); currentNode = currentNode.getRight(); } } } /** * 深度优先遍历-中序遍历:8,4,9,2,5,1,6,3,7 * * @param node */ public static void testDepthFirstTraversalMind(Node node) { if (node == null) { return; } //step1:遍历左节点 testDepthFirstTraversalMind(node.getLeft()); //step2:遍历当前节点数据 System.out.println("data=>" + node.getData()); //step3:遍历右边节点 testDepthFirstTraversalMind(node.getRight()); } /** * 深度优先遍历-中序遍历:8,4,9,2,5,1,6,3,7 * * @param node */ public static void testDepthFirstTraversalMindByStack(Node node) { Stack<Node> stack = new Stack<>(); Node currentNode = node; //遍历当前节点&并将当前节点的左节点压入栈中 while (currentNode != null || !stack.isEmpty()) { //遍历左节点 while (currentNode != null) { stack.push(currentNode); currentNode = currentNode.getLeft(); } //回溯栈顶元素,遍历右节点 if (!stack.isEmpty()) { currentNode = stack.pop(); System.out.println("data=>" + currentNode.getData()); currentNode = currentNode.getRight(); } } } /** * 深度优先遍历-后续遍历:8,9,4,5,2,6,7,3,1 * * @param node */ public static void testDepthFirstTraversalAfter(Node node) { if (node == null) { return; } //step1:遍历左节点 testDepthFirstTraversalAfter(node.getLeft()); //step2:遍历右节点 testDepthFirstTraversalAfter(node.getRight()); //step3:遍历当前节点数据 System.out.println("data=>" + node.getData()); } /** * 深度优先遍历-后续遍历:8,9,4,5,2,6,7,3,1 * * @param node */ public static void testDepthFirstTraversalAfterByStack(Node node) { Stack<Node> stack = new Stack<>(); Node currentNode = node; Node temp = null; //遍历当前节点&并将当前节点的左节点压入栈中 while (currentNode != null || !stack.isEmpty()) { //遍历左节点 while (currentNode != null) { stack.push(currentNode); currentNode = currentNode.getLeft(); } //回溯 if (!stack.isEmpty()) { //如果是左节点,则打印 currentNode = stack.pop(); if (!stack.isEmpty()) { temp = stack.peek(); } else { temp = null; } System.out.println("data=>" + currentNode.getData()); if (temp != null && currentNode == temp.getLeft()) { currentNode = temp.getRight(); break; } else { currentNode = null; } } } } /** * 广度优先遍历 * * @param node */ public static void testBreadthFirstTraversal(Node node) { ConcurrentLinkedQueue<Node> queue = new ConcurrentLinkedQueue<>(); queue.add(node); Node currentNode = null; while ((currentNode = queue.poll()) != null) { Node left = currentNode.getLeft(); Node right = currentNode.getRight(); if (left != null) { queue.add(left); } if (right != null) { queue.add(right); } System.out.println("data=>" + currentNode.getData()); } } } class Node { private Node left; private Node right; private String data; public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
二、