5.1 Array Storage Binary tree experiment

Source: Internet
Author: User

iOS Learning Exchange Group: 230644538 #include <stdio.h> #define MAX 30/****-----------------------------------------*****///  Function name: createtree (int btree[], int list[], int n)//parameter: (incoming) int btree[] binary tree//(incoming) int list[] Data array//(incoming) int n Number of data//function: Build two fork tree/****-----------------------------------------*****/void createtree (int *bt, int list[], int n) {int    I    int level;    for (i = 0; i < MAX; i++) {bt[i] = 0;  } Bt[0] = list[0];            Establish two fork root node for (i = 1; i < n; i++) {level = 1;//build from First layer while (Bt[level]! = 0) {//Determine if there are any subtrees            if (List[i] < Bt[level])//Establish left sub-tree level = level*2; else level = level*2+1;    Establish right sub-tree} Bt[level] = List[i];    }}/********************** Test Main program ***************************/int main () {int count, I;    int Btree[max];    int Nodelist[max];    printf ("Input the number of elements (n <): \ n");    scanf ("%d", &count);    printf ("Input elements:\n"); for (i = 0; I < count;    i++) {scanf ("%d", &nodelist[i]);    } createtree (Btree, NodeList, Count);    printf ("The Binary Tree is:\n");  for (i = 0; i < MAX;    i++) {printf ("%d", btree[i]);    } printf ("\ n"); return 0;}

5.1 Array Storage Binary tree experiment

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.