# Java data structure and algorithm

catalogue

Implementation of pre order, middle order and post order search code

Binary tree - finds the specified node

requirement

Code implementation (1)

Complete code

Operation effect

Binary tree - delete node

requirement

Finish deleting node

code implementation

Complete code

Operation results

# Implementation of pre order, middle order and post order search code

## Binary tree - finds the specified node

### requirement

1) Please write pre order search, middle order search and post order search methods

2) Three search methods are used to find the node with heroNO=5

3) And analyze various search methods, and compare how many times

### Code implementation (1)

```	//Preorder traversal
public HeroNode preOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
//Medium order traversal
public HeroNode infixOrderSearch(int no) {
if (root!=null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
//Postorder traversal
public HeroNode postOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}```
```	//Preorder traversal search
/*no Find no
*If found, the Node is returned. If not found, null is returned
* */
public HeroNode preOrdersearch(int no) {
//Compare whether the current node is
if (this.no==no) {
return this;
}
//1. Judge whether the left child node of the current node is empty. If not, recursive preorder search
//2. If the left recursive preamble finds a node, it returns
HeroNode resNode=null;
if (this.left!=null) {
resNode=preOrdersearch(no);
}
if (resNode!=null) {//That means we found the left subtree
return resNode;
}
//1. Left recursive preorder search. If a node is found, it returns. Continue to judge whether or not
//2. Whether the right child node of the current node is empty. If it is not empty, continue to search in the right recursive preamble
if (this.right!=null) {
resNode=this.right.preOrdersearch(no);
}
return resNode;
}
//Middle order traversal search
public HeroNode infixOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, search in recursive middle order
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.infixOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}
//If found, it returns. If not found, it is compared with the current node. If yes, it returns the current node
if (this.no==no) {
return this;
}

//Otherwise, continue the middle order search of right recursion
if (this.right!=null) {
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}

//Postorder traversal search
public HeroNode postOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, it will be searched recursively
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.postOrderSearch(no);
}
if (resNode!=null) {//Description found in the left subtree
return resNode;
}

//If the left subtree is not found, the right subtree recursively performs a post order traversal search
if (this.right!=null) {
resNode=this.right.postOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}

//If the left and right subtrees are not found, compare the current node
if (this.no==no) {
return this;
}
return resNode;
}```
```		//Preorder traversal
//Number of preorder traversals: 4
System.out.println("Preorder traversal mode~~~");
HeroNode resNode=binaryTree.preOrderSearch(5);
if (resNode!=null) {
System.out.printf("Yes, the information is no=%d name=%s",resNode.getNo(),resNode.getName());
}else {
System.out.printf("Can't find no=%d Hero of",5);
}

//Medium order traversal
//Middle order traversal: 3 times
System.out.println("Middle order traversal mode~~~");
HeroNode resNode=binaryTree.infixOrderSearch(5);
if (resNode!=null) {
System.out.printf("Yes, the information is no=%d name=%s",resNode.getNo(),resNode.getName());
}else {
System.out.printf("Can't find no=%d Hero of",5);
}

//Postorder traversal search
//Number of subsequent traversal Searches: 5
System.out.println("postorder traversal ~~~");
HeroNode resNode=binaryTree.postOrderSearch(5);
if (resNode!=null) {
System.out.printf("Yes, the information is no=%d name=%s",resNode.getNo(),resNode.getName());
}else {
System.out.printf("Can't find no=%d Hero of",5);
}```

### Complete code

