C language implementation of binary sort tree

Source: Internet
Author: User
Tags sort

Introduction to Binary sort tree

Binary sort trees (Binary sort tree, abbreviated BST), also known as binary lookup trees, are the basis of red-black trees, AVL trees and so on. It is either an empty tree or a binary tree with the following properties:

1. If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;

2. If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

3, its left and right subtree are also two-fork sorting tree.

The following tree is a two-fork sort tree:

It is obvious that an ordered sequence can be obtained by the sequential traversal of the two-fork sort tree, and the elements in the ordered sequence are arranged in order from small to large, so a disordered sequence may be transformed into an ordered sequence by constructing a binary sort tree.

Binary sort tree related operations

Binary sort trees usually have operations such as find, insert, delete, and so on. Find operation is very simple, nothing more than recursive lookup, somewhat similar to the process of binary tree traversal. Insert operation is not difficult, usually first in the binary sort tree ptree to find out if there is equal to the given value of the element, if not found, then insert the given value into the two-fork sorting tree, but to ensure that the inserted tree remains a two-fork sorting tree. In this way, the new node is inserted in a unique location, and the newly inserted node must be a newly added leaf node and is the left or right child who finds the last node accessed on the path when it is unsuccessful. The binary sort tree is a dynamic tree because of its characteristic of inserting nodes in the lookup process.

Before giving the specific code of each operation, we should look at the deletion of the two-fork sort tree in detail, the deletion operation is more difficult than the other operation of the binary sort tree, but it is only relative to other operations of its own, but it is also very easy to understand. Less gossip, the following is a concrete analysis of the binary sorting tree delete operation.

We assume that the node to be deleted in the binary sort tree is P (that is, the node to which P points, below), the parent node is F, and of course the node p may be the left child or the right child of node F, but in each of the following situations, you will find that neither the left child nor the right child affects the versatility of the deletion operation. Obviously, the delete operation is divided into the following 3 cases:

1, if the deletion point P is a leaf node, then delete p, and will not destroy the whole tree structure, so just make p=null can.

2, if the deleted point P only Zuozi or right subtree, then just the left subtree or right subtree back to the parent node of P, that is, do the following: P=p->lchild or P=p->rchild.

3, if the deleted point P both Zuozi and right subtree, obviously not as simple as the above two situations. We want to make the node p deleted, the structure of the binary sort tree is unchanged, it needs to do some operations on its subtree, and only need to operate a subtree, Operation Zuozi and operation of the right subtree similar ideas, we here to operate the left subtree as an example to implement node p delete operation, And the following diagram to do a specific analysis (in the figure triangle represents the node's left subtree or right subtree).

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.