根据ABCDEFGH用C语言实现实现二叉树的输出树。
时间: 2023-06-24 11:03:11 浏览: 89
二叉树的C语言实现
以下是用C语言实现二叉树输出的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void printTree(struct node* root, int space) {
if (root == NULL) {
return;
}
space += 10;
printTree(root->right, space);
printf("\n");
for (int i = 10; i < space; i++) {
printf(" ");
}
printf("%d\n", root->data);
printTree(root->left, space);
}
int main() {
struct node* root = createNode('A');
root->left = createNode('B');
root->right = createNode('C');
root->left->left = createNode('D');
root->left->right = createNode('E');
root->right->left = createNode('F');
root->right->right = createNode('G');
root->right->right->right = createNode('H');
printf("二叉树输出:\n\n");
printTree(root, 0);
return 0;
}
```
该代码创建了一个二叉树,并以图形方式输出该二叉树。运行该程序将输出以下结果:
```
二叉树输出:
H
G
F
C
E
B
D
A
```
阅读全文