Step-by-step write algorithm (sort binary tree)

Source: Internet
Author: User

Text: Step by step write algorithm (sort binary tree)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


We talked about the data structure of the doubly linked list before. Each loop node has two pointers, one pointing to the previous node, and one pointing to the successor, so that all nodes are worn together like a pearl by a line. However, the data structure we are discussing today is a little different, it has three nodes. It is defined in this way:

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 see that each data node has three pointers, namely, a pointer to the parent, a pointer to the left child, and a pointer to the right child. Each node is connected to each other by pointers. The relationship between the linked pointers is a parent-child relationship. So what does it mean to sort two-pronged trees? In fact, as long as the basic definition of the binary tree to add two basic conditions can be: (1) All the left subtree node value is less than the value of this node, (2) All the right node value is greater than the value of this node.

Now that we see the definition of the node, we can get, as long as we follow a certain sequence, we can print the nodes in the two-fork tree in one order. So, how does the creation, lookup, and traversal of a node proceed, and how should the height of the binary tree be calculated? We're here on one by one.

1) Create a two-fork 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));p treenode->data = Data;return ptreenode;}
Analysis: We see that the creation of binary tree nodes is not fundamentally different from the creation of linked list nodes and stack nodes that we see. You first need to create memory for the node, and then initialize the memory for processing. Finally, input parameter data is input into Tree_node.


2) Finding the 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); Elsereturn Find_data_in_tree_node (Ptreenode->right_child, data);}
Analysis: Our lookups are performed in recursive iterations. Because the whole binary tree is a sort of binary tree, so our data only need and each node in order to compare it, if the value is smaller than the node data, then left to continue to traverse; If the traversal encounters a null pointer, it can only indicate that 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);}
Analysis: As with the above to find data, statistical work is relatively simple. If it is a node pointer, then directly return 0, otherwise you need to separately count the number of nodes in the left node tree, the number of nodes in the right node tree, so that all the total number of nodes added up on it.


4) Print node data in order from small to large

void Print_all_node_data (const tree_node* ptreenode) {if (Ptreenode) {print_all_node_data (ptreenode->left_child); printf ("%d\n", Ptreenode->data);p rint_all_node_data (Ptreenode->right_child);}}
Analysis: Because of the particularity of the binary tree itself, the function of printing binary tree sequentially is simpler. First, print the nodes of the left subtree, then print the values of this node, and finally print the values of the right subtree node so that the values of all the nodes can be printed out.


5) Height of the statistics 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);}
Analysis: The height of the tree actually refers to the maximum height of all leaf nodes, from the root node to the leaf node. Of course, the program has been very clear, if the node is empty, then unfortunately, the height of the node is 0; Conversely, if the height of the Zuozi is greater than the height of the right subtree, then the height of the entire binary tree is Zuozi height plus 1; If the height of the right subtree is greater than the height of the left subtree, Then the height of the entire binary tree is the height of the right subtree plus 1. The height of the calculation tree is very useful when we design a balanced binary tree, especially when testing, we hope that we have a lot of understanding and mastery.


Summarize:

1) binary tree is the basis of all trees, follow the balance of binary tree, linear binary tree, red and black trees, composite binary tree, B-tree, plus tree are based on this, I hope you learn well;

2) binary tree A lot of operations are closely linked with the stack, if you can not understand the recursion, you may use loops or stacks instead;

3) Practice out of the truth, you can own the sort of binary tree code a lot of practice. I do not deceive everyone to say that I personally write balanced binary tree not more than 20 times, even if this is not guaranteed to be correct every time, even so, every time I write code has a different feeling.


"Preview: The following blog introduces the insertion and deletion of balanced binary trees"


Step-by-step write algorithm (sort binary tree)

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.