Binary tree, binary sort tree, red black tree Learn

Source: Internet
Author: User

Binary sort tree is a more useful compromise scheme.
Array search is more convenient, you can directly use subscript, but delete or insert some elements is more troublesome.
The linked list, by contrast, deletes and inserts elements quickly, but looks very slowly.
Binary sort tree has the benefit of both the list and the array.
It is more useful to handle large volumes of dynamic data.

Binary Tree data structure:

struct _bitnode  {      int  data;       *leftchild;       *rightchild;   *pbitree;

Binary tree creation and traversal: http://blog.csdn.net/pony_maggie/article/details/38390513

=======================================================================

Binary sort Tree

Http://www.cnblogs.com/zhuyf87/archive/2012/11/09/2763113.html

The data structure is the same as stated below:

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

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

3. Its left and right sub-trees are also two-fork sorting trees respectively.

Middle sequence traversal (left and right) binary sort tree can get an ordered sequence based on the keyword

Search, INSERT, delete time complexity is equal to tree height, expect O (Logn), the worst O (n) (sequence order, the tree degenerate into linear table, such as right oblique tree).

Although the worst-case efficiency of a binary sort tree is O (n), it supports dynamic lookups, and there are many improved versions of the two-fork sorting tree that can make the tree tall as O (Logn), such as balanced binary-tree AVL, red-black tree, and so on.

=======================================================================

Balanced binary tree AVL (built on a binary sort tree!) )

Http://www.cnblogs.com/fornever/archive/2011/11/15/2249492.html

Balanced binary Tree definition (AVL):

It is either an empty tree, or a two-prong tree with the following properties: The absolute value of the difference in the depth of the Saozi right subtree is not more than 1, and its Saozi right subtree is a balanced binary tree.

Balance factor (BF): The depth of the left subtree of the node minus the depth of the right subtree, then apparently -1<=bf<=1;

To understand, the transformations between the two nodes can continue to balance the rules of a binary tree!

Node 1 and node 2,1 can be used as 2 of the left child, and 2 can be used as 1 right child.

Similarly, 3 can be used as 2 of the right child, and 2 can be used as a 3 left child.

Insert:

root node of the smallest unbalanced subtree: that is, when you are inserting, find the location where you need to insert the node and insert it, looking up (backtracking) from that node, the first unbalanced node, the balance factor BF, becomes-2 or 2.

Why introduce the concept of this least-balanced root node, because at the time of insertion, when the subtree is balanced , the rest of the balance factor will not change, that is, the whole tree is back to balance.

Why is it?

Look at the graph to understand how the unbalanced subtree rotates:

Y is the inserted node, and x is the root of the smallest unbalanced subtree.

The insertion position is left and left

In the first case, the second is understood, because I said the change rule.

In the second case, a and X transform, D becomes X's left child. (because it must be a two-fork sort tree, all nodes of x Zuozi are smaller than x!) )

The transformation means that node A is going to overwrite the memory that stores the node X,

Transformation process:

The transformed nodes are associated with each subtree. 2,

When transforming, node X takes its own child node B, and Node A takes its own child node,-c-y and-D. That

After the transformation, found that node A has three sub-trees, rub, give more D to find a suitable location (by size to find location), placement.

Right-right

Around

It's going to be transformed two times.

Right-left

So how do you find the root node x of the least-balanced subtree and determine if it belongs to that situation?

Look at the original blog post, regardless of the code implementation.

After the decision is made, call the Keepbalance function to keep the two fork tree balanced.

There are deletions and insertions in the original text.

=======================================================================

Red and black Trees

1. If inserting a node causes the tree to be unbalanced, both AVL and rb-tree are required to rotate at most 2 times, that is, both O (1), but in the worst case, when deleting node causes a tree imbalance, AVL needs to maintain the balance of all node from the deleted node to root, so it needs to rotate the magnitude O (Logn), and rb-tree up to 3 rotations, requiring only the complexity of O (1).

2. Second, AVL's structure is more balanced than rb-tree, where inserting and deleting node is more likely to cause tree unbalance, so the AVL needs to rebalance more frequently when large amounts of data need to be inserted or deleted. As a result, rb-tree is more efficient in scenarios that require a lot of insertion and deletion of node. Naturally, AVL's search is more efficient due to the high balance of AVL.

Acjx
Links: http://www.zhihu.com/question/20545708/answer/58717264

The principle and algorithm of red-black tree (I.) Detailed INTRODUCTION

The realization of the C language of red and Black Tree (II.)

Red black Tree (iii) The classic implementation of red and black trees in the Linux kernel

Binary tree, binary sort tree, red black tree Learn

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.