```package tree;

public class BinaryTreeDemo {

public static void main(String[] args) {
//First you need to create a binary tree
BinaryTree binaryTree=new BinaryTree();
//Create required nodes
HeroNode root=new HeroNode(1, "Song Jiang");
HeroNode node2=new HeroNode(2, "Wu Yong");
HeroNode node3=new HeroNode(3, "Lu Junyi");
HeroNode node4=new HeroNode(4, "Lin Chong");
HeroNode node5=new HeroNode(5, "Guan Sheng");

//It shows that we first create the binary tree manually, and then we learn to create the binary tree recursively
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);

//test
System.out.println("Preorder traversal");//1,2,3,5,4
binaryTree.preOrder();

System.out.println("Medium order traversal");//2,1,5,3,4
binaryTree.infixOrder();

System.out.println("Postorder traversal");//2,5,4,3,1
binaryTree.postOrder();

//Preorder traversal
//Number of preorder traversals: 4
/*	System.out.println("Preorder traversal method (~ ~ ");
HeroNode resNode=binaryTree.preOrderSearch(5);
if (resNode!=null) {
System.out.printf("Found, the information is no =% D, name =% s ", resnode. Getno(), resnode. Getname());
}else {
System.out.printf("No hero found for no=%d ", 5);
}*/

//Medium order traversal
//Middle order traversal: 3 times
/*		System.out.println("Middle order traversal mode ~ ~ ");
HeroNode resNode=binaryTree.infixOrderSearch(5);
if (resNode!=null) {
System.out.printf("Found, the information is no =% D, name =% s ", resnode. Getno(), resnode. Getname());
}else {
System.out.printf("No hero found for no=%d ", 5);
}*/

//Postorder traversal search
//Number of subsequent traversal Searches: 5
System.out.println("postorder traversal ~~~");
HeroNode resNode=binaryTree.postOrderSearch(5);
if (resNode!=null) {
System.out.printf("Yes, the information is no=%d name=%s",resNode.getNo(),resNode.getName());
}else {
System.out.printf("Can't find no=%d Hero of",5);
}
}

}

//Define BinaryTree
class BinaryTree{
private HeroNode root;

public void setRoot(HeroNode root) {
this.root=root;
}
//Preorder traversal
public void preOrder() {
if (this.root!=null) {
this.root.preOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}
//Medium order traversal
public void infixOrder() {
if (this.root!=null) {
this.root.infixOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}
//Postorder traversal
public void postOrder() {
if (this.root!=null) {
this.root.postOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}

//Preorder traversal
public HeroNode preOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
//Medium order traversal
public HeroNode infixOrderSearch(int no) {
if (root!=null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
//Postorder traversal
public HeroNode postOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
}
//Create the HeroNode node first
class HeroNode{
private int no;
private String name;
private HeroNode left;//Default null
private HeroNode right;//Default null
public HeroNode(int no,String name) {
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
//Method of writing preorder traversal
public void preOrder() {
System.out.println(this);//Output parent node first
//Recursive pre order traversal to the left subtree
if (this.left!=null) {
this.left.preOrder();
}
//Recursive right subtree preorder traversal
if (this.right!=null) {
this.right.preOrder();
}
}
//Medium order traversal
public void infixOrder() {
//Recursive traversal to the middle order of the left subtree
if (this.left!=null) {
this.left.infixOrder();
}
//Output parent node
System.out.println(this);
//Recursive traversal in right subtree
if (this.right!=null) {
this.right.infixOrder();
}
}
//Postorder traversal
public void postOrder() {
if (this.left!=null) {
this.right.postOrder();
}
if (this.right!=null) {
this.right.postOrder();
}
System.out.println(this);
}

//Preorder traversal search
/*no Find no
*If found, the Node is returned. If not found, null is returned
* */
public HeroNode preOrdersearch(int no) {
System.out.println("Enter preorder traversal");
//Compare whether the current node is
if (this.no==no) {
return this;
}
//1. Judge whether the left child node of the current node is empty. If not, recursive preorder search
//2. If the left recursive preamble finds a node, it returns
HeroNode resNode=null;
if (this.left!=null) {
resNode=preOrdersearch(no);
}
if (resNode!=null) {//That means we found the left subtree
return resNode;
}
//1. Left recursive preorder search. If a node is found, it returns. Continue to judge whether or not
//2. Whether the right child node of the current node is empty. If it is not empty, continue to search in the right recursive preamble
if (this.right!=null) {
resNode=this.right.preOrdersearch(no);
}
return resNode;
}
//Middle order traversal search
public HeroNode infixOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, search in recursive middle order
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.infixOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}
//If found, it returns. If not found, it is compared with the current node. If yes, it returns the current node
if (this.no==no) {
return this;
}

//Otherwise, continue the middle order search of right recursion
if (this.right!=null) {
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}

//Postorder traversal search
public HeroNode postOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, it will be searched recursively
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.postOrderSearch(no);
}
if (resNode!=null) {//Description found in the left subtree
return resNode;
}

//If the left subtree is not found, the right subtree recursively performs a post order traversal search
if (this.right!=null) {
resNode=this.right.postOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}
System.out.println("Enter post order search");
//If the left and right subtrees are not found, compare the current node
if (this.no==no) {
return this;
}
return resNode;
}
}```

