#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;
}
将一个有序整数数组放在一个二叉树中
最新推荐文章于 2024-03-05 19:30:45 发布