Re-build a binary tree based on the pre-order traversal and Middle-order traversal results

Source: Internet
Author: User

The premise is that the values of each node are different.

The idea is that the first value of the forward traversal is the value of the parent node. You can narrow the range by finding the value in the middle traversal. Use recursion.

Step 5:

1. Create a root node based on the first value of the table in the previous order. If there is only one node, the result will be returned as a result of recursive termination.

2. Find the parent node location in the middle-order table

3. If there is a left subtree, create the left subtree recursively and assign the return value to the left subtree pointer.

4. If the right subtree exists, create the right subtree recursively and assign the return value to the right subtree pointer.

5. Return the root node


# Include <stdio. h> # include <stdlib. h> typedef struct mynode {int a; struct mynode * leftpoint; struct mynode * rightpoint;} node, * nodepoint; nodepoint construct (int * pre_start, int * pre_end, int * mid_start, int * mid_end) {// build the parent node int root_value = pre_start [0]; nodepoint root = (nodepoint) malloc (sizeof (node); root-> a = root_value; root-> leftpoint = root-> rightpoint = NULL; if (pre_start = pre_end && Mid_start = mid_end & * pre_start = * mid_start) // return root for Recursive termination; // search for the root node int * mid_root = mid_start in the central sequence table; while (mid_root <= mid_end & * mid_root! = Root_value) ++ mid_root; if (mid_root = mid_end & * mid_root! = Root_value) exit (0); // recursive int mid_left_end = mid_root-mid_start; int * pre_left_end = pre_start + mid_left_end; if (mid_left_end> 0) {// left subtree, build left subtree root-> leftpoint = construct (pre_start + 1, pre_left_end, mid_start, mid_root-1);} if (mid_left_end <pre_end-pre_start) {// right subtree available, build right subtree root-> rightpoint = construct (pre_left_end + 1, pre_end, mid_root + 1, mid_end);} return root; // return the root node} void Print_tree_pre (nodepoint p) {if (! P) return; else {printf ("% d \ t", p-> a); print_tree_pre (p-> leftpoint); print_tree_pre (p-> rightpoint );}} void print_tree_mid (nodepoint p) {if (p = NULL) return; else {print_tree_mid (p-> leftpoint); printf ("% d \ t ", p-> a); print_tree_mid (p-> rightpoint) ;}} void print_tree_aft (nodepoint p) {if (p = NULL) return; else {print_tree_aft (p-> leftpoint); print_tree_aft (p-> rightpoint); printf ("% d \ t", p-> a) ;}} void main () {int a [8] = {,}, B [8] = {,}; nodepoint node; node = construct (a, a + 7, B, B + 7); print_tree_pre (node); printf ("\ n"); print_tree_mid (node ); printf ("\ n"); print_tree_aft (node); printf ("\ n ");}



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.