# Binary Tree Pre-, Medium-, Post-order Queries and Binary Tree Deletions (Including Binary Tree Pre-, Medium-, and Post-order Traversal)

Posted by objNoob on Fri, 22 May 2020 05:05:29 +0200

Binary Tree, Pre-order Query, Ideas (Intermediate and Follow-up Ideas are similar):

First, define a method that returns Node, passing a parameter to be queried, public Node preOrderSearch(int value){...}

1. Determine if the value of the root node is equal to the value to be queried, and if so return -if(This.value== value) {return this;}

2. If the root node judges differently, define a class variable to determine whether the left and right subtrees are queried for the value to be found, and return a value for the defined method.Node resNode = null;

3. Determine if the left subtree is empty or not, call the left subtree lookup recursively. If the left subtree is found recursively, assign the result of the lookup to resNode

4. Determine whether the class variable resNode is empty or not. If not, the left subtree is found and returned.

5. If the left subtree is not found and resNode is not returned, then neither the root node nor the left subtree is queried for the value to be queried.

Determines if the right subtree is not empty then recursively searches, finds and assigns the result to resNode

6. If the right subtree finds the value to look for, resNode will accept the return; if the root node, left subtree, right subtree cannot find the value to query, it will directly return the defined class variable (because the class variable is assigned a null)

Binary Tree Delete Node Ideas:

First, if the root node is empty, it returns directly to "Binary tree is empty and cannot be deleted". If not, it determines whether the root node is a node to be deleted, it empties the root node root = null; otherwise, it calls the recursive deletion method.

1. If the left subtree is not empty and the value of the left subtree is equal to the value to be deleted, leave the left subtree empty and return

2. If the right subtree is not empty and the value of the right subtree is equal to the value to be deleted, leave the right subtree empty and return

3. If neither Step 1 nor Step 2 has deleted a value, the left subtree is deleted recursively

3. Recursive right subtree deletion if no value has been deleted in step 3

Implementation code:

``````package BinaryTree;
/*
* 1,Forward, middle and back traversal of a binary tree
* 2,Forward, Middle and Backward Searches for Binary Trees
* 3,Forward, middle, and back deletions of binary trees
*/
public class BinaryTreeDemo1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree binaryTree = new BinaryTree();

HereNode root = new HereNode(1,"Song River");
HereNode node2 = new HereNode(2,"Wu Yong");
HereNode node3 = new HereNode(3,"Lu Junyi");
HereNode node4 = new HereNode(4,"Zhou De");
HereNode node5 = new HereNode(5,"Lin Haiyu");

root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);

binaryTree.setRoot(root);//Set Parent Node
//
//		binaryTree.preOrder(); //Call prefix traversal, output 1, 2, 3, 5, 4
//		binaryTree.infixOrder(); //Intermediate traversal: Output 2, 1, 5, 3, 4,
//		binaryTree.postOrder(); //Postorder traversal: Output 2, 5, 4, 3, 1
//
//		//--------------------------------
//		HereNode resNode = binaryTree.preOrderSearch(5);
//		//HereNode resNdoe = new HereNode(); //The lookup method in HereNode needs to be called, so instantiate it
//		//resNode =BinaryTree.preOrderSearch(no); //Call the prefix lookup in Here, need to pass a parameter to find
//		if(resNode != null) {
//			System.out.printf("found, information no =%d name =%s"),ResNode.getNo()ResNode.getName());
//		} else {
//			System.out.printf( No, information no =%d, 5);
//		}

//________________________________________________________________

binaryTree.preOrder();//Nodes 1, 2, 3, 5, 4 of Pre-traversal output before deletion
binaryTree.delOrder(3);//Delete the corresponding node of the binary tree
binaryTree.preOrder();//Pre-order traversal after deleting corresponding nodes of binary tree yields 1,2
}

}

