The pre order traversal search, middle order traversal search and post order traversal search of binary tree in Java version

Posted by mepaco on Sat, 19 Feb 2022 23:47:43 +0100

PS: This article is a reprint of the article. It will be more readable to read the original text. There is a link to the original text at the end of the article

catalogue

1. Node search of binary tree

 1,1 Preorder traversal search

 1,2 Middle order traversal search

 1,3 Post order traversal search


1. Node search of binary tree

1. 1 preorder traversal search

In the Java version of data structure and algorithm (IV), we learned the preorder traversal, inorder traversal and postorder traversal of binary tree. With the previous foundation, in this article, we will talk about the preorder traversal search, inorder traversal search and postorder traversal search of binary tree, so it will be relatively easy to understand this article.

Here, let's talk about the search idea of preorder traversal:

(1) First judge whether the value of the current node is equal to the value of the node to be searched. If it is equal, return to the current node; If not, judge whether the left child node of the current node is empty.

(2) If the left child node of the current node is not empty, the left child node will be recursively searched.

(3) If the left child node can be found by recursive preorder search, return; No. continue to judge whether the right child node of the current node is empty. If it is not empty, continue to recurse to the right and search in the previous order; If a whole tree is not found after searching, null is returned.

Seeing this, some readers may say that I'm still very ignorant. It doesn't matter. Let's take an example here. The search idea of preorder traversal above is clearer; Let's draw a binary tree first, as shown in Figure 1:

picture

First of all, the binary tree in Figure 1 is regular. You can see that the sequence number of the parent node is smaller than that of the child node, and the left child node of the same parent node is smaller than that of the right child node; OK, suppose we are looking for a person with serial number 5 here, let's list the search steps:

(1) At the beginning, compare the sequence number of the node to be searched with the sequence number of the root node, and find that 1 is not equal to 5, so recursively search the left child node of the root node.

(2) Compare the sequence number of the left child node of the root node (sequence number is 2) with the sequence number of the node to be searched. It is found that 2 is not equal to 5, so the left child node (sequence number is 4) of the node with sequence number 2 is searched recursively.

(3) Compare the sequence number of the node (sequence number 4) with the sequence number of the node to be searched. It is found that 4 is not equal to 5. It is also found that the node with sequence number 4 is a leaf node, so the recursive preorder traversal of the node with sequence number 4 is ended.

(4) Here, the recursive preorder search of the left child node of the node with sequence number 2 has ended, so the recursive preorder search of the right child node (sequence number 5) of the node with sequence number 2 is carried out. It is found that the sequence number of the right child node of the node with sequence number 2 is equal to the sequence number of the node to be searched. Finally, the right child node of the node with sequence number 2 is returned to end the search.

Suppose we need to find a character that doesn't exist? For example, the search idea of a character with sequence number 8 is the same as that of a node with sequence number 5, except that searching for a node with sequence number 5 does not need to traverse the whole tree in Figure 1, while searching for a node with sequence number 8 needs to traverse the whole tree in figure 1. Here I will not list the pre sequence traversal search idea of searching for a character with sequence number 8.

Well, let's implement it with code, and use the pre order traversal to find the characters with sequence number 5 and sequence number 8 respectively;

(1) Write a node class, FigureNode:

public class FigureNode {
private int no;
private String name;
private FigureNode left;
private FigureNode right;

public FigureNode(int no, String name) {

super();
this.no = no;
this.name = name;

}

public String getName() {

return name;

}

public int getNo() {

return no;

}

public FigureNode getLeft() {

return left;

}

public void setLeft(FigureNode left) {

this.left = left;

}

public FigureNode getRight() {

return right;

}

public void setRight(FigureNode right) {

this.right = right;

}

}

(2) Write a Test class Test:

