## Introduction to binary sort tree

## Creation of binary sort tree (addition of nodes)

From the definition of binary sort tree, its premise is binary tree, and it is defined in a recursive way. Its nodes meet a partial order relationship. The value of the root node of the left child tree must be smaller than that of the parent node, and the value of the root node of the right child tree must be larger than that of the parent node.

The purpose of constructing such a tree is to improve the sorting speed. If we traverse the binary tree sorting tree, we can find that the obtained sequence is an increasing sequence.

Node addition of a binary sort tree refers to adding a given value to a position in the tree after generating a node, and maintaining that the tree is still a binary sort tree.

### Algorithm principle

For the node to be added, start from the root node and judge it in four cases:

1) If it is an empty tree, create a node with the value of node and return it;

2) The value of node is equal to the value of the root node of the tree. You do not need to add, but directly return to the root node;

3) If the value of node is less than the value of the root node of the tree, the adding position must be in the left subtree. The process of adding the left subtree is executed recursively, and the adding result is returned as a new left subtree;

4) If the value of node is greater than the value of the root node of the tree, the addition position must be in the right subtree. The process of adding the right subtree is executed recursively, and the addition result is returned as a new right subtree;

/** * Method of adding nodes */ public void add(Node node) { if (root == null) { root = node;// If root is empty, direct root to node } else { root.add(node); } } /** * The method of adding nodes is recursive. Note that the requirements of binary tree sorting tree should be met * * @param node */ public void add(Node node) { if (node == null) { return; } // Judge the value of the incoming node and the value relationship with the root node of the current subtree if (node.value < this.value) { // If the left child node bit of the current node is null if (this.left == null) { this.left = node; } else { // Recursively add to the left subtree this.left.add(node); } } else {// The value of the added node is greater than the value of the current node if (this.right == null) { this.right = node; } else { // Recursive right subtree addition this.right.add(node); } } }

## Traversal of binary sort tree

The middle order traversal of a binary sort tree is the most commonly used. The middle order traversal of a binary sort tree is an increasing sequence.

The increasing sequence has monotonicity, so we can use this property to find the k-th node of the tree in an effective time.

## Search of binary sort tree

For the tree node to be found, start from the root node, and judge it in four cases:

1) If it is an empty tree, null is returned directly;

2) If the value of node is equal to the value of the root node of the tree, it is returned directly;

3) If the value of node is less than the value of the root node of the tree, it means that the node corresponding to node is neither in the root node nor on the right subtree, then the search result of the left subtree will be returned recursively;

4) If the value of node is greater than the value of the root node of the tree, it means that the node corresponding to node is neither in the root node nor on the left subtree, then the search result of the right subtree will be returned recursively;

// Find node public Node search(int value) { if (root == null) { return null; } else { return root.search(value); } } /** * Find node */ public Node search(int value) { if (value == this.value) {// This node is found return this; } else if (value < this.value) {// If the searched value is less than the current node, the left subtree will be searched recursively // If the left child node is empty if (this.left == null) { return null; } return this.left.search(value); } else {// If the searched value is not less than the current node, the right subtree will be searched recursively if (this.right == null) { return null; } return this.right.search(value); } }

## Node deletion of binary tree

### Scenario 1: delete leaf node

1) You need to find the targetNode to delete first

2) Find parent node of targetNode

3) Determines whether the targetNode is the left child node or the right child node of the parent

4) Delete according to the previous situation: the left child node parent.left = null; Right child node parent.right = null

### Case 2: delete a node with only one subtree

1) You need to find the targetNode to delete first

2) Find parent node of targetNode

3) Determines whether the child node of the targetNode is a left child node or a right child node

4) Is the targetNode the left child node or the right child node of the parent

5) If the targetNode has a left child node

5.1 if targetNode is the left child node of parent: parent.left = targetNode.left;

5.2 if targetNode is the right child node of parent: parent.right = targetNode.left;

6) If the targetNode has a right child node

6.1 if targetNode is the left child node of parent: parent.left = targetNode.right;

6.2 if targetNode is the right child node of parent: parent.right = targetNode.right;

