c语言单链表以及二叉树中创建时,编写函数,输入字符序列,建立二叉树的二叉链表

本文目录一览:

请问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;

}

扩展资料:

简单二叉树定义范例:此树的顺序结构为: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);

}

}

数据结构 c语言版二叉树(1) 建立一棵含有n个结点的二叉树,采用二叉链表存储;

#includestdio.h

#includestdlib.h

typedef struct node *tree_pointer;

struct node{

char ch;

tree_pointer left_child,right_child;

};

tree_pointer root=NULL;

tree_pointer create(tree_pointer ptr)

{

char ch;

scanf(“%c”,ch);

if(ch==’ ‘)

ptr=NULL;

else{

ptr=(tree_pointer)malloc(sizeof(node));

ptr-ch=ch;

ptr-left_child=create(ptr-left_child);

ptr-right_child=create(ptr-right_child);

}

return ptr;

}

void preorder(tree_pointer ptr)

{

if(ptr){

printf(“%c”,ptr-ch);

preorder(ptr-left_child);

preorder(ptr-right_child);

}

}

void inorder(tree_pointer ptr)

{

if(ptr){

inorder(ptr-left_child);

printf(“%c”,ptr-ch);

inorder(ptr-right_child);

}

}

void postorder(tree_pointer ptr)

{

if(ptr){

postorder(ptr-left_child);

postorder(ptr-right_child);

printf(“%c”,ptr-ch);

}

}

void main()

{

printf(“构建一个二叉树(结点数为n):\n”);

root=create(root);

printf(“前序遍历二叉树:\n”);

preorder(root);

printf(“\n”);

printf(“中序遍历二叉树:\n”);

inorder(root);

printf(“\n”);

printf(“后序遍历二叉树:\n”);

postorder(root);

printf(“\n”);

}

如何用C语言用顺序表和链表分别建一个二叉树

#includestdio.h

typedef struct node

{

char data;

struct node *lchild;

struct node *rchild;

}BitTree;

BitTree *creat_bt(int n);

BitTree *p[10];

int main()

{

BitTree *h;

int n;

printf(“请输入你节点数\n”);

scanf(“%d”,n);

h=creat_bt(n);

return 0;

}

BitTree *creat_bt(int n)

{

BitTree *s,*t;

int j,i;

while(n–)

{

s=new(BitTree);

scanf(“%d%c”,i,s-data);

s-lchild=s-rchild=NULL;

p[i]=s;

if(i==1)

t=s;

else

{

j=i/2;

if(i%2==0)

p[j]-lchild=s;

else

p[j]-rchild=s;

}

}

return t;

}

这个是我用链表创建的二叉树

原创文章,作者:BEUW,如若转载,请注明出处:https://www.506064.com/n/146747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
BEUWBEUW
上一篇 2024-10-31 15:32
下一篇 2024-10-31 15:32

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python中capitalize函数的使用

    在Python的字符串操作中,capitalize函数常常被用到,这个函数可以使字符串中的第一个单词首字母大写,其余字母小写。在本文中,我们将从以下几个方面对capitalize函…

    编程 2025-04-29
  • 利用Python实现两个链表合并为一个有序链表

    对于开发工程师来说,实现两个链表合并为一个有序链表是必须掌握的技能之一。Python语言在链表处理上非常便利,本文将从多个方面详细阐述如何利用Python实现两个链表合并为一个有序…

    编程 2025-04-29
  • Python中set函数的作用

    Python中set函数是一个有用的数据类型,可以被用于许多编程场景中。在这篇文章中,我们将学习Python中set函数的多个方面,从而深入了解这个函数在Python中的用途。 一…

    编程 2025-04-29
  • 三角函数用英语怎么说

    三角函数,即三角比函数,是指在一个锐角三角形中某一角的对边、邻边之比。在数学中,三角函数包括正弦、余弦、正切等,它们在数学、物理、工程和计算机等领域都得到了广泛的应用。 一、正弦函…

    编程 2025-04-29
  • 单片机打印函数

    单片机打印是指通过串口或并口将一些数据打印到终端设备上。在单片机应用中,打印非常重要。正确的打印数据可以让我们知道单片机运行的状态,方便我们进行调试;错误的打印数据可以帮助我们快速…

    编程 2025-04-29
  • Python3定义函数参数类型

    Python是一门动态类型语言,不需要在定义变量时显示的指定变量类型,但是Python3中提供了函数参数类型的声明功能,在函数定义时明确定义参数类型。在函数的形参后面加上冒号(:)…

    编程 2025-04-29
  • Python实现计算阶乘的函数

    本文将介绍如何使用Python定义函数fact(n),计算n的阶乘。 一、什么是阶乘 阶乘指从1乘到指定数之间所有整数的乘积。如:5! = 5 * 4 * 3 * 2 * 1 = …

    编程 2025-04-29
  • Python定义函数判断奇偶数

    本文将从多个方面详细阐述Python定义函数判断奇偶数的方法,并提供完整的代码示例。 一、初步了解Python函数 在介绍Python如何定义函数判断奇偶数之前,我们先来了解一下P…

    编程 2025-04-29
  • 分段函数Python

    本文将从以下几个方面详细阐述Python中的分段函数,包括函数基本定义、调用示例、图像绘制、函数优化和应用实例。 一、函数基本定义 分段函数又称为条件函数,指一条直线段或曲线段,由…

    编程 2025-04-29

发表回复

登录后才能评论