Java creates a binary search tree to realize the operation examples of search, insertion and deletion

Binary search tree implemented in Java, and search, insert and delete the tree (merge, delete, copy and delete)

First, we should have a coding idea, which is roughly as follows:

1. Search: according to the data characteristics of binary search tree, we can search according to the value comparison of nodes. When the search value is greater than the current node, go to the right, otherwise go to the left!

2. Insert: we should know that all inserted leaf nodes are leaf nodes, so we need to find the location of the leaf node to be inserted. The idea of inserting is the same as that of searching.

3. Delete:

翻译错误 TIMEOUT

2) Copy delete: copy delete is a relatively simple delete operation and the most commonly used delete operation. There are also three situations: when the current node has no left subtree and has a right subtree, let the root node of the current right subtree replace the deleted node; When the current node has no right subtree and has a left subtree, let the root node of the current left subtree replace the deleted node; When the current deleted node has both left and right subtrees, we need to find the avatar of the deleted node. We can find the rightmost node in the left subtree of the deleted node, assign the value of this node to the deleted node, and then don't forget to make the "pointer" pointed by the parent node of this avatar node to the avatar null, (in fact, it doesn't matter in Java. There is a garbage disposal mechanism to process it automatically). You can also use the leftmost node of the right subtree of the currently deleted node as a substitute node to realize this process.

Let's start with the code.

The first is the ## binary search tree node class##

Implement binary search tree

Test class

All right, that's it!

The above example of creating binary search tree in Java to realize search, insertion and deletion is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>