public class Test {
private FigureNode root;

public static void main(String[] args) {

Test test = new Test();
test.createBinaryTree();
test.preOrderSeach(5);
System.out.println("*****************");
test.preOrderSeach(8);

}

//Create a binary tree
private void createBinaryTree() {

FigureNode figureNode = new FigureNode(1,"Tathagata Buddha");
FigureNode figureNode2 = new FigureNode(2,"guanyin bodhisattva");
FigureNode figureNode3 = new FigureNode(3,"Ox demon king");
FigureNode figureNode4 = new FigureNode(4,"Sun WuKong");
FigureNode figureNode5 = new FigureNode(5,"Zhu Bajie");
FigureNode figureNode6 = new FigureNode(6,"Red boy");
FigureNode figureNode7 = new FigureNode(7,"Princess Iron Fan");

root = figureNode;//The character of the root node is the Tathagata Buddha
root.setLeft(figureNode2);//The left child node of the root node is the node with sequence number 2
root.setRight(figureNode3);//The right child node of the root node is the node with sequence number 3
figureNode2.setLeft(figureNode4);//The left child node with sequence number 2 is the node with sequence number 4
figureNode2.setRight(figureNode5);//The right child node with sequence number 2 is the node with sequence number 5
figureNode3.setLeft(figureNode6);//The left child node with sequence number 3 is the node with sequence number 6
figureNode3.setRight(figureNode7);//The right child node with sequence number 3 is the node with sequence number 7

}

//The pre order traversal search takes the root node as the current node
private void preOrderSeach(int no) {

if (root != null) {
  System.out.println("Preorder traversal search");
  FigureNode result = preOrderSeach(root, no);
  if (result == null) {
    System.out.println("No serial number is" + no + "Character");
  } else {
    System.out.println("Found serial number" + no + "Who is he or she"
        + result.getName());
  }
} else {
  System.out.println("This is an empty binary tree");
}

}

//Preorder traversal search
private FigureNode preOrderSeach(FigureNode node, int no) {

if (node.getNo() == no) {// Compare current node
  return node;
}

FigureNode res = null;

// Judge whether the left child node of the current node is empty
if (node.getLeft() != null) {

  // Left recursive preorder search returns when a node is found
  res = preOrderSeach(node.getLeft(), no);// Recursive preorder search
}

// If it is not empty, it indicates that the node is found during "left recursive preamble search"
if (res != null) {
  return res;
}

// Is the right child node of the current node empty
if (node.getRight() != null) {

  // Right recursive preorder search
  res = preOrderSeach(node.getRight(), no);
}

// If it is not empty, it means that the node is found during "right recursive preorder search"
return res;

}
}

The running results of the program are as follows:

picture

1. 2-order traversal search

Search idea of preorder traversal:

(1) Judge whether the left child node of the current node (assuming node A) is empty. If it is not empty, search in recursive middle order.

(2) If the recursive middle order search of the left child node of the current node (assuming node A) has been found, return; If not found, it will be compared with the current node (assuming node A). If yes, it will return to the current node. Otherwise, it will judge whether the right child node of the current node (assuming node A) is empty.

(3) If the right child node of the current node (assuming node A) is not empty, the recursive middle order search of the right child node of the current node (assuming node A) is performed; If the recursive middle order search of the right child node of the current node (assuming node A) has been found, return; If the entire tree is not found, null is returned.

Let's also give an example here. We also use the binary tree in Figure 1 to search for the middle order traversal. Suppose I want to find the character with serial number 7 and the character with serial number 8. Here I only write the search idea of the character with serial number 7. The search idea of the character with serial number 8 is the same as that of the character with serial number 7, Interested readers can analyze the search ideas of characters with serial number 8 by themselves. OK, let's start to analyze the search ideas of characters with serial number 7:

(1) At the beginning, I saw the root node with sequence number 1, found that the left child node of the root node was not empty, and then I went to the left child node of the root node (sequence number 2) for medium order traversal search.

(2) It is found that the left child node of the node with sequence number 2 is not empty, and the middle order traversal search is carried out to the left child node of the node with sequence number 2 (sequence number 4).

(3) If the node with sequence number 4 is found to be a leaf node, the sequence number of the leaf node will be compared with the sequence number of the node to be found. 4 is not equal to 7, so the recursive middle order traversal of the left child node of the node with sequence number 2 has ended.

(4) At this time, the sequence number of the node with sequence number 2 is compared with the sequence number of the node to be searched. 2 is not equal to 7, so judge whether the right child node of the node with sequence number 2 is empty. If it is found that it is not empty, carry out the recursive middle order traversal search of the right child node with sequence number 2 (sequence number 5).

(5) If the node with sequence number 5 is found to be a leaf node, the sequence number of the node with sequence number 5 will be compared with the sequence number of the node to be searched. 5 is not equal to 7. The recursive middle order traversal of the right child node of the node with sequence number 2 ends, and the recursive middle order traversal of the left child node of the root node (sequence number 2) ends.

(6) At this time, compare the sequence number of the root node with the sequence number of the node to be searched. 1 is not equal to 7, so judge whether the right child node of the root node is empty. If it is found not to be empty, perform recursive middle order traversal search of the right child node of the root node (sequence number is 3).

(7) It is found that the left child node of the node with sequence number 3 is not empty, and then the recursive middle order traversal search of the left child node (sequence number 6) of the node with sequence number 3 is carried out.

(8) It is found that the node with sequence number 6 is a leaf node, so compare the sequence number of the leaf node with that of the node to be searched. 6 is not equal to 7. At this time, the recursive middle order traversal of the left child node of the node with sequence number 3 ends, and then compare the sequence number of the node with sequence number 3 with that of the node to be searched. 3 is not equal to 7.

