[Algorithm Basics (7)] Binary Tree

Use recursive and non-recursive methods to implement pre-order traversal, mid-order traversal and post-order traversal of a binary tree

1. Recursive order

public static void f(Node head) {
    
    
  // 1
  if (head == null) {
    
    
    return;
  }
  
  // 1
  f(head.left);
  // 2
  // 2
  f(head.right);
  // 3
  // 3
}

1,2,4,4,4,2,5,2,1,3,6,6,6,3,7,7,7,3,1

In recursive order, each node will be returned 3 times

In recursive order, first order, middle order and last order can be processed.

preorder traversal

For all subtrees, the head node is printed first, then the left node is printed, and then the right node is printed.

1,2,4,5,3,6,7

In recursive order, there is only the first print, which is preorder traversal.

In-order traversal (left and right)

4,2,5,1,6,3,7

In recursive order, printing is done for the second time, which is in-order traversal.

Postorder traversal (left and right heads)

4,5,2,6,7,3,1

In recursive order, printing is done last time, which is post-order traversal.

2. How to implement non-recursion

Preorder traversal (head and left) 1,2,4,5,3,6,7

Prepare a stack 1. First put the head node into the stack 2. Each time a node is popped in the stack, it is recorded as cur. Pop it up and print it. 3. First right and then left (if any) 4. Repeat

public static void preOrderUnRecur(Node head) {
    
    
  System.out.print("pre-order: ");
  // 如果头节点不等于空
  if (head != null) {
    
    
    // 准备一个栈
    Stack<Node> stack = new Statck<Node>();
    // 1 先把头节点放到栈里
    stack.add(head);
    while(!stack.isEmpty()) {
    
    
      // 2 每次在栈中弹出一个节点 记为cur 弹出并打印
    	head = stack.pop();
      System.out.print(head.value + " ");
      
      // 3 先右后左(如果有)
      if (head.right != null) {
    
    
        stack.push(head.right);
      }
      if (head.left != null) {
    
    
        stack.push(head.left);
      }
    }
  }
  System.out.println();
}

Postorder traversal

Change the pre-order traversal from head left and right to head right left and put it on the collection stack, and then print the (left and right heads) in the collection stack, which is post-order traversal.

public static void preOrderUnRecur(Node head) {
    
    
  System.out.print("after-order: ");
  // 如果头节点不等于空
  if (head != null) {
    
    
    // 准备一个栈
    Stack<Node> stack = new Statck<Node>();
    Stack<Node> stack_temp = new Statck<Node>();
    // 1 先把头节点放到栈里
    stack.add(head);
    while(!stack.isEmpty()) {
    
    
      // 2 每次在栈中弹出一个节点 记为cur 弹出并打印
    	head = stack.pop();
      stack_temp.push(head.value);
     
      
      // 3 先左后右(如果有)
      if (head.left != null) {
    
    
        stack.push(head.left);
        stack_temp.push(head.left);
      }
      if (head.right != null) {
    
    
        stack.push(head.right);
        stack_temp.push(head.right);
      }
    }
    
    while(!stack.isEmpty()) {
    
    
       System.out.print(stack_temp.pop() + " ");
    }
  }
  System.out.println();
}

In-order traversal (left and right)

First press the left border all-in, then press the right and repeat the pop-up

public static void inOrderUnRecur(Node head) {
    
    
  System.out.print("in-order: ");
  if (head != null) {
    
    
    Stack<Node> stack = new Stack<Node>();
    while(!stack.isEmpty() || head != null) {
    
    
      if (head != null) {
    
    
        stack.push(head);
        head = head.left;
      } else {
    
    
        head = stack.pop();
        System.out.print(head.value + " ");
        head = head.right;
      } 
    }
  }
}

Guess you like

Origin blog.csdn.net/sinat_29843547/article/details/131235159