将一个有序整数数组放在一个二叉树中

来源:互联网 发布:上瘾网络剧猛其其花絮 编辑:程序博客网 时间:2024/05/25 21:36
#include <stdio.h>#include <windows.h>#include <iostream>using namespace std;struct student{    int vaule;    struct student *lchild;    struct student *rchild;};void arraytotree(int *a,int len,struct student*&p){    if (len>0)    {        p = new student;        int mid = len / 2;        (p)->vaule = a[mid];        arraytotree(a, mid, p->lchild);        arraytotree(a + mid + 1, len - mid - 1, p->rchild);    }    else    {        p = NULL;    }}void display_tree(struct student *head){    if (NULL != head)    {        display_tree(head->lchild);        cout << head->vaule << endl;        display_tree(head->rchild);    }}int main(){    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };    struct student *tree=NULL;    arraytotree(a, sizeof(a) / sizeof(a[0]), tree);    printf("After convert:");    display_tree(tree);      system("pause");    return 0; }

这里写图片描述

阅读全文
0 0
原创粉丝点击