(9) At this time, judge whether the right child node of the node with sequence number 3 is empty. If it is not empty, perform the recursive middle order traversal search of the right child node (sequence number 7) of the node with sequence number 3.

(10) It is found that the node with sequence number 7 is a leaf node, so compare the sequence number of the leaf node with the sequence number of the node to be searched. If it is found that 7 is equal to 7, return to the leaf node immediately and the whole search process is over.

OK, for the middle order traversal and search of characters with sequence number 7 and sequence number 8 in the binary tree in Figure 1, we also use code to realize one:

(1) Let's continue to use the preceding sequence to traverse the node class FigureNode. Here, we can only add a test class Test2:

public class Test2 {
private FigureNode root;

public static void main(String[] args) {

Test2 test = new Test2();
test.createBinaryTree();
test.infixOrderSearch(7);
System.out.println("*****************");
test.infixOrderSearch(8);

}

//Create a binary tree
private void createBinaryTree() {

FigureNode figureNode = new FigureNode(1,"Tathagata Buddha");
FigureNode figureNode2 = new FigureNode(2,"guanyin bodhisattva");
FigureNode figureNode3 = new FigureNode(3,"Ox demon king");
FigureNode figureNode4 = new FigureNode(4,"Sun WuKong");
FigureNode figureNode5 = new FigureNode(5,"Zhu Bajie");
FigureNode figureNode6 = new FigureNode(6,"Red boy");
FigureNode figureNode7 = new FigureNode(7,"Princess Iron Fan");

root = figureNode;//The character of the root node is the Tathagata Buddha
root.setLeft(figureNode2);//The left child node of the root node is the node with sequence number 2
root.setRight(figureNode3);//The right child node of the root node is the node with sequence number 3
figureNode2.setLeft(figureNode4);//The left child node with sequence number 2 is the node with sequence number 4
figureNode2.setRight(figureNode5);//The right child node with sequence number 2 is the node with sequence number 5
figureNode3.setLeft(figureNode6);//The left child node with sequence number 3 is the node with sequence number 6
figureNode3.setRight(figureNode7);//The right child node with sequence number 3 is the node with sequence number 7

}

//Middle order traversal search, with the root node as the current node
private void infixOrderSearch(int no) {

if (root != null) {
  System.out.println("Middle order traversal search");
  FigureNode result = infixOrderSearch(root, no);
  if (result == null) {
    System.out.println("No serial number is" + no + "Character");
  } else {
    System.out.println("Found serial number" + no + "Who is he or she"
        + result.getName());
  }
} else {
  System.out.println("This is an empty binary tree");
}

}

//Middle order traversal search
private FigureNode infixOrderSearch(FigureNode node, int no) {

FigureNode res = null;

// Judge whether the left child node of the current node is empty
if (node.getLeft() != null) {

  // Left recursive middle order search returns if a node is found
  res = infixOrderSearch(node.getLeft(), no);
}

// If it is not empty, the node is found during "left recursive middle order search"
if (res != null) {
  return res;
}

if (node.getNo() == no) {// Compare current node
  return node;
}

// Is the right child node of the current node empty
if (node.getRight() != null) {

  // Right recursive middle order search
  res = infixOrderSearch(node.getRight(), no);
}

// If it is not empty, it means that the node is found during "right recursive middle order search"
return res;

}
}

The running results of the program are as follows:

picture

1. 3 post order traversal search

Search idea of preorder traversal:

(1) Judge whether the left child node of the current node (assuming node A) is empty. If it is not empty, it will be searched recursively.

(2) If the left child node of the current node (assuming node A) has been found by recursive post order traversal, it returns; If the left child node of the current node (assuming node A) is not found by recursive post order traversal, judge whether the right child node of the current node (assuming node A) is empty. If not, recursive post order traversal finds the right child node of the current node (assuming node A). If found, it returns.

(3) If the right child node of the current node (assuming node A) is not found by recursive post order traversal, it will be compared with the current node. If it is found, it will be returned; If A binary tree is not found after the subsequent traversal, null is returned.

OK, let's also take an example. Use the binary tree in Figure 1 for post order traversal search. Suppose I'm looking for a character with serial number 6 and a character with serial number 8. I won't write the character search idea with serial number 8. I only write the post order traversal search idea with serial number 6;

(1) At the beginning, what we see is the root node (serial number is 1). First judge whether the left child node of the root node is empty, and find that it is not empty, so we carry out recursive post order traversal and search of the left child node of the root node (serial number is 2).