## Binary tree - delete node

### requirement

1) If the deleted node is a leaf node, delete the node

2) If the deleted node is a non leaf node, the subtree is deleted

3) Test, delete No. 5 leaf node and No. 3 subtree

### Finish deleting node

regulations:

1) If the deleted node is a leaf node, delete the node

2) If the deleted node is a non leaf node, the subtree is deleted

thinking

First:

Consider that if the tree is an empty tree root, if there is only one root node, it is equivalent to setting the binary tree empty

//Then proceed to the following steps

1. Because our binary tree is unidirectional, we judge whether the child node of the current node needs to be deleted, but not whether the current node needs to be deleted

2. If the left child node of the current node is not empty and the left child node is to delete the node, set this.left=null; And return (end recursive deletion)

3. If the right child node of the current node is not empty and the right child node is to delete the node, set this.right.=null; And return (end recursive deletion)

4. If the node is not deleted in step 2 and step 3, we need to recursively delete the right subtree

5. If the node is not deleted in step 4, it should be deleted recursively to the right subtree

### code implementation

```		//Test a delete node
System.out.println("Pre sequence traversal before deletion");
binaryTree.preOrder();//1,2,3,5,4
binaryTree.delNode(3);
System.out.println("After deletion, the preamble traverses");
binaryTree.preOrder();//1,2,3,4```
```	//Delete Vertex
public void delNode(int no) {
if (root!=null) {
//If there is only one root node, judge whether root is to delete the node immediately
if (root.getNo()==no) {
root=null;
}else {
//Recursive deletion
root.delNode(no);
}
}else {
System.out.println("The tree is empty and cannot be deleted~");
}
}```
```	//Recursively delete nodes
//1) If the deleted node is a leaf node, delete the node
//2) If the deleted node is a non leaf node, the subtree is deleted
public void delNode(int no) {
//thinking
/*
* 1.Because our binary tree is unidirectional, we judge whether the child nodes of the current node need to be deleted,
You cannot judge whether the current node needs to be deleted

2.If the left child node of the current node is not empty and the left child node is to delete the node,
this.left=null; And return (end recursive deletion)

3.If the right child node of the current node is not empty and the right child node is the node to be deleted,
this.right.=null; And return (end recursive deletion)

4.If the node is not deleted in step 2 and step 3, we need to delete the right subtree recursively

5.If the node is not deleted in step 4, it should be deleted recursively to the right subtree*/
//2. If the left child node of the current node is not empty and the left child node is to delete the node,
Will this.left=null;And return(End recursive deletion)
if (this.left!=null&&this.left.no==no) {
this.left=null;
return;
}
//3. If the right child node of the current node is not empty and the right child node is to delete the node,
Will this.right.=null;And return(End recursive deletion)
if (this.right!=null&&this.right.no==no) {
this.right=null;
return;
}
//4. We need to delete recursively to the left subtree
if (this.left!=null) {
this.left.delNode(no);
return;
}
//5. The right subtree should be deleted recursively
if (this.right!=null) {
this.right.delNode(no);
}
}```

