二叉树结点的结构体设计
时间: 2024-01-23 11:20:45 浏览: 32
二叉树结点的结构体通常包括三个成员变量:数据域、左子树指针和右子树指针。
具体代码如下:
```
struct TreeNode {
int val; // 数据域
TreeNode* left; // 左子树指针
TreeNode* right; // 右子树指针
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
```
其中,构造函数的作用是初始化结点的值,并将左子树指针和右子树指针初始化为 nullptr(空指针),表示该结点暂时没有左右子树。
相关问题
写一个基于刚才结点结构体的完全二叉树创建代码
好的,下面是一个基于刚才的结点结构体定义的完全二叉树创建代码,可以根据用户输入的数据构建一棵完全二叉树:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef int TElemType;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
// 队列结构体
typedef struct {
BiTree data[MAX_SIZE];
int front, rear;
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = q->rear = 0;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->front == q->rear;
}
// 入队
int enQueue(Queue *q, BiTree t) {
if ((q->rear + 1) % MAX_SIZE == q->front) return 0; // 队满
q->data[q->rear] = t;
q->rear = (q->rear + 1) % MAX_SIZE;
return 1;
}
// 出队
int deQueue(Queue *q, BiTree *t) {
if (isEmpty(q)) return 0; // 队空
*t = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return 1;
}
// 创建完全二叉树
void createCompleteBiTree(BiTree *root) {
Queue q;
initQueue(&q);
*root = NULL;
TElemType data;
printf("请输入完全二叉树的结点数据,以 -1 结束:\n");
while (scanf("%d", &data) != EOF && data != -1) {
BiTree node = (BiTree) malloc(sizeof(BiNode));
node->data = data;
node->lchild = node->rchild = NULL;
enQueue(&q, node);
if (*root == NULL) {
*root = node;
} else {
BiTree front;
if (deQueue(&q, &front)) {
if (front->lchild == NULL) {
front->lchild = node;
} else if (front->rchild == NULL) {
front->rchild = node;
}
enQueue(&q, front);
}
}
}
}
// 中序遍历
void inOrderTraverse(BiTree root) {
if (root == NULL) return;
inOrderTraverse(root->lchild);
printf("%d ", root->data);
inOrderTraverse(root->rchild);
}
int main() {
BiTree root;
createCompleteBiTree(&root);
printf("中序遍历结果:\n");
inOrderTraverse(root);
printf("\n");
return 0;
}
```
这个代码使用队列实现了完全二叉树的创建,用户输入数据时按照完全二叉树的顺序插入节点,同时使用队列记录每个节点的位置,便于后续操作。最后使用中序遍历对创建好的完全二叉树进行遍历,输出结果。
C语言实现计算二叉树结点的数目
以下是C语言实现计算二叉树结点数目的代码和解释:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结点结构体
typedef struct BiTNode {
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
// 计算二叉树结点数目的函数
int GetNodeNumber(BiTree T) {
int count = 0;
if(T != NULL) {
count = GetNodeNumber(T->lchild) + GetNodeNumber(T->rchild) + 1; // 此处加的1为根节点
}
return count;
}
int main() {
// 创建一棵二叉树
BiTree T = (BiTree)malloc(sizeof(BiTNode));
T->data = 'A';
T->lchild = (BiTree)malloc(sizeof(BiTNode));
T->lchild->data = 'B';
T->lchild->lchild = NULL;
T->lchild->rchild = NULL;
T->rchild = (BiTree)malloc(sizeof(BiTNode));
T->rchild->data = 'C';
T->rchild->lchild = NULL;
T->rchild->rchild = NULL;
// 计算二叉树结点数目并输出
int nodeNum = GetNodeNumber(T);
printf("The number of nodes in the binary tree is: %d\n", nodeNum);
return 0;
}
```
解释:
1. 首先定义了二叉树结点的结构体,包括数据域和左右子树指针。
2. 定义了计算二叉树结点数目的函数`GetNodeNumber`,该函数采用递归的方式计算二叉树结点数目。如果当前结点不为空,则递归计算左右子树的结点数目并加上1(当前结点)。
3. 在`main`函数中创建了一棵二叉树,并调用`GetNodeNumber`函数计算二叉树结点数目并输出。
--相关问题--:
1. 如何判断一个二叉树是否为满二叉树?
2. 如何
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)