数据结构 如何创建一棵树,请给出c语言详细代码,谢谢

如题所述

刚刚回答了一个类似的问题,以下代码供参考:
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef char TElemType;
typedef int Status;
typedef struct BiTNode { // 结点结构
TElemType data;
struct BiTNode *lchild, *rchild;
// 左右孩子指针
} BiTNode, *BiTree;

//以下是建立二叉树存储结构,空节点输入作为#结束标识
Status CreateBiTree(BiTree &T) {
//请将该算法补充完整,参见第6章课件算法或课本
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;

} // CreateBiTree
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}

void Inorder(BiTree T)
{ // 中序遍历二叉树
//请将该算法补充完整,参见第6章课件算法
if(T)
{
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BiTree T)
{ // 后序遍历二叉树
//请将该算法补充完整,参见第6章课件算法
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c",T->data);
}
}

//以下是求叶子结点数
void CountLeaf(BiTree T,int& count){
//请将该算法补充完整,参见第6章课件算法
if(T){
if((!T->lchild)&&(!T->rchild))
count++;
CountLeaf(T->lchild,count);
CountLeaf(T->rchild,count);
}
}

//以下是求二叉树的深度
int Depth(BiTree T ){
//请将该算法补充完整,参见第6章课件算法
int depthval,depthLeft,depthRight;
if(!T) depthval=0;
else{
depthLeft = Depth(T->lchild);
depthRight = Depth(T->rchild);
if(depthLeft>depthRight)depthval = 1+depthLeft;
else depthval = 1+depthRight;
}
return depthval;
}

void main(){
BiTree T;
int s=0,d;
printf("\n creat of the bitree:\n");
CreateBiTree(T);
printf("\n output result of Preorder:\n");
Preorder(T);
CountLeaf(T,s);
d=Depth(T);
printf("\n leaves=%d\n",s);
printf("\n depth=%d\n",d);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2023-03-09
以下是一个简单的C语言程序,可以创建一棵二叉树:

```c
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
int data;
struct node *left;
struct node *right;
} Node;

Node* create(int data) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}

void insert(Node **root, int data) {
if (*root == NULL) {
*root = create(data);
} else if (data < (*root)->data) {
insert(&(*root)->left, data);
} else if (data > (*root)->data) {
insert(&(*root)->right, data);
}
}

void inorder(Node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
Node *root = NULL;

// 创建二叉树
insert(&root, 5);
insert(&root, 3);
insert(&root, 7);
insert(&root, 1);
insert(&root, 9);

// 中序遍历二叉树并输出结果
inorder(root);

return 0;
}
```

在上述代码中,我们首先定义了二叉树节点的数据结构 `Node`,其中包含了节点的值以及左右子节点的指针。然后,我们提供了两个函数:`create()` 和 `insert()`。`create()` 函数用于创建新的节点,并返回其指针;`insert()` 函数用于向二叉树中插入新的节点。

在 `insert()` 函数中,我们首先判断当前根节点是否为 `NULL`,如果是,则将新节点插入到该位置;否则,递归地向下查找合适的插入位置。需要注意的是,这里假设二叉树中不会存储重复的值。

最后,我们在 `main()` 函数中创建了一棵二叉树,并调用 `inorder()` 函数进行中序遍历,将其结果输出到控制台。在实际应用中,可以根据需要实现其他遍历方式,如前序遍历和后序遍历等。

例如,对于以上示例代码,程序将输出以下结果:

```
1 3 5 7 9
```

需要注意的是,在实际应用中还需要考虑更多的边界情况和错误处理。例如,可能出现内存分配失败、节点重复插入或删除失败等问题。
第2个回答  2012-11-05
# include <stdio.h>
# include <stdlib.h
typedef struct BiTNode
{
char data;
struct BiTNode * lchild,* rchild;
}BiTNode, *BiTree;
//先序建立二叉树中的节点
BiTree CreatBiTree()
{
BiTree T;
char ch;
fflush(stdin);
scanf("%c",&ch);
if(ch == '0')
{
return NULL;
}
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
if(!T)
exit(1);
T->data=ch;
T->lchild = CreatBiTree();
T->rchild = CreatBiTree();
return T;
}
}

void PreTravel(BiTree T)
{
if(T)
{
printf("%c ",T->data);
PreTravel(T->lchild);
PreTravel(T->rchild);
}
}

int main()
{
BiTree T;
printf("先序建立二叉树结点(‘0’表示空):\n");
T = CreatBiTree();
printf("先序遍历创建的二叉树:\n");
PreTravel(T);
printf("\n");
return 0;
}
/*
结果:
------------------------
先序建立二叉树结点(‘0’表示空):
a
b
0
0
c
0
0
先序遍历创建的二叉树:
a b c
Press any key to continue
------------------------------
*/
相似回答