//Create Binary Tree
class BinaryTree{
private HereNode root;	//Create parent node

public void setRoot(HereNode root2) {	//Get the incoming parent node
this.root = root2;
}

public void preOrder() {	//If parent node does not traverse in precedence for air conditioning
if(root != null) {
this.root.preOrder();
}
}
public void infixOrder() {	//If parent node does not traverse in precedence for air conditioning
if(root != null) {
this.root.infixOrder();
}
}
public void postOrder() {	//If parent node does not traverse in precedence for air conditioning
if(root != null) {
this.root.postOrder();
}
}

//-----------------------------------

public HereNode preOrderSearch(int no) {	//Preorder Search
if(root != null) {
return this.root.preOrderSearch(no);
} else {
return null;
}
}

public HereNode infixOrderSearch(int no) {	//Search in Middle Order
if(root != null) {
return this.root.infixOrderSearch(no);
} else {
return null;
}
}

public HereNode postOrderSearch(int no) {	//Find in Postorder
if(root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}

//----------------------------------

public void delOrder(int no) {
if(root != null) {			//1. If the root node is not empty
if(root.getNo() == no) {//Determine immediately if the root node is to be deleted
root = null;
} else {				//If the root node is not the one to delete, recursively call the delete method to determine the left and right subtrees
this.root.delNode(no);
}
} else {
System.out.println("The binary tree is empty and cannot be deleted");
}
}
}

//Create HereNode Node

class HereNode{
private int no;
private String name;
private HereNode left;	//Default to null
private HereNode right; //Default to null

public HereNode(int no, String name) {
this.no = no;
this.name = name;
}
//getset method
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 HereNode getLeft() {
return left;
}
public void setLeft(HereNode left) {
this.left = left;
}
public HereNode getRight() {
return right;
}
public void setRight(HereNode right) {
this.right = right;
}

@Override
public String toString() {	//Override toString method
return "HereNode [no=" + no + ", name=" + name + "]";
}

//_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu_

//Pre-order traversal
public void preOrder() {
System.out.println(this);//Output Current Node
if(this.left != null) {
this.left.preOrder();
}
if(this.right != null) {
this.right.preOrder();
}
}
//Intermediate traversal
public void infixOrder() {
if(this.left != null) {
this.left.infixOrder();
}
System.out.println(this);//Output Current Node
if(this.right != null) {
this.right.infixOrder();
}
}
//Post-order traversal
public void postOrder() {
if(this.left != null) {
this.left.postOrder();
}
if(this.right != null) {
this.right.postOrder();
}
System.out.println(this);//Output Current Node
}

//Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

//Preorder Search
public HereNode preOrderSearch(int no) {
if(this.no == no) {		//Parent Node Judgment
return this;
}
HereNode resNode = null;
if(this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if(resNode != null) {	//Indicates that the left subtree is found and can be returned
return resNode;
}
if(this.right != null) {	//If the left subtree is not found, the right subtree continues to search
resNode = this.right.preOrderSearch(no);
}
return resNode;		//Return to null if you don't find anything at the end
}

//Search in Middle Order
public HereNode infixOrderSearch(int no) {
if(this.no == no) {		//Parent Node Judgment
return this;
}
HereNode resNode = null;
if(this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if(resNode != null) {	//Indicates that the left subtree is found and can be returned
return resNode;
}
if(this.right != null) {	//If the left subtree is not found, the right subtree continues to search
resNode = this.right.infixOrderSearch(no);
}
return resNode;		//Return to null if you don't find anything at the end
}

//Find in Postorder
public HereNode postOrderSearch(int no) {
if(this.no == no) {		//Parent Node Judgment
return this;
}
HereNode resNode = null;
if(this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if(resNode != null) {	//Indicates that the left subtree is found and can be returned
return resNode;
}
if(this.right != null) {	//If the left subtree is not found, the right subtree continues to search
resNode = this.right.postOrderSearch(no);
}
return resNode;		//Return to null if you don't find anything at the end
}

//Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu_
public void delNode(int no) {
if(this.left != null && this.left.no == no) {	//2. If the left child node is not empty and the left child node's no is to be deleted, theThis.left= null empty
this.left = null;
return;
}
if(this.right != null & this.right.no == no) {	//3. If the left child node is not empty and the left child node's no is to be deleted, theThis.left= null empty
this.right = null;
return;
}
if(this.left  != null) {						//4. If 23 steps are not deleted, the left subtree is deleted recursively
this.left.delNode(no);
}
if(this.right != null) {						//5. Delete right subtree if 4 steps are not deleted
this.right.delNode(no);
}
}
}``````