Thinking Logic of computer programs (42)-Sort binary tree

Source: Internet
Author: User

Section 40 describes the hashmap,41 section describes the HashSet, their common implementation mechanism is the hash table, a common limitation is that there is no order, we mentioned that they have a can maintain the order of the corresponding class TreeMap and treeset, the common implementation of the two classes based on the sort of binary tree, In order to understand treemap/treeset better, this section first introduces some basic concepts and algorithms of binary tree sorting.

Basic concepts

First of all, the concept of the tree, in reality, the tree is from the bottom of the long, the tree will fork, in the computer program, generally speaking, in contrast to the reality, the tree is from the top down long, will also fork, there is a root node, each node can have one or more children nodes, no children node is generally called leaf node

A binary tree is a tree, but each node has a maximum of two child nodes, one left and one right, the left is called the left child, and the right side is called the right child, we look at two examples, as follows:


The two trees are two fork tree, the left root node is 5, in addition to the leaf node, each node has two child nodes, the right root node is 7, some nodes have two children nodes, some only one.

The tree has a concept of height or depth, which is the maximum number of nodes from the root to the leaf node, the height of the left tree is 3, and the right is 5.

The sort binary tree is also a binary tree, but it has no repeating elements, and is an ordered two-fork tree, what order is it? For each node:

    • If the left dial hand tree is not empty, all nodes on the left subtree are smaller than that node
    • If the right subtree is not empty, all nodes on the right subtree are larger than the node

The two binary trees above are sorted binary trees. For example, the left tree, the root node is 5, the left is less than 5, the right is greater than 5. Look at the tree on the right, the root node is 7, the left is less than 7, the right is greater than 7, in the left subtree with 3 root, the value of its right subtree is greater than 3.

What are the advantages of sorting binary trees? How do you perform basic operations such as Find, traverse, insert, and delete in a tree? Let's take a look at the basic algorithm.

Basic algorithms

Find

Sorting binary trees has a good advantage in that finding an element is convenient and efficient, and the basic steps are:

    1. First compared to the root node, if the same, you find the
    2. Recursive lookup to record if the root node is small
    3. If it is greater than the root node, recursively finds in the right subtree

This step is similar to the idea of binary searching in an array or binary lookup, if the binary tree is relatively balanced, similar to the left side of the two-fork tree, then each comparison will be able to reduce the comparative range of half, high efficiency.

In addition, in the sort binary tree, you can easily find the minimum maximum, the minimum is the leftmost node, from the root node to find the left child can, the maximum value is the rightmost node, from the root node to find the right child can.

Traverse

Sort binary tree can also be conveniently sequential traversal, recursive way, with the following algorithm can be sequentially traversed:

    1. Visit Zuozi
    2. Accessing the current node
    3. Access to the right sub-tree

For example, traversal accesses the following two-fork tree:


Start from the root node, but first access the left subtree of the root node, to the leftmost node, so the first access is no right subtree, return to the previous layer, Access 3, then access 3 right subtree, 4 no left subtree, so access 4, then 4 right subtree 6, and so on, and so on, the order of access is ordered: 1 3 4 6 7 8 9.

Without recursion, you can also implement sequential traversal, the first node is the leftmost node, starting with the first node, and then looking for the successor node in turn. Given a node, the algorithm to find its successor is:

    • If the node has a right child, the successor is the smallest node in the right subtree.
    • If the node does not have a right child, the successor is the parent node or an ancestor node, looking up from the current node, if it is the right child of the Father node, continue to look for the parent node until it is not the right child or the parent node is empty, the first non-right child node of the Father node is the successor node, if no such ancestor , the traversal ends.

The text description is more abstract, let's take a look at a diagram, for example, each node followed by a green arrow as shown:

For each node, the control algorithm, we explained in detail below:

    • The first node 1 does not have a right child, it is not the right child of the parent node, so its successor is its parent node 3.
    • 3 have right child, the smallest right subtree is 4, so 3 of the successor node is 4.
    • 4 have right child, the right subtree has only one node 6, so 4 of the successor node is 6.
    • 6 No right child, look up the parent node, it is the right child of parent node 4, 4 is the right child of parent node 3, 3 is not the right child of parent node 7, so 6 of the successor node is 3 parent node 7.
    • 7 have right child, the smallest right subtree is 8, so 7 of the successor node is 8.
    • 8 No right child, looking up the parent node, which is not the right child of parent node 9, so its successor is its parent node 9.
    • 9 No right child, look up the parent node, it is the parent node 7 of the right child, and then looked up, but 7 is already the root node, the parent node is empty, so the successor is empty.

How to build a sort binary tree? Can be formed and persisted in the process of inserting, deleting elements.

Insert