### Case 3: delete nodes of two subtrees

1) You need to find the targetNode to delete first

2) Find the smallest node from the right subtree of targetNode

3) Save the value of the smallest node with a temporary variable

4) Delete the minimum node

5)targetNode.value = temp

As shown in the figure, the following figure shows the process of deleting root node 5 from this tree. First, because it has left and right subtree nodes, find the smallest node 6 from the right subtree, save the value of node 6 and delete node 6.

Note: there is also a deletion idea to find the largest for the left subtree.

### Before introducing the node deletion algorithm of binary sort tree, we first need to know the following three methods:

1) Find nodes to delete

// Find nodes to delete public Node search(int value) { if (root == null) { return null; } else { return root.search(value); } } /** * Find nodes to delete */ public Node search(int value) { if (value == this.value) {// This node is found return this; } else if (value < this.value) {// If the searched value is less than the current node, the left subtree will be searched recursively // If the left child node is empty if (this.left == null) { return null; } return this.left.search(value); } else {// If the searched value is not less than the current node, the right subtree will be searched recursively if (this.right == null) { return null; } return this.right.search(value); } }

2) Find parent node

// Find parent node public Node searchParent(int value) { if (root == null) { return null; } else { return root.searchParent(value); } } /** * Find the parent node of the node you want to delete * * @param value The value of the node to find * @return The parent node of the node to be deleted is returned. If not, null is returned */ public Node searchParent(int value) { // If the current node is the parent node of the node to be deleted, return if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) { return this; } else { // If the searched value is less than the value of the current node, and the left child node of the current node is not empty if (value < this.value && this.left != null) { return this.left.searchParent(value);// Left subtree recursive lookup } else if (value >= this.value && this.right != null) { return this.right.searchParent(value);// Recursive search of right subtree } else { return null;// Parent node not found } } }

3) Return minimum node

/** * 1.Returns the value of the minimum node of the binary sort tree with node as the root node. 2. Delete the minimum node of the binary sort tree with node as the root node * * @param node Incoming node (as the root node of binary sort tree) * @return Returns the value of the smallest node of a binary sort tree with node as the root node */ public int delRightTreeMin(Node node) { Node target = node; // Loop to find the left child node, and the minimum value will be found while (target.left != null) { target = target.left; } // At this point, the target points to the smallest node // Delete minimum node delNode(target.value); return target.value; }

### The final method of deleting nodes

// Delete node public void delNode(int value) { if (root == null) { return; } else { // 1. Find the targetNode to delete Node targetNode = search(value); // 2. If the node to be deleted is not found if (targetNode == null) { return; } // If it is found that the current binary sort tree has only one node if (root.left == null && root.right == null) { root = null; return; } // To find the parent node of targetNode Node parent = searchParent(value); // If the node to be deleted is a leaf node if (targetNode.left == null && targetNode.right == null) { if (parent.left != null && parent.left.value == value) {// Is the right child node parent.left = null; } else if (parent.right != null && parent.right.value == value) {// Is the left child node parent.right = null; } } else if (targetNode.left != null && targetNode.right != null) {// Delete a node with two subtrees int minVal = delRightTreeMin(targetNode.right); targetNode.value = minVal; } else {// Delete a node with only one subtree // If the targetNode has a left child node if (targetNode.left != null) { if (parent != null) { // If the targetNode is the left child node of the parent if (parent.left.value == value) { parent.left = targetNode.left; } else { // targetNode is the right child node of the parent parent.right = targetNode.left; } } else { root = targetNode.left; } } else {// If the targetNode has a right child node if (parent != null) { // If the targetNode is the left child node of the parent if (parent.left.value == value) { parent.left = targetNode.right; } else {// If the targetNode is the right child node of the parent parent.right = targetNode.right; } } else { root = targetNode.right; } } } } }

## Summary of binary sort tree

Looking at the search, insertion and deletion of binary sort tree. It completely depends on the shape of the binary sort tree. If it is a complete binary tree or close to a complete binary tree, the three processes are O(log_2_n). If it is a skew tree, the three processes approximately operate on the linear table, which is O(n)