c語言二叉樹創建
❶ c語言二叉樹先序建立求助啊!!
把typedef去掉
❷ 數據結構二叉樹的創建(c語言版)
http://blog.csdn.net/odaynot/article/details/8107368
❸ C語言二叉樹的建立
typedef struct BT
{
char data;
struct BT *lchild,*rchild;
}BTree;
BTree *create(BTree * t)
{
char p;
p=getch();
if(p=='#')
t=NULL;
else
{
t=(BTree *)malloc(sizeof(t));
t->data=p;
t->lchild=create(t->lchild);
t->rchild=create(t->rchild);
}
return t;
}
void preorder(BTree *t)
{
if(t)
{
printf("%c ",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
BTree *t;
t=create(t);
preorder(t);
}
❹ 求數據結構(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語言二叉樹的創建和遍歷
我寫了一個二叉樹 你給看看 一定能行的 我自己用了
#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<iostream.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}bitnode,*bitree;
bitree put()
{
char ch;
bitree t;
cin>>ch;
if(ch=='@')
t=NULL;
else
{
if(!(t=(bitree)malloc(sizeof(bitnode))))
return 0;
t->data=ch;
t->lchild=put();
t->rchild=put();
}
return t;
}
void print(bitree t)
{
if(t)
{
print(t->lchild);
cout<<t->data;
print(t->rchild);
}
}
void show(bitree t)
{
bitree stack[15];
bitree p;
int top;
top=0;
p=t;
while(!(p==NULL&&top==0))
{
while(p!=NULL)
{
cout<<p->data;
if(top<15)
{
stack[top]=p;
top++;
}
else
{
cout<<"over:";
return ;
}
p=p->lchild;
}
if(top<=0)
return ;
else
{
top--;
p=stack[top];
p=p->rchild;
}
}
}
int coutleaf(bitree t)
{
if(t->rchild==NULL&&t->rchild==NULL)
return (1);
return(coutleaf(t->lchild)+coutleaf(t->rchild));
}
void main()
{
bitree q;
q=put();
cout<<"中序:";
print(q);
cout<<endl;
cout<<"非遞歸:";
cout<<endl;
show(q);
cout<<endl;
cout<<coutleaf(q);
cout<<endl;
}
❼ 請問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;
}
(7)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);
}
}
❽ C語言 二叉樹的建立
關鍵在這行 if(ch!='#') 如果不等於'#'就繼續輸入它的左子樹和右子樹。