In the sort binary tree, insert element first to find the insertion position, that is, the parent node of the new node, how to find it? Similar to finding elements, look down from the root node, with the following steps:

    1. Compared to the current node, if it is the same, the representation already exists and can no longer be inserted.
    2. If it is less than the current node, it is looked for in the left subtree, and if the left dial hand tree is empty, the current node is the parent node to find.
    3. If it is larger than the current node, look in the right subtree, and if the right subtree is empty, the current node is the parent node you are looking for.

Once the parent node is found, it can be inserted, if the insertion element is smaller than the parent, insert as left child or as right child.

Let's take a look at the process of inserting 7, 3, 4, 1, 9, 6, 8, in turn, as shown in the following example:

Delete

Removing a node from a sorted binary tree is a bit more complex, with three scenarios:

    1. Nodes are leaf nodes
    2. Node only one child
    3. Node has two children

Let's take a look at each other.

If the node is a leaf node, it is very simple, can be deleted directly, modify the parent node corresponding to the child is empty.

If the node has only one child node, replace the child node to be truncated, or link directly between the child node and the parent node. For example, in the left binary tree Delete node 4, that is, 4 of the parent node 3 and 4 of the child node 6 directly establish links.

If the node has two children, then first find the successor of the node (according to the following algorithm, followed by the smallest node in the right subtree, the successor is definitely no left child), to find a successor, replace the deletion point for subsequent content, and then delete the successor node. The successor node had no left child, which converted the condition of the two children for the condition of the leaf node or only one child.

For example, in, delete a node from the left binary tree 3,3 has two children, followed by 4, first replacing the content of 3 for 4, and then deleting Node 4.

Balanced sort of binary tree

As can be seen from the previous description, the shape of the binary tree is closely related to the order of insertions and deletions, and in extreme cases, the ordered binary tree may degenerate into a linked list, for example, if the insertion Order is: 1 3 4 6 7 8 9, the sort binary tree shape is:


Degenerate into a linked list, the advantages of sorting binary tree are not, even if not degenerate into a linked list, if the sorting binary tree height imbalance, the efficiency will be very low.

What is the specific definition of balance? There is a highly balanced definition, that is, the height difference of the left and right subtrees of any node is at most one. The ordered binary tree, which satisfies this balance definition, is also known as the AVL tree, which derives its name from its inventor, g.m. Adelson-VElsky and E.M. LAndis, in their algorithm, re-balances the tree with one or more rotation operations when inserting and deleting nodes.

In the implementation of TREEMAP, it is not the AVL tree, but the red and black trees, similar to the AVL tree, the red and black tree is also a balanced sort of binary tree, but also in the insertion and deletion of nodes by rotation operation to balance, but it is not highly balanced, but roughly balanced, so-called roughly refers to, It ensures that, for any path from the root to the leaf node, no path will be longer than twice times the length of the other path. Red and black trees weaken the need for balance, but reduce the cost of maintaining balance, and in practical applications, the statistical performance is higher than the AVL tree.

Why is it called the Red and black tree? Because it shades each node, color or black or red, and has some constraints on the coloring of the nodes, this constraint can be satisfied to ensure that the tree is roughly balanced.

For the AVL tree and the red-black tree, they maintain the balance of the details are more complex, we do not introduce, we need to know is that they are sort of binary tree, all through the insertion and deletion of small overhead rotation operation to maintain the height of the tree balance or roughly balanced, so as to ensure the tree's search efficiency.

Summary

This section describes the basic concepts and algorithms for sorting binary trees.

Sorting binary tree maintains the order of elements, and is a very efficient data structure, the basic preservation, deletion, search efficiency is O (h), H is the height of the tree, in the case of tree balance, H is log2 (n), n is the number of nodes, for example, if n is 1024, then LOG2 (n) is 10.

The basic sort of binary tree can not guarantee the balance of the tree, may degenerate into a linked list, there are many algorithms to maintain the balance of trees, AVL tree is the first, can guarantee the height of the tree balance, but the red and black trees are more widely used in practice, although can only guarantee the approximate balance, but reduce the cost of maintaining the tree balance, the

Like a hash table, a tree is also an important data structure and way of thinking in computer programs. To be able to manipulate data quickly, hashes and trees are two basic ways of thinking that do not require order, prioritize hashing, need order, and consider trees. In addition to the container class Treemap/treeset, the index structure in the database is tree-based (but based on B-trees rather than binary trees), and indexes are key to the ability to quickly access data in large amounts of data.

Understanding the basic concepts and algorithms for sorting binary trees makes it easier to understand treemap and treeset, so let's explore these two classes in the next two sections.

---------------

To be continued, check out the latest articles, please pay attention to the public number "old Horse Programming" (Scan the QR code below), from the introduction to advanced, in layman's words, Lao Ma and you explore the nature of Java programming and computer technology. Original intentions, All rights reserved.

Thinking Logic of computer programs (42)-Sort binary tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.