Reconstruction (C ++) using a fully-Ordered Binary Tree)

Source: Internet
Author: User
Document directory
  • Lab questions (6 questions in total, 3rd questions)
Lab questions (6 questions in total, 3rd questions)
Title: Re-built by a fully Ordered Binary Tree
Time limit: 1000 MS
Memory limit: 3000 K
Total time limit: 3000 MS
Description: Node records of a Complete Binary Tree stored in order. The number of nodes is N. Create a binary tree based on the node records of the input sequence structure, and output the first, middle, and last traversal results of the tree.
Note: The number "0" indicates that this node does not exist and no child node exists.
Input: Number of Tree nodes N
Full Binary Tree stored in sequential mode
Output: First traverse the output
Sequential traversal output
Post-order traversal output
Input example: 10
1 2 0 3 4 0 5 6 7
Output example: 1 2 3 5 6 4 75 3 6 2 7 4 15 6 3 7 4 2 1
Tip: All the child nodes with the number "0" are "0"
Source:
View code

  1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<malloc.h>
4
5 typedef int ElemType;
6 typedef struct TNode
7 {
8 ElemType data;
9 struct TNode* lChild;
10 struct TNode* rChild;
11 }TNode,*BiTree;
12
13 ElemType* Get(int);
14 BiTree Creat_BiTree(ElemType *,int,int);
15 void PreOrderTraverse(BiTree);
16 void InOrderTraverse(BiTree);
17 void PostOrderTraverse(BiTree);
18 void DestroyTree(BiTree);
19
20 int main(void)
21 {
22 int n;
23 //int i;
24 ElemType *a;
25 BiTree T;
26
27 scanf("%d",&n);
28 a=Get(n);
29 //for(i=0;i<n;i++)
30 // printf("%d\n",a[i]);
31 T=Creat_BiTree(a,0,n);
32 PreOrderTraverse(T);
33 printf("\n");
34 InOrderTraverse(T);
35 printf("\n");
36 PostOrderTraverse(T);
37 printf("\n");
38 DestroyTree(T);
39 free(a);
40
41 return 0;
42 }
43
44 ElemType* Get(int n)
45 {
46 int i;
47 ElemType *a;
48 a=(ElemType*)malloc(sizeof(ElemType)*n);
49 if(!a)
50 {
51 printf("out of space!\n");
52 exit(0);
53 }
54 for(i=0;i<n;i++)
55 scanf("%d",&a[i]);
56
57 return a;
58 }
59
60 BiTree Creat_BiTree(ElemType *a,int i,int n)
61 {
62 //int i;
63 BiTree tmpcell;
64 if(*a==0||i>n-1)
65 return NULL;
66 /*else if(2*i+1>n-1)
67 {
68 tmpcell=(BiTree)malloc(sizeof(TNode));
69 tmpcell->data=*a;
70 tmpcell
71 }*/
72 else
73 {
74 tmpcell=(BiTree)malloc(sizeof(TNode));
75 tmpcell->data=*a;
76 tmpcell->lChild=Creat_BiTree(a+i+1,2*i+1,n);
77 tmpcell->rChild=Creat_BiTree(a+i+2,2*i+2,n);
78 }
79
80 return tmpcell;
81 }
82
83 void PreOrderTraverse(BiTree T)
84 {
85 if(T!=NULL)
86 {
87 printf("%d ",T->data);
88 PreOrderTraverse(T->lChild);
89 PreOrderTraverse(T->rChild);
90 }
91 }
92
93 void InOrderTraverse(BiTree T)
94 {
95 if(T!=NULL)
96 {
97 InOrderTraverse(T->lChild);
98 printf("%d ",T->data);
99 InOrderTraverse(T->rChild);
100 }
101 }
102
103 void PostOrderTraverse(BiTree T)
104 {
105 if(T!=NULL)
106 {
107 PostOrderTraverse(T->lChild);
108 PostOrderTraverse(T->rChild);
109 printf("%d ",T->data);
110 }
111 }
112
113 void DestroyTree(BiTree T)
114 {
115 if(T!=NULL)
116 {
117 DestroyTree(T->lChild);
118 DestroyTree(T->rChild);
119 free(T);
120 }
121 }

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.