### Complete code

```package tree;

public class BinaryTreeDemo {

public static void main(String[] args) {
//First you need to create a binary tree
BinaryTree binaryTree=new BinaryTree();
//Create required nodes
HeroNode root=new HeroNode(1, "Song Jiang");
HeroNode node2=new HeroNode(2, "Wu Yong");
HeroNode node3=new HeroNode(3, "Lu Junyi");
HeroNode node4=new HeroNode(4, "Lin Chong");
HeroNode node5=new HeroNode(5, "Guan Sheng");

//It shows that we first create the binary tree manually, and then we learn to create the binary tree recursively
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);

//test
/*	System.out.println("Preorder traversal "; / / 1,2,3,5,4
binaryTree.preOrder();

System.out.println("Middle order traversal "")// 2,1,5,3,4
binaryTree.infixOrder();

System.out.println("Postorder traversal "; / / 2,5,4,3,1
binaryTree.postOrder();*/

//Preorder traversal
//Number of preorder traversals: 4
/*	System.out.println("Preorder traversal method (~ ~ ");
HeroNode resNode=binaryTree.preOrderSearch(5);
if (resNode!=null) {
System.out.printf("Found, the information is no =% D, name =% s ", resnode. Getno(), resnode. Getname());
}else {
System.out.printf("No hero found for no=%d ", 5);
}*/

//Medium order traversal
//Middle order traversal: 3 times
/*		System.out.println("Middle order traversal mode ~ ~ ");
HeroNode resNode=binaryTree.infixOrderSearch(5);
if (resNode!=null) {
System.out.printf("Found, the information is no =% D, name =% s ", resnode. Getno(), resnode. Getname());
}else {
System.out.printf("No hero found for no=%d ", 5);
}*/

//Postorder traversal search
//Number of subsequent traversal Searches: 5
/*	System.out.println("Post order traversal method (~ ~ ");
HeroNode resNode=binaryTree.postOrderSearch(5);
if (resNode!=null) {
System.out.printf("Found, the information is no =% D, name =% s ", resnode. Getno(), resnode. Getname());
}else {
System.out.printf("No hero found for no=%d ", 5);
}*/

//Test a delete node
System.out.println("Pre sequence traversal before deletion");
binaryTree.preOrder();//1,2,3,5,4
binaryTree.delNode(3);
System.out.println("After deletion, the preamble traverses");
binaryTree.preOrder();//1,2,3,4
}

}

//Define BinaryTree
class BinaryTree{
private HeroNode root;

public void setRoot(HeroNode root) {
this.root=root;
}
//Delete Vertex
public void delNode(int no) {
if (root!=null) {
//If there is only one root node, judge whether root is to delete the node immediately
if (root.getNo()==no) {
root=null;
}else {
//Recursive deletion
root.delNode(no);
}
}else {
System.out.println("The tree is empty and cannot be deleted~");
}
}
//Preorder traversal
public void preOrder() {
if (this.root!=null) {
this.root.preOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}
//Medium order traversal
public void infixOrder() {
if (this.root!=null) {
this.root.infixOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}
//Postorder traversal
public void postOrder() {
if (this.root!=null) {
this.root.postOrder();
}else {
System.out.println("Binary tree is empty and cannot be traversed");
}
}

//Preorder traversal
public HeroNode preOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
//Medium order traversal
public HeroNode infixOrderSearch(int no) {
if (root!=null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
//Postorder traversal
public HeroNode postOrderSearch(int no) {
if (root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
}
//Create the HeroNode node first
class HeroNode{
private int no;
private String name;
private HeroNode left;//Default null
private HeroNode right;//Default null
public HeroNode(int no,String name) {
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}

//Recursively delete nodes
//1) If the deleted node is a leaf node, delete the node
//2) If the deleted node is a non leaf node, the subtree is deleted
public void delNode(int no) {
//thinking
/*
* 1.Because our binary tree is one-way, we judge whether the child nodes of the current node need to delete nodes, but not whether the current node needs to delete nodes

2.If the left child node of the current node is not empty and the left child node is to delete the node, set this.left=null; And return (end recursive deletion)

3.If the right child node of the current node is not empty and the right child node is to delete the node, set this.right.=null; And return (end recursive deletion)

4.If the node is not deleted in step 2 and step 3, we need to delete the right subtree recursively

5.If the node is not deleted in step 4, it should be deleted recursively to the right subtree*/
//2. If the left child node of the current node is not empty and the left child node is to delete the node, set this.left=null; And return (end recursive deletion)
if (this.left!=null&&this.left.no==no) {
this.left=null;
return;
}
//3. If the right child node of the current node is not empty and the right child node is to delete the node, set this.right.=null; And return (end recursive deletion)
if (this.right!=null&&this.right.no==no) {
this.right=null;
return;
}
//4. We need to delete recursively to the left subtree
if (this.left!=null) {
this.left.delNode(no);
return;
}
//5. The right subtree should be deleted recursively
if (this.right!=null) {
this.right.delNode(no);
}
}
//Method of writing preorder traversal
public void preOrder() {
System.out.println(this);//Output parent node first
//Recursive pre order traversal to the left subtree
if (this.left!=null) {
this.left.preOrder();
}
//Recursive right subtree preorder traversal
if (this.right!=null) {
this.right.preOrder();
}
}
//Medium order traversal
public void infixOrder() {
//Recursive traversal to the middle order of the left subtree
if (this.left!=null) {
this.left.infixOrder();
}
//Output parent node
System.out.println(this);
//Recursive traversal in right subtree
if (this.right!=null) {
this.right.infixOrder();
}
}
//Postorder traversal
public void postOrder() {
if (this.left!=null) {
this.right.postOrder();
}
if (this.right!=null) {
this.right.postOrder();
}
System.out.println(this);
}

//Preorder traversal search
/*no Find no
*If found, the Node is returned. If not found, null is returned
* */
public HeroNode preOrdersearch(int no) {
System.out.println("Enter preorder traversal");
//Compare whether the current node is
if (this.no==no) {
return this;
}
//1. Judge whether the left child node of the current node is empty. If not, recursive preorder search
//2. If the left recursive preamble finds a node, it returns
HeroNode resNode=null;
if (this.left!=null) {
resNode=preOrdersearch(no);
}
if (resNode!=null) {//That means we found the left subtree
return resNode;
}
//1. Left recursive preorder search. If a node is found, it returns. Continue to judge whether or not
//2. Whether the right child node of the current node is empty. If it is not empty, continue to search in the right recursive preamble
if (this.right!=null) {
resNode=this.right.preOrdersearch(no);
}
return resNode;
}
//Middle order traversal search
public HeroNode infixOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, search in recursive middle order
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.infixOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}
//If found, it returns. If not found, it is compared with the current node. If yes, it returns the current node
if (this.no==no) {
return this;
}

//Otherwise, continue the middle order search of right recursion
if (this.right!=null) {
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}

//Postorder traversal search
public HeroNode postOrderSearch(int no) {
//Judge whether the left child node of the current node is empty. If it is not empty, it will be searched recursively
HeroNode resNode=null;
if (this.left!=null) {
resNode=this.left.postOrderSearch(no);
}
if (resNode!=null) {//Description found in the left subtree
return resNode;
}

//If the left subtree is not found, the right subtree recursively performs a post order traversal search
if (this.right!=null) {
resNode=this.right.postOrderSearch(no);
}
if (resNode!=null) {
return resNode;
}
System.out.println("Enter post order search");
//If the left and right subtrees are not found, compare the current node
if (this.no==no) {
return this;
}
return resNode;
}
}```

### Operation results

Posted on Sun, 05 Dec 2021 02:26:43 -0500 by reag27