One-step write algorithm (ranking 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]

 

 

 

 

We have discussed the data structure of a two-way linked list. Each cyclic node has two pointers, one pointing to the previous node and the other pointing to the subsequent node, so that all the nodes are put together like a pearl. However, the data structure we discuss today is a little different. It has three nodes. It is defined as follows:

 

 

Typedef struct _ TREE_NODE

{

Int data;

Struct _ TREE_NODE * parent;

Struct _ TREE_NODE * left_child;

Struct _ TREE_NODE * right_child;

} TREE_NODE;

Typedef struct _ TREE_NODE

{

Int data;

Struct _ TREE_NODE * parent;

Struct _ TREE_NODE * left_child;

Struct _ TREE_NODE * right_child;

} TREE_NODE; based on the data structure above, we can see that each data node has three pointers: pointer to the parent, pointer to the left child, and pointer to the right child. Each node is connected by pointers. The link pointer is a parent-child relationship. So what does the binary tree sort mean? In fact, it is very easy to add two basic conditions to the basic definition of a binary tree: (1) the node values of all left subtree are smaller than the value of this node; (2) the value of all right nodes is greater than the value of this node.

 

Now that we can see the definition of the node, we can get that, as long as we traverse in a certain order, we can print the nodes in the binary tree in a certain order. Then, how does one create, search, and traverse nodes? How does one calculate the height of a binary tree? Let's go one by one.

 

1) create a binary tree node

 

 

TREE_NODE * create_tree_node (int data)

{

TREE_NODE * pTreeNode = NULL;

PTreeNode = (TREE_NODE *) malloc (sizeof (TREE_NODE ));

Assert (NULL! = PTreeNode );

 

Memset (pTreeNode, 0, sizeof (TREE_NODE ));

PTreeNode-> data = data;

Return pTreeNode;

}

TREE_NODE * create_tree_node (int data)

{

TREE_NODE * pTreeNode = NULL;

PTreeNode = (TREE_NODE *) malloc (sizeof (TREE_NODE ));

Assert (NULL! = PTreeNode );

 

Memset (pTreeNode, 0, sizeof (TREE_NODE ));

PTreeNode-> data = data;

Return pTreeNode;

} Analysis: we can see that the creation of Binary Tree nodes is essentially different from the creation of linked list nodes and stack nodes. First, you need to create memory for the node, and then initialize the memory. Finally, enter the input parameter data to tree_node.

 

 

 

 

2) data search

 

 

TREE_NODE * find_data_in_tree_node (const TREE_NODE * pTreeNode, int data)

{

If (NULL = pTreeNode)

Return NULL;

 

If (data = pTreeNode-> data)

Return (TREE_NODE *) pTreeNode;

Else if (data <pTreeNode-> data)

Return find_data_in_tree_node (pTreeNode-> left_child, data );

Else

Return find_data_in_tree_node (pTreeNode-> right_child, data );

}

TREE_NODE * find_data_in_tree_node (const TREE_NODE * pTreeNode, int data)

{

If (NULL = pTreeNode)

Return NULL;

 

If (data = pTreeNode-> data)

Return (TREE_NODE *) pTreeNode;

Else if (data <pTreeNode-> data)

Return find_data_in_tree_node (pTreeNode-> left_child, data );

Else

Return find_data_in_tree_node (pTreeNode-> right_child, data );

} Analysis: our search is performed by recursive iteration. Because the entire Binary Tree is a Sort binary tree, we only need to compare the data with each node in sequence. If the value is smaller than the node data, we can continue traversing to the left; otherwise, continue the traversal to the right. If the traversal encounters a NULL pointer, the current data does not exist in the binary tree.

 

 

 

 

3) Data Statistics

 

 

Int count_node_number_in_tree (const TREE_NODE * pTreeNode)

{

If (NULL = pTreeNode)

Return 0;

 

Return 1 + count_node_number_in_tree (pTreeNode-> left_child)

+ Count_node_number_in_tree (pTreeNode-> right_child );

}

Int count_node_number_in_tree (const TREE_NODE * pTreeNode)

{

If (NULL = pTreeNode)

Return 0;

 

Return 1 + count_node_number_in_tree (pTreeNode-> left_child)

+ Count_node_number_in_tree (pTreeNode-> right_child );

} Analysis: similar to the data search above, statistics are relatively simple. If it is a node pointer, return 0 directly. Otherwise, you need to calculate the number of nodes in the left node tree and the number of nodes in the right node tree respectively, so that the total number of all nodes can be added up.

 

 

 

 

4) print node data in ascending order

 

 

Void print_all_node_data (const TREE_NODE * pTreeNode)

{

If (pTreeNode ){

Print_all_node_data (pTreeNode-> left_child );

Printf ("% d \ n", pTreeNode-> data );

Print_all_node_data (pTreeNode-> right_child );

}

}

Void print_all_node_data (const TREE_NODE * pTreeNode)

{

If (pTreeNode ){

Print_all_node_data (pTreeNode-> left_child );

Printf ("% d \ n", pTreeNode-> data );

Print_all_node_data (pTreeNode-> right_child );

}

} Analysis: Due to the particularity of the binary tree itself, the function of printing the binary tree in sequence is also relatively simple. First, print the node of the Left subtree, then print the value of the current node, and finally print the value of the right subtree node, so that all the node values can be printed out.

 

 

 

 

5) height of the statistical tree

 

 

Int calculate_height_of_tree (const TREE_NODE * pTreeNode)

{

Int left, right;

If (NULL = pTreeNode)

Return 0;

 

Left = calculate_height_of_tree (pTreeNode-> left_child );

Right = calculate_height_of_tree (pTreeNode-> right_child );

Return (left> right )? (Left + 1): (right + 1 );

}

Int calculate_height_of_tree (const TREE_NODE * pTreeNode)

{

Int left, right;

If (NULL = pTreeNode)

Return 0;

 

Left = calculate_height_of_tree (pTreeNode-> left_child );

Right = calculate_height_of_tree (pTreeNode-> right_child );

Return (left> right )? (Left + 1): (right + 1 );

} Analysis: the tree height actually refers to the maximum height of all leaf nodes from the root node to the leaf node. Of course, the program shows that if the node is empty, it is a pity that the node height is 0; otherwise, if the height of the Left subtree is greater than that of the right subtree, the node height of the entire binary tree is the height of the Left subtree plus 1. If the height of the right subtree is greater than that of the left subtree, the height of the entire binary tree is the height of the right subtree plus 1. The height of the computing tree is very useful when we design a balanced binary tree, especially during testing. I hope you can understand it more and be proficient in it.

 

 

 

 

Summary:

 

1) binary trees are the foundation of all trees. Subsequent balanced binary trees, linear Binary Trees, red and black trees, composite Binary Trees, B trees, and B + trees are all based on this. I hope you can learn well;

 

2) Many Binary Tree operations are closely related to stacks. If you cannot understand recursion for the moment, you can use loops or stacks instead;

 

3) practice to learn more about the code of Binary Tree sorting. I personally write a balanced binary tree no less than 20 times, and even this cannot ensure that it is correct each time. Even so, I feel different every time I write code.

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.