One-step write algorithm (hash Binary Tree)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Friends who have used a balanced binary tree know that the biggest advantage of a balanced binary tree is sorting. Data Sorting should be taken into account when data is inserted or deleted. However, data query is as important as data addition and deletion. Unfortunately, due to the addition and deletion of nodes, the efficiency of data query is very low. Let's take a look at the following extreme scenario. All the branch nodes have only one side of the data:

 

 

/*

* 7 3

*/\

* 6 4

*/\

* 5 7

*/\

* 2 12

*/\

* 1 20

*/

/*

* 7 3

*/\

* 6 4

*/\

* 5 7

*/\

* 2 12

*/\

* 1 20

*/

 

 

The above figure illustrates the problem. Although the query 7 and 6 are very convenient, the efficiency of the query 5, 2, and 1 is very low. This is also the case of the binary tree on the right. Is there a way to make the search efficiency between data not significantly different? Up to now, there are three main methods:

 

(1) hash Binary Tree

 

(2) avl Tree

 

(3) red and black trees

 

Today, we will focus on the hash tree. The other two will be introduced in the blog.

 

So what is a hash tree? In fact, it is also very simple, that is, we add a next pointer to the binary tree node and create a hash table at the same time, in this way, we can directly use hash queries instead of balanced binary tree queries when querying data. Generally, the nodes of the hash tree are defined as follows:

 

 

Typedef struct _ HASH_TREE

{

Int data;

Struct _ HASH_TREE * next;

Struct _ HASH_TREE * left;

Struct _ HASH_TREE * right;

} HASH_TREE;

Typedef struct _ HASH_TREE

{

Int data;

Struct _ HASH_TREE * next;

Struct _ HASH_TREE * left;

Struct _ HASH_TREE * right;

} HASH_TREE; in fact, compared to a common balanced binary tree, there is only one more next pointer. When will this next pointer be processed? It is mainly handled when adding or deleting nodes.

 

 

STATUS add_node_cmd_tree (HASH_TREE ** ppHash, int data)

{

 

/* Add hash node into tree */

 

/* Add hash node into hash table */

 

Return TRUE;

}

STATUS add_node_cmd_tree (HASH_TREE ** ppHash, int data)

{

 

/* Add hash node into tree */

 

/* Add hash node into hash table */

 

Return TRUE;

} The added code is similar in the deletion process.

 

 

STATUS delete_node_from_tree (HASH_TREE ** ppHash, int data)

{

HASH_TREE * pNode;

/* Delete hash node from tree, but not free space */

/* Delete hash node from hash table */

Free (pNode );

Return TRUE;

}

STATUS delete_node_from_tree (HASH_TREE ** ppHash, int data)

{

HASH_TREE * pNode;

/* Delete hash node from tree, but not free space */

/* Delete hash node from hash table */

Free (pNode );

Return TRUE;

}

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.