Rebuilding a binary tree from a central sequence of a binary tree

Source: Internet
Author: User
Document directory
  • Lab questions (6 questions in total, 4th questions)
 Lab questions (6 questions in total, 4th questions)
Title: Rebuilding a binary tree from a central sequence of a binary tree
Time limit: 1000 MS
Memory limit: 10000 K
Total time limit: 3000 MS
Description: Given the central sequence and sequence output of a binary tree, generate the tree and output the node information in the tree structure in the first and last order as an integer.
Input:
Number of Tree nodes sequence output sequence medium sequence input sequence
Output: First, then
Input example:
6
1 2 3 5 6 72 5 1 6 3 7
Output example: 1 2 5 3 6 75 2 6 7 3 1
Tip:

The output 1st of the sequence are root nodes.

Source:

# Include <stdio. h> # include <stdlib. h> int * level; int * In; typedef struct bitnode {int data; bitnode * lchild; bitnode * rchild; // left and right child pointer} bitnode, * bitree; typedef struct qelemnode {int data; int L, H; // lower bound and Upper Bound bitree parent of the central sequence; // int LR; // LR = 1-> left subtree of the parent; LR = 2-> right subtree of the parent; LR = 0-> root node} qelemnode; typedef qelemnode qelemtype; typedef struct qnode {qelemtype data; struct qnode * Next;} qnode, * queueptr; typed EF struct {queueptr front; queueptr rear;} linkqueue; bitree t; void initqueue (linkqueue & Q) {q. front = Q. rear = (queueptr) malloc (sizeof (qnode); If (! Q. front) Exit (0); q. front-> next = NULL;} void enqueue (linkqueue & Q, qelemtype e) {queueptr P; P = (queueptr) malloc (sizeof (qnode); If (! P) Exit (0); P-> DATA = E; P-> next = NULL; q. rear-> next = P; q. rear = P;} void dequeue (linkqueue & Q, qelemtype & E) {queueptr P; If (Q. front = Q. rear) Exit (0); P = Q. front-> next; E = p-> data; q. front-> next = p-> next; If (Q. rear = P) Q. rear = Q. front; free (p);} int queueempty (linkqueue q) {If (Q. front = Q. rear) return 1; else return 0;} void initbitree (bitree & T) {T = NULL;} void destroybitree (bitree & T) {If (t) // non-empty tree {If (t-> lchi LD) // destroybitree (t-> lchild) with the left child; // destroy the left child subtree if (t-> rchild) // destroybitree (t-> rchild) with the right child; // destroy the right child subtree free (t); // release the root node T = NULL; // NULL pointer assigned 0} void postordertraverse (bitree t) {If (T! = NULL) {postordertraverse (t-> lchild); postordertraverse (t-> rchild); printf ("% d", T-> data );}} void preordertraverse (bitree t) {If (T! = NULL) {printf ("% d", T-> data); preordertraverse (t-> lchild); preordertraverse (t-> rchild );}} bitree createbitreefromlevelinorder (int * level, int * In, int N) {int r =-1, low, high, LR, I, j; bitree root = NULL, P, F; int ch; qelemtype s; linkqueue Q; initqueue (Q); S. data = level [++ R]; S. L = 0; S. H = n-1; S. parent = NULL; S. LR = 0; enqueue (Q, S); While (! Queueempty (q) {dequeue (Q, S); CH = S. data; Low = S. l; high = S. h; F = S. parent; LR = S. LR; For (j = low; j <= high; j ++) if (in [J] = CH) {I = J; break;} p = (bitree) malloc (sizeof (bitnode); P-> DATA = CH; P-> lchild = p-> rchild = NULL; If (LR = 0) root = P; else if (LR = 1) F-> lchild = P; else f-> rchild = P; If (Low = high) continue; if (I = low) // No left child at the node {S. L = low + 1; S. LR = 2; S. data = level [++ R]; S. parent = P; enqueue (Q, S);} else if (I = high) // The node has no right child {S. H = high-1; S. LR = 1; S. data = level [++ R]; S. parent = P; enqueue (Q, S);} else // The node has left and right children {S. H = I-1; S. L = low; S. LR = 1; S. data = level [++ R]; S. parent = P; enqueue (Q, S); S. L = I + 1; S. H = high; S. LR = 2; S. data = level [++ R]; S. parent = P; enqueue (Q, S) ;}} return root;} int main () {initbitree (t); int N; int I; scanf ("% d ", & N); Level = (int *) malloc (sizeof (INT) * n); In = (int *) malloc (sizeof (INT) * n ); for (I = 0; I <n; I ++) {scanf ("% d", level + I) ;}for (I = 0; I <N; I ++) {scanf ("% d", in + I);} t = createbitreefromlevelinorder (level, in, n); preordertraverse (t ); printf ("\ n"); postordertraverse (t); Return 0 ;}

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.