當前位置:首頁 » 編程語言 » 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!
順便發一份給你,注意接收!