#pragma once
#define MAX_TREE_SIZE 100
// 指定树结点存储的数据类型
typedef char ElemType;
typedef struct PTNode {
ElemType data;
int parent; // 双亲位置域
} PTNode;
// 树结构
typedef struct {
PTNode nodes[MAX_TREE_SIZE];
int r, n; // 根的位置和结点数
}PTree;
typedef bool Status;
#define OK true;
#define ERROR false;
// 输入创建满二叉树
Status CreatePTree(PTree& T);
// 打印
void printPT(PTree& T);
文件tree.h
#define _CRT_SECURE_NO_DEPRECATE
#include "tree.h"
#include <malloc.h>
#include <stdio.h>
#include <cmath>
#include <iostream>
using namespace std;
// 输入创建满二叉树
Status CreatePTree(PTree& T) {
char ch = 'A';
if (T.n < 0) T.n = 0;
// * 表示空
while (ch != '*') {
PTNode P;
scanf("%c", &ch);
P.data = ch;
T.nodes[T.n++] = P;
}
int depth = 0;
int all = T.n;
int temp = 1;
while (all > 0) {
all -= temp;
depth++;
temp *= 2;
}
while (T.n < pow(2, depth) - 1) {
PTNode P;
P.data = '*';
T.nodes[T.n++] = P;
}
return OK;
}
void printPT(PTree& T) {
int len = T.n;
while (len-- > 0) {
cout << T.nodes[T.n - len - 1].data << ' ';
}
}
文件tree.cpp
#include <stdio.h>
#include "tree2.h"
#include "tree.h"
int main() {
PTree P;
printf("满二叉输入\n");
CreatePTree(P);
printf("\n数组结构\n");
printPT(P);
return 0;
}
文件main.cpp
测试结果: