#include<iostream>
using namespace std;
#define InitSize 20
typedef int TElemType;
typedef struct node{
struct node* lchild, *rchild;
TElemType data;
}BinNode;
typedef struct {
BinNode tnode[InitSize];
int curSize, maxSize;
}BinTree;
void TreeInit(BinTree&BT)
{
BT.curSize = 0;
BT.maxSize = InitSize;
}
void CreateBinTree(BinTree&BT,TElemType ch[],int n)
{
TreeInit(BT);
if (BT.maxSize < n)return;
for (int i = 0;i < n;i++)
{
BT.tnode[i].data = ch[i];
int parent = (i - 1) / 2;
if ( parent>= 0)
{
if (i % 2)
BT.tnode[parent].lchild = &BT.tnode[i];
else
BT.tnode[parent].rchild = &BT.tnode[i];
}
}
BT.curSize = n;
}
int main()
{
TElemType ch[10] = { 1,2,3,4,5,6,7,8,9,10 };
BinTree BT;
CreateBinTree(BT, ch, 10);
system("pause");
}
C++根据数组建立二叉树
最新推荐文章于 2024-05-14 18:27:26 发布