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

c语言创建二叉树switch

发布时间: 2022-08-09 21:05:52

c语言二叉树的创建和遍历

我写了一个二叉树 你给看看 一定能行的 我自己用了
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
#define Max 20 //结点的最大个数
typedef struct BinTNode{
char data;
struct BinTNode *lchild,*rchild;
}BinTNode,*BinTree; //自定义二叉树的结点类型
//定义二叉树的指针
int NodeNum,leaf; //NodeNum为结点数,leaf为叶子数

//==========以广义表显示二叉树==============
void DisTree(BinTree T)
{
if(T)
{
printf("%c",T->data);
if((T->lchild)||(T->rchild))
{
if(T->lchild)
{
printf("%c",'(');
DisTree(T->lchild);
}
if(T->rchild)
{
printf("%c",',');
DisTree(T->rchild);
printf("%c",')');
}
}
}
}
//==========基于先序遍历算法创建二叉树==============
//=====要求输入先序序列,其中加入虚结点"#"以示空指针的位置==========
BinTree CreatBinTree(BinTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BinTNode *)malloc(sizeof(BinTNode))))
printf("Error!");
T->data=ch;
T->lchild=CreatBinTree(T->lchild);
T->rchild=CreatBinTree(T->rchild);
}
return T;
}
//========NLR 先序遍历=============
void Preorder(BinTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
//========LNR 中序遍历===============
void Inorder(BinTree T)
{
if(T){
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
}
//==========LRN 后序遍历============
void Postorder(BinTree T)
{
if(T){
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c",T->data);
}
}
//=====采用后序遍历求二叉树的深度、结点数及叶子数的递归算法========
int TreeDepth(BinTree T)
{
int hl,hr,max;
if(T){
hl=TreeDepth(T->lchild); //求左深度
hr=TreeDepth(T->rchild); //求右深度
max=hl>hr? hl:hr; //取左右深度的最大值
NodeNum=NodeNum+1; //求结点数
if(hl==0&&hr==0)
leaf=leaf+1; //若左右深度为0,即为叶子。
return(max+1);
}
else return(0);
}
//====利用"先进先出"(FIFO)队列,按层次遍历二叉树==========
void Levelorder(BinTree T)
{
int front=0,rear=1;
BinTNode *cq[Max],*p; //定义结点的指针数组cq
cq[1]=T; //根入队
while(front!=rear)
{
front=(front+1)%NodeNum;
p=cq[front]; //出队
printf("%c",p->data); //出队,输出结点的值
if(p->lchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->lchild; //左子树入队
}
if(p->rchild!=NULL){
rear=(rear+1)%NodeNum;
cq[rear]=p->rchild; //右子树入队
}
}
}
//==========主函数=================
void main()
{
BinTree T,root;
int i,depth;
printf("\n");
printf("输入完全二叉树的先序序列:"); //输入完全二叉树的先序序列,
// 用#代表虚结点,如ABD###CE##F##
root=CreatBinTree(T); //创建二叉树,返回根结点
DisTree(root);
printf("\n");
do //从菜单中选择遍历方式,输入序号。
{
printf("\t********** 菜单 ************\n");
printf("\n");
printf("\t1: 先序遍历\n");
printf("\t2: 中序遍历\n");
printf("\t3: 后序遍历\n");
printf("\t4: 该树的深度,结点数,叶子数\n");
printf("\t5: 层次遍历\n"); //按层次遍历之前,先选择4,求出该树的结点数。
printf("\t0: 退出\n");
printf("\t*******************************\n");
scanf("%d",&i);
//输入菜单序号(0-5)
switch(i)
{
case 1: {printf("Print Bin_tree Preorder: ");
Preorder(root); //先序遍历
}break;
case 2: {printf("Print Bin_Tree Inorder: ");
Inorder(root); //中序遍历
}break;
case 3: {printf("Print Bin_Tree Postorder: ");
Postorder(root); //后序遍历
}break;
case 4: {depth=TreeDepth(root); //求树的深度及叶子数
printf("树深=%d 树总结点数=%d",depth,NodeNum);
printf(" 树叶子数=%d",leaf);
}break;
case 5: {printf("LevePrint Bin_Tree: ");
Levelorder(root); //按层次遍历
}break;
default: exit(1);
}
}while(i>=0&&i<6);
}

兄弟你看看 不懂再往下留言 记得给我的劳动成果一点点奖励哦!!

Ⅱ 用C语言编写程序,创建一个二叉树的二叉链表结构,然后输出从根结点到所有叶子结点的路径。

#include
#include
#include
typedef
struct
node
{
char
data;
struct
node
*lchild;
struct
node
*rchild;
}tnode;
tnode
*createtree()
{
tnode
*t;
char
ch;
ch=getchar();
if(ch=='0')
t=null;
else
{
t=(tnode
*)malloc(sizeof(tnode));
t->data=ch;
t->lchild=createtree();
t->rchild=createtree();
}
return
t;
}
void
listtree(tnode
*t)
{
if
(t!=null)
{
printf("%c",t->data);
if(t->lchild!=null||t->rchild!=null)
{
printf("(");
listtree(t->lchild);
if(t->rchild!=null)
printf(",");
listtree(t->rchild);
printf(")");
}
}
}
void
inorder(tnode
*t)
{
if(t!=null)
{
inorder(t->lchild);
printf("%c\t",t->data);
inorder(t->rchild);
}
}
void
leve(tnode
*t)
{
tnode
*quee[100];
int
front,rear;
front=-1;
rear=0;
quee[rear]=t;
while(front!=rear)
{
front++;
printf("%c\t",quee[front]->data);
if(quee[front]->lchild!=null)
{
rear++;
quee[rear]=quee[front]->lchild;
}
if(quee[front]->rchild!=null)
{
rear++;
quee[rear]=quee[front]->rchild;
}
}
}
main()
{
tnode
*t=null;
printf("请输入二叉树元素:");
t=createtree();
printf("广义表的输出:");
listtree(t);
printf("\n");
printf("二叉树的中序遍历:");
inorder(t);
printf("\n");
printf("二叉树的层次遍历:");
leve(t);
printf("\n");
system("pause");
}
/*
输入:ab00cd00e00f000
输出:a(b,c((d,e))
中序遍历:
b
a
d
c
e
层次遍历:a
b
c
d
e
*/

Ⅲ C语言 二叉树建立与指针

2. &和scanf里面的&一样是为了取地址。
1. 传入二级指针是为了修改左右孩子。 createbintree(&(*t)->lchild);和createbintree(&(*t)->rchild)这里如果不用二级指针,那就只能传入左右孩子的值,无法无法修改它们的值。
一般情况下(不用引用的情况下),函数传变量的值的时候就是使用变量的值,也就是变量的一个临时拷贝;而传它的地址的时候一般是为了修改此变量(在函数内可以通过地址找到变量位置,进而修改它)。想想交换变量值的函数为什么要写成 void switch(int *a,int* b)这种形式,就能够明白了。
3. 第一个问题说这么多,第三个就可以简单地说了。void initstack(Stack st) 这种写法,是把一个外部的Stack类的变量拷贝给了st,而那个变量的确是没有初始化的,所以编译错误(应该是警告吧)。而void initstack(Stack *st)这种方式就是传进了那个变量的地址,这样就能在函数内部修改它从而对其初始化了,当然也不会提示错误了。

说这么多,不知说的是否清晰,有什么问题可以继续问

Ⅳ 求数据结构(C语言版)建立二叉树的代码~~急~~谢谢了

BT.H文件
#include
<stdio.h>
#include
<malloc.h>
#include
<conio.h>
#define
TRUE
1
#define
FALSE
0
#define
ERROR
0
#define
OK
1
#define
Stack_Size
50
#define
NUM
50
#define
MAXSIZE
50
//队列的最大长度
//定义二叉树
typedef
char
DataType;
typedef
struct
Node
{
DataType
data;
struct
Node
*LChild;
struct
Node
*RChild;
}BiTNode,
*BiTree;
//定义stack
typedef
BiTree
StackElementType;
typedef
struct
{
StackElementType
elem[Stack_Size];
int
top;
}SeqStack;
//定义队列
typedef
BiTree
QueueElementType;
typedef
struct
{
QueueElementType
element[MAXSIZE];
int
front;
int
rear;
}SeqQueue;
//队列的抽象
void
InitQueue(SeqQueue
*Q)
{
Q->front=Q->rear=0;
}
int
EnterQueue(SeqQueue
*Q,
QueueElementType
x)
{
if((Q->rear+1)%MAXSIZE==Q->front)
return(FALSE);
Q->element[Q->rear]=x;
Q->rear=(Q->rear+1)%MAXSIZE;
return(TRUE);
}

Ⅳ C语言创建二叉树 遍历 求深度 求解!!

测试结果:
At the first,we create a tree
Please input nodes of tree
a
b
c
#
#
d
e
#
g
#
#
f
#
#
#
abcdegf
cbegdfa
cgefdba

The count of leaves is: 3

The height of tree is 5
请按任意键继续. . .

【楼主】 建树的函数是一个递归的过程
我上面测试的这颗树
a
/
b
/ \
c d
/ \
e f
\
g

仔细看我的建树的时候的输入!

为了测试上面的5个函数,我把case去掉了。你补上去!
【你的CreateBinTree()】是没有参数的,所以你后面的申明不能带参数

测试代码:
#include<stdio.h>
#include<stdlib.h>

typedef struct bnode{
char data;
struct bnode *lchild;
struct bnode *rchild;
}*BTree,Bnode;

BTree CreateBinTree()
{
char ch;
BTree t;
ch=getchar();
getchar();
if(ch=='#')
t=NULL;
else
{
t=(BTree)malloc(sizeof(Bnode));
t->data=ch;
t->lchild=CreateBinTree();
t->rchild=CreateBinTree();
}
return t;
}

void PreOrder(BTree t)
{
if(t)
{
printf("%c",t->data);
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}

void InOrder(BTree t)
{
if(t)
{
InOrder(t->lchild);
printf("%c",t->data);
InOrder(t->rchild);
}
}

void PostOrder(BTree t)
{
if(t)
{
PostOrder(t->lchild);
PostOrder(t->rchild);
printf("%c",t->data);
}
}

int Height(BTree t)
{
int h1,h2;
if(t==NULL)
return 0;
else
{
h1=Height(t->lchild);
h2=Height(t->rchild);
if(h1>h2)
return h1+1;
else
return h2+1;
}
}

int LeafCount(BTree t)
{
int count1,count2;
if(t==NULL)
return 0;
else
{
if(t->lchild==NULL&&t->rchild==NULL)
return 1;
else
{
count1=LeafCount(t->lchild);
count2=LeafCount(t->rchild);
return count1+count2;
}
}
}

main()
{ BTree root;/*定义指向树的指针*/
int t=1;
printf("At the first,we create a tree\n");
printf("Please input nodes of tree\n");
root=CreateBinTree();//调用函数创建树
if (root==NULL) printf("It's an empty tree!\n");
else
{

PreOrder(root); printf("\n");
InOrder(root); printf("\n");
PostOrder(root); printf("\n");
printf("\nThe count of leaves is: %d \n",LeafCount(root));
printf("\nThe height of tree is %d \n",Height(root));
system("pause");
}
}

Ⅵ 二叉树的建立与遍历(C语言)

楼主你好~~~“ф”字符的源代码我忘记了,我这里有一个自己写过的遍历算法
#include<iostream.h>
typedef struct btnode
{
char data;
struct btnode *Lchild,*Rchild;
}*bitreptr;

void Create(bitreptr &p)
{
char n;
p=new btnode;
cin>>n;
if(n!='#')
{
p->data=n;
Create(p->Lchild);
Create(p->Rchild);

}
else
p=NULL;

}

void preorder(bitreptr &p)
{
if(p)
{
cout<<p->data<<" ";
preorder(p->Lchild);
preorder(p->Rchild);
}
}

void midorder(bitreptr &p)
{
if(p)
{
midorder(p->Lchild);
cout<<p->data<<" ";
midorder(p->Rchild);
}
}

void postorder(bitreptr &p)
{
if(p)
{
postorder(p->Lchild);
postorder(p->Rchild);
cout<<p->data<<" ";
}
}

void change(bitreptr &p)
{
bitreptr t,q;
if(p)
{
t=p->Lchild;
q=p->Rchild;
p->Lchild=q;
p->Rchild=t;
change(p->Lchild);
change(p->Rchild);
}
}

void main()
{
char i;
cout<<"请选择所需功能('A'输出该二叉树序列,'B'输出交换后二叉树序列)"<<endl;
cin>>i;
bitreptr p;
cout<<"输入数据:";
Create(p);
switch(i)
{
case 'A':
{
cout<<"前序:";
preorder(p);
cout<<endl;
cout<<"中序:";
midorder(p);
cout<<endl;
cout<<"后序:";
postorder(p);
cout<<endl;
}break;
case 'B':
{
change(p);
cout<<"交换二叉树前序:";
preorder(p);
cout<<endl;
cout<<"交换二叉树中序:";
midorder(p);
cout<<endl;
cout<<"交换二叉树后序:";
postorder(p);
cout<<endl;
}break;
}

}

这个算法输入时要以“#”代表空节点,及将[测试数据] “ABCффDEфGффFффф”改成“ABC##DE#G##F###”即可。另外我的算法包括了二叉树左右子树交换的代码“change(bitreptr &p)”,只要楼主稍作修改就可以得到你想要的完美结果~

Ⅶ 用C语言建立一棵二叉树,使用二杈链表存储,对其进行后续遍历,输出后序遍历序列

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#define Maxsize 100
typedef int datatype;

typedef struct node
{
datatype data;
struct node* lchild;
struct node* rchild;
}BTNode;

void CreatBTNode(BTNode *&b,char * str)
{
BTNode *p,*st[Maxsize];
int top=-1;
p=NULL;
b=NULL;
int j=0,k;
char ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;st[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
{
b=p;
}
else
{
switch(k)
{
case 1:st[top]->lchild=p;break;
case 2:st[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}

}

void DispBTNode(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{
printf("(");
DispBTNode(b->lchild);
if(b->rchild!=NULL)
printf(",");
DispBTNode(b->rchild);
printf(")");
}
}

}

BTNode *FindNode(BTNode *b,char x)
{
BTNode *p=NULL;
if(b==NULL)
{
return NULL;
}
else if(b->data==x)
{
return b;
}
else
{
p=FindNode(b->lchild,x);
if(p!=NULL)
{
return p;
}
else
{
return FindNode(b->rchild,x);
}
}
}

void main()
{
BTNode *b,*q;
char str[100];
printf("您输入的二叉树为\n");
scanf("%s",&str);
CreatBTNode(b,str);
DispBTNode(b);
q=FindNode(b,'A');
printf("\n");
printf("*********************************\n");
printf("%c\n",q->data);

}

Ⅷ 数据结构二叉树的创建(c语言版)

//输入的格式为:abd##e##c##

#include "stdlib.h"

typedef int Element;

struct Tree
{
Element data;
struct Tree *left;
struct Tree *right;
};

void CreateBinSortTree(struct Tree **t);
void InsertNode2Tree(struct Tree **root, Element num);

void PrintTree(struct Tree *r, int order);

int main(int argc, char* argv[])
{
printf("请输入一组字母(#表示子树为空)\n");
struct Tree *t=NULL;
CreateBinSortTree(&t);

printf("\n根左右:");
PrintTree(t,1);

printf("\n左根右:");
PrintTree(t,2);

printf("\n左右根:");
PrintTree(t,3);

printf("\n");
return 0;
}

void CreateBinSortTree(struct Tree **t)
{
char ch;
ch = getchar();
if (ch == '#')
{
*t = NULL;
return ;
}
*t = (struct Tree *)malloc(sizeof(struct Tree));
(*t)->data = ch;
CreateBinSortTree( &((*t)->left));
CreateBinSortTree( &((*t)->right));

}

void PrintTree(struct Tree *r, int order)
{
if (r == NULL)
{
return ;
}
switch(order)
{

case 1:
printf("%c ",r->data);
PrintTree(r->left,order);
PrintTree(r->right,order);
break;
case 2:
PrintTree(r->left,order);
printf("%c ",r->data);
PrintTree(r->right,order);
break;
case 3:
PrintTree(r->left,order);
PrintTree(r->right,order);
printf("%c ",r->data);
break;
}

}

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

}


(9)c语言创建二叉树switch扩展阅读:

简单二叉树定义范例:此树的顺序结构为: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语言数据结构:怎么建立一个二叉树

只要将一个二叉树用“括号表示法”表示出来,然后,用链式存储结构将其各个结点存储就可以了,也就是输入一个二叉树。最后,用中序遍历输出!
typedef struct node
{ ElemType data;
struct node *lchild,*rchild;
} BTNode;
//创建一个二叉树;
void CreateBTNode(BTNode * &b,char *str)
{ BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL; //建立的二叉树初始时为空
ch=str[j];
while (ch!='\0') //str未扫描完时循环
{ switch(ch)
{
case '(':top++;St[top]=p;k=1; break;
//为左孩子结点
case ')':top--;break;
case ',':k=2; break;
//为孩子结点右结点
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if (b==NULL) ///p为二叉树的根结点
b=p;
else //已建立二叉树根结点
{ switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}
}
//中序遍历输出;
void InOrder(BTNode *b)
{ if (b!=NULL)
{ InOrder(b->lchild);
printf("%c ",b->data); //访问根结点
InOrder(b->rchild);
}
}
OK!
顺便发一份给你,注意接收!