当前位置:首页 » 编程语言 » 二叉树c语言用什么定义
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

二叉树c语言用什么定义

发布时间: 2023-02-16 07:03:39

1. 请问c语言中二树杈的定义和具体用法是什么

二叉树也是递归定义的,其结点有左右子树之分,逻辑上二叉树有五种基本形态: (1)空二叉树——(a); (2)只有一个根结点的二叉树——(b); (3)右子树为空的二叉树——(c); (4)左子树为空的二叉树——(d); (5)完全二叉树——(e)注意:尽管二叉树与树有许多相似之处,但二叉树不是树的特殊情形。

2. 如何用C语言定义一个二叉树

创建的方法有很多啊
可以用链表,可以用数组,而且你的创建到底是形成一个数据结构,还是实实在在的建树呢
假如这样
struct
treenode
{
int
data;
treenode
*leftchild;
treenode
*rightchild;
}
这就是一个树了。你建立很多节点,然后让左右孩子指向你想要的。也是树。

3. 请问C语言如何创建二叉树

创建二叉树的源程序如下:

#include <cstdlib>

#include <stdio.h>

typedef struct node

{ //树的结点

int data;

struct node* left;

struct node* right;

} Node;

typedef struct

{ //树根

Node* root;

} Tree;

void insert(Tree* tree, int value)//创建树

{

Node* node=(Node*)malloc(sizeof(Node));//创建一个节点

node->data = value;

node->left = NULL;

node->right = NULL;

if (tree->root == NULL)//判断树是不是空树

{

tree->root = node;

}

else

{//不是空树

Node* temp = tree->root;//从树根开始

while (temp != NULL)

{

if (value < temp->data)//小于就进左儿子

{

if (temp->left == NULL)

{

temp->left = node;

return;

}

else

{//继续判断

temp = temp->left;

}

}

else {//否则进右儿子

if (temp->right == NULL)

{

temp->right = node;

return;

}

else {//继续判断

temp = temp->right;

}

}

}

}

return;

}

void inorder(Node* node)//树的中序遍历

{

if (node != NULL)

{

inorder(node->left);

printf("%d ",node->data);

inorder(node->right);

}

}

int main()

{

Tree tree;

tree.root = NULL;//创建一个空树

int n;

scanf("%d",&n);

for (int i = 0; i < n; i++)//输入n个数并创建这个树

{

int temp;

scanf("%d",&temp);

insert(&tree, temp);

}

inorder(tree.root);//中序遍历

getchar();

getchar();

return 0;

}


(3)二叉树c语言用什么定义扩展阅读:

简单二叉树定义范例:此树的顺序结构为:ABCDE

#include <cstdlib>

#include <stdio.h>

#include <string>

int main()

{

node* p = newnode;

node* p = head;

head = p;

string str;

cin >> str;

creat(p, str, 0)//默认根结点在str下标0的位置

return 0;

}

//p为树的根结点(已开辟动态内存),str为二叉树的顺序存储数组ABCD##E或其他顺序存储数组,r当前结点所在顺序存储数组位置

void creat(node* p, string str, int r)

{

p->data = str[r];

if (str[r * 2 + 1] == '#' || r * 2 + 1 > str.size() - 1)p->lch = NULL;

else

{

p->lch = newnode;

creat(p->lch, str, r * 2 + 1);

}

if (str[r * 2 + 2] == '#' || r * 2 + 2 > str.size() - 1)p->rch = NULL;

else

{

p->rch = newnode;

creat(p->rch, str, r * 2 + 2);

}

}

4. 如何用C语言定义一个二叉树

如果只是定义的话,下面的code就可以了,欢迎交流
structNode{
intvalue;//二叉树的值
Node*left;//指向左子树
Node*right;//指向右子树
};

5. 数据结构二叉树的程序,用c语言怎么实现

您好,想要实现一个二叉树,需要用到结构体来存储每个节点的信息,并使用指针来存储每个节点的左右子节点的地址。具体的实现方法可以参考下面的代码示例:

#include <stdio.h>

#include <stdlib.h>

struct TreeNode {

int val;

struct TreeNode *left;

struct TreeNode *right;

};

struct TreeNode* createNode(int val) {

struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

void insertNode(struct TreeNode* root, int val) {

if (root == NULL) {

return;

}

if (val < root->val) {

if (root->left == NULL) {

root->left = createNode(val);

} else {

insertNode(root->left, val);

}

} else {

if (root->right == NULL) {

root->right = createNode(val);

} else {

insertNode(root->right, val);

}

}

}

void printTree(struct TreeNode* root) {

if (root == NULL) {

return;

}

printf("%d ", root->val);

printTree(root->left);

printTree(root->right);

}

int main() {

struct TreeNode* root = createNode(5);

insertNode(root, 3);

insertNode(root, 2);

insertNode(root, 4);

insertNode(root, 7);

insertNode(root, 6);

insertNode(root, 8);

printTree(root);

return 0;

}

在这段代码中,我们定义了一个结构体 TreeNode 来表示二叉树的每个节点,结构体中包含了一个节点的数值 val,以及指向左子节点和右子节点的指针 left 和 right。

6. C语言二叉树定义

这个结构体的{}之后,表示的是类型名称啊,BiTNode表示这种结构体的类型名称为BiTNode,*BiTree表示指向这种结构体的指针的类型名称为BiTree。

比如你定义变量的时候:
BiTNode a;//定义了一个上面那样的结构体
BiTree b;//定义了一个指针,该指针的类型是指向一个上面那样的结构体的