Binary Tree topic

Source: Internet
Author: User
[Topic 5] Create a binary tree using the result of forward and middle traversal in <Binary Tree C ++ implementation> Histogram }. First look at the first element 1 in the previous order, representing the root of the binary tree to be constructed. Search for 1 in the sequence list. After finding 1, you can judge that the left of 1 is the left subtree of the root, on the right is the right subtree of the root, so that the rest of the middle sequence is divided into {, 2,} and {,}, and the corresponding front sequence is divided into {2, 4, 7,} and {3, 5, 6, 8}; then recursively, scale down along the forward sequence, for {4, 7, 2,} and {2, 4, 7 ,} perform the same operation. The recursive boundary is that there is only one element left in the pre-order sequence and the middle sequence, and the elements are equal. In this way, the left-side left subtree has been constructed, and then the last {5, 3, 8, 6} and {3, 5, 6, 8} Do recursion, it is not difficult. The Code is as follows. Code 5]
# Ifndef null # define null 0 # endifstruct limit {int m_value; binary_tree_node * m_left; Limit * m_right;}; Limit * construct_core (int * start_preorder, int * end_preorder, int * start_inorder, int * end_inorder); binary_tree_node * construct_binary_tree (int * pre_order, int * in_order, int Len) {If (pre_order = NULL | in_order = NULL | Len <1) return NULL; return construct_core (pre_o Rder, pre_order + len-1, in_order, in_order + len-1);} binary_tree_node * construct_core (int * start_preorder, int * end_preorder, int * start_inorder, int * end_inorder) {int root_value = start_preorder [0]; binary_tree_node * node = new binary_tree_node; node-> m_value = root_value; node-> m_left = node-> m_right = NULL; if (start_preorder = end_preorder & start_inorder = end_inorder & * start_preorder = * Start_inorder) return node; // recursive boundary int * in_start = start_inorder; while (in_start <= end_inorder & * in_start! = Root_value) ++ in_start; If (in_start = end_inorder & * in_start! = Root_value) return NULL; // This array is unreasonable. The value in the previous order does not contain int left_len = in_start-start_inorder; int * left_preorder_end = start_preorder + left_len; If (left_len> 0) {// construct the left subtree node-> m_left = construct_core (start_preorder + 1, left_preorder_end, start_inorder, in_start-1);} If (left_len <end_preorder-start_preorder) {// construct the right subtree node-> m_right = construct_core (left_preorder_end + 1, end_preorder, in_start + 1, end_inorder );} Return node ;}# include <iostream >#include <queue> using namespace STD; void level_visit (binary_tree_node * root) {If (root = NULL) return; queue <binary_tree_node *> m_queue; m_queue.push (Root); While (! M_queue.empty () {binary_tree_node * B _node = m_queue.front (); m_queue.pop (); cout <B _node-> m_value <""; if (B _node-> m_left! = NULL) m_queue.push (B _node-> m_left); If (B _node-> m_right! = NULL) m_queue.push (B _node-> m_right );}}
[Test 5] The last function above is used to print out Binary Trees and traverse them in layers.
int main(){int pre[] = {1,2,4,7,3,5,6,8};int in[] = {4,7,2,1,5,3,8,6};binary_tree_node* root = construct_binary_tree(pre, in, sizeof(pre)/sizeof(pre[0]));level_visit(root);}
[Question 18] tree sub-structure [Question 19] binary tree image [question 24] Binary Search Tree post-order traversal sequence [Question 27] Binary Search Tree and two-way linked list
(To be continued)
 

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.