(2) Then judge whether the left child node of the node with sequence number 2 is empty. If it is not empty, perform recursive post order traversal and search of the left child node (sequence number 4) of the node with sequence number 2.

(3) It is found that the node with sequence number 4 is a leaf node, so compare the sequence number of the leaf node with the sequence number of the node to be searched. 4 is not equal to 6. At this time, the recursive post sequence traversal of the left child node of the node with sequence number 2 ends.

(4) Judge whether the right child node of the node with sequence number 2 is empty, and find that it is not empty, so carry out the recursive post order traversal search of the right child node (sequence number 5) of the node with sequence number 2.

(5) It is found that the node with sequence number 5 is a leaf node, so the sequence number of the leaf node is compared with the sequence number of the node to be found. 5 is not equal to 6.

(6) At this time, the recursive post order traversal search of the right child node of the node with sequence number 2 has ended, so compare the sequence number of the node with sequence number 2 with the sequence number of the node to be searched, and 2 is not equal to 6.

(7) At this time, the recursive postorder traversal search of the left child node of the root node has ended, and then judge whether the right child node of the root node is empty. If it is found that it is not empty, the recursive postorder traversal search of the right child node of the root node (serial number is 3) is carried out.

(8) Judge whether the left child node with sequence number 3 is empty, and find that it is not empty, so carry out the recursive post order traversal search of the left child node (sequence number 6) of the node with sequence number 3.

(9) It is found that the node with sequence number 6 is a leaf node, so compare the sequence number of the leaf node with the sequence number to be searched, and 7 is equal to 7, so return to the leaf node and end the whole recursive sequence search.

Well, for the post order traversal and search of characters with serial number 6 and serial number 8 in the binary tree in Figure 1, we still use code to realize one:

(1) Let's continue to use the preceding sequence to traverse the node class FigureNode. Here, we only need to add a test class Test3:

public class Test3 {
private FigureNode root;

public static void main(String[] args) {

Test3 test = new Test3();
test.createBinaryTree();
test.postOrderSearch(6);
System.out.println("*****************");
test.postOrderSearch(8);

}

//Create a binary tree
private void createBinaryTree() {

FigureNode figureNode = new FigureNode(1, "Tathagata Buddha");
FigureNode figureNode2 = new FigureNode(2, "guanyin bodhisattva");
FigureNode figureNode3 = new FigureNode(3, "Ox demon king");
FigureNode figureNode4 = new FigureNode(4, "Sun WuKong");
FigureNode figureNode5 = new FigureNode(5, "Zhu Bajie");
FigureNode figureNode6 = new FigureNode(6, "Red boy");
FigureNode figureNode7 = new FigureNode(7, "Princess Iron Fan");

root = figureNode;// The character of the root node is the Tathagata Buddha
root.setLeft(figureNode2);// The left child node of the root node is the node with sequence number 2
root.setRight(figureNode3);// The right child node of the root node is the node with sequence number 3
figureNode2.setLeft(figureNode4);// The left child node with sequence number 2 is the node with sequence number 4
figureNode2.setRight(figureNode5);// The right child node with sequence number 2 is the node with sequence number 5
figureNode3.setLeft(figureNode6);// The left child node with sequence number 3 is the node with sequence number 6
figureNode3.setRight(figureNode7);// The right child node with sequence number 3 is the node with sequence number 7

}

//The subsequent traversal search takes the root node as the current node
private void postOrderSearch(int no) {

if (root != null) {
  System.out.println("Post order traversal search");
  FigureNode result = postOrderSearch(root, no);
  if (result == null) {
    System.out.println("No serial number is" + no + "Character");
  } else {
    System.out.println("Found serial number" + no + "Who is he or she"
        + result.getName());
  }
} else {
  System.out.println("This is an empty binary tree");
}

}

//Post order traversal search
private FigureNode postOrderSearch(FigureNode node, int no) {

FigureNode res = null;

// Judge whether the left child node of the current node is empty
if (node.getLeft() != null) {

  // Left recursive postorder search returns when a node is found
  res = postOrderSearch(node.getLeft(), no);
}

// If it is not empty, it means that the node is found during "left recursive post order search"
if (res != null) {
  return res;
}

// Is the right child node of the current node empty
if (node.getRight() != null) {

  // Right recursive postorder search
  res = postOrderSearch(node.getRight(), no);
}

// If it is not empty, it means that the node is found during "right recursive post order search"
if (res != null) {
  return res;
}

if (node.getNo() == no) {// Compare current node
  return node;
}
return res;

}
}

The running results of the program are as follows:

picture

Summary: at present, the comparison times of post order traversal search are the least. It is recommended to use post order traversal search. Interested readers can try it.

Topics: Java