本文目錄一覽:
C語言 二叉樹的建立
從鍵盤輸入字符,然後回車,字符會停留在緩衝區內,之後你每次scanf(“%c”, ch)就會從緩衝區取出一個來
二叉樹怎樣用C語言實現
以下代碼可實現二叉樹的遞歸創建與中序遍歷,但在輸入時需在輸入完有效數據後再連續輸入多個負數才可以。
#include stdio.h
#include stdlib.h
typedef struct BitNode
{
int data;
struct BitNode *lchile,*rchild;
}*BiTree;
BiTree CreateBiTree(BiTree T)
{
int d;
scanf(“%d”,d);
if (d0)
return NULL;
else
{
if (!(T=(BiTree)malloc(sizeof(BiTree))))
{
exit(1);
}
T-data=d;//先根序創建二叉樹
printf(“%d “,T-data);
T-lchile=CreateBiTree(T-lchile);//創建左子樹
T-rchild=CreateBiTree(T-rchild);//創建右子樹
return T;
}
}
void InOrderView(BiTree T)//中序遍歷二叉樹
{
if(T)
{
InOrderView(T-lchile);
printf(“%d “,T-data);
InOrderView(T-rchild);
}
}
void main()
{
BiTree T;
T=CreateBiTree(T);//遞歸創建二叉樹
InOrderView(T);
}
完整正確的C語言二叉樹程序
我有現成的,分享給大家了。
#includestdio.h
#includestdlib.h
#define maxsize 100
typedef struct btnode
{
int data ; //結點數據類型
struct btnode *lchild, *rchild; //定義左、右孩子為指針型
} bitree;
bitree *creat(bitree *t) //創建二叉樹
{
bitree *s,*p,*q;
int x;
scanf(“%d”,x);
while(x!=0)
{
s= ( bitree *)malloc(sizeof(bitree));
s-data=x;
s-lchild=s-rchild=NULL;
if(t==NULL)
t=s;
else
{
p=t;
while(p)
{
q=p;
if(s-datap-data)
p=p-lchild;
else
p=p-rchild;
}
if(s-dataq-data)
q-lchild=s;
else
q-rchild=s;
}
scanf(“%d”,x);
}
return(t);
}
bitree *insert(bitree *t) //插入結點
{
bitree *s,*p,*q;
int x;
scanf(“%d”,x);
while(x!=0)
{
s= ( bitree *)malloc(sizeof(bitree));
s-data=x;
s-lchild=s-rchild=NULL;
if(t==NULL)
t=s;
else
{
p=t;
while(p)
{
q=p;
if(s-datap-data)
p=p-lchild;
else
p=p-rchild;
}
if(s-dataq-data)
q-lchild=s;
else
q-rchild=s;
}
scanf(“%d”,x);
}
return(t);
}
void search(bitree *t,int k) //查找數據
{
int flag=0;
while(t!=NULL)
{
if(t-data==k)
{
printf(“已查到要找的數!\n”);
flag=1;
break;
}
else
if(t-datak)
t=t-rchild;
else
t=t-lchild;
}
if(flag!=1)
printf(“沒有找到要查找的數據!\n”);
}
bitree *dele(bitree *t,int k) //刪除數據
{
int flag;
bitree *p,*q,*s=NULL,*f;
f=t;
while(t!=NULL) //查找數據
{
if(t-data==k)
{
printf(“已找到所查找的數!\n”);
break;
}
else
if(t-datak)
{
p=t;
t=t-rchild;
flag=0;
}
else
{
p=t;
t=t-lchild;
flag=1;
}
}
if(t-lchild==NULLt-rchild==NULL) //刪除葉子結點
{
free(t);
if(flag==0)
p-rchild=NULL;
else
p-lchild=NULL;
}
else
{
if(t-lchild==NULLt-rchild!=NULL) //刪除只有右子樹的結點
{
if(flag==0)
p-rchild=t-rchild;
else
p-lchild=t-rchild;
free(t);
}
else
{
if(t-lchild!=NULLt-rchild==NULL) //刪除只有左子樹的結點
{
if(flag==0)
p-rchild=t-lchild;
else
p-lchild=t-lchild;
free(t);
}
else //刪除左右子樹都有的結點
{
p=t;
t=t-lchild;
q=t;
while(t-rchild)
{
q=t;
t=t-rchild;
}
if(t==q)
{
p-data=t-data;
p-lchild=t-lchild;
free(t);
}
else
{
p-data=t-data;
q-rchild=t-lchild;
free(t);
}
}
}
}
return(f);
}
void output(bitree * t) //實現二叉樹的遍歷
{
bitree *q[maxsize],*p;
int f,r;
q[0]=t;
f=r=0;
while (f=r)
{
p=q[f];
f++;
printf(“%d “,p-data);
if(p -lchild!=NULL)
{
r++;
q[r]=p-lchild;
}
if (p-rchild!=NULL)
{
r++;
q[r]=p-rchild;
}
}
}
void main()
{
bitree *q=NULL,*r;
int m,n,x=1;
while(x==1)
{
system(“cls”);
printf(” ********************************\n”);
printf(” 創建請按1\n”);
printf(” 插入請按2\n”);
printf(” 查找請按3\n”);
printf(” 刪除請按4\n”);
printf(” 顯示請按5\n”);
printf(” 退出請按0\n”);
printf(” ********************************\n”);
scanf(“%d”,m);
switch(m)
{
case 1:printf(“請輸入數據並以’0’結束\n”);
r=creat(q);system(“pause”);break;
case 2:printf(“請輸入數據並以’0’結束\n”);
r=insert(r);break;
case 3:printf(“請輸入要查找的數:”);
scanf(“%d”,n);
search(r,n);
system(“pause”);
break;
case 4:printf(“請輸入要刪除的數:”);
scanf(“%d”,n);
r=dele(r,n);
printf(“已刪除輸入數據!\n”);
system(“pause”);
break;
case 5:output(r);system(“pause”);printf(“\n”);
break;
case 0:x=0;break;
}
}
}
二叉樹c語言實現
#includeiostream.h
#include stdio.h
#include stdlib.h
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree T)
{
char ch;
ch=getchar();
if (ch == ‘ ‘)
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T-data=ch;//生成根節點
CreatBiTree(T-lchild);//構造左子樹
CreatBiTree(T-rchild);//構造右子樹
}
}
void preorder(BiTree T)//前序遍歷
{
if (T!=NULL){
printf (“%c”,T-data);
preorder(T-lchild);
preorder(T-rchild);
}
}
void inorder(BiTree T)//中序遍歷
{
if (T!=NULL){
inorder(T-lchild);
printf (“%c”,T-data);
inorder(T-rchild);
}
}
void postorder(BiTree T)//後序遍歷
{
if (T!=NULL){
postorder(T-lchild);
postorder(T-rchild);
printf (“%c”,T-data);
}
}
void main ()
{
cout”請輸入要創建的二叉樹包括空格:”endl ;
BiTree T;
CreatBiTree(T);//創建二叉樹
cout”前序遍歷的結果為:”endl;
preorder(T);
coutendl;
cout”中序遍歷的結果為:”endl;
inorder(T);
coutendl;
cout”後序遍歷的結果為:”endl;
postorder(T);
}
數據結構二叉樹的程序,用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\n”, 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。
二叉樹的C語言程序求教
typedef char DataType;//給char起個別名 DataType
//定義二叉樹的數據結構
typedef struct node{
DataType data;//二叉樹存儲的數據
struct node *lchild, *rchild;//二叉樹的左、右子樹的指針
}BinTNode;
typedef BinTNode *BinTree ; //自定義一個數據類型(二叉樹的指針類型)
#include stdio.h
#include malloc.h
//以前序遍歷構建二叉樹
void CreatBinTree(BinTree *T)
{
char ch;//定義臨時變量ch,用來接受數據
if ((ch=getchar())==’ ‘)
*T=NULL;//如果輸入為空,則停止構建二叉樹
else{*T=(BinTNode *)malloc(sizeof(BinTNode));//為二叉樹分配內存空間
(*T)-data=ch;//把輸入的數據存入二叉樹
CreatBinTree((*T)-lchild);//構建左子樹(遞歸)
CreatBinTree((*T)-rchild);//構建左子樹(遞歸)
}
}
//求二叉樹的結點數
int Node(BinTree T)
{ int static nodes=0;//定義一個變量存儲二叉樹的結點數
if(T)//如果二叉樹不為空(是結點),執行此語句
{
Node(T-lchild);//看左子樹是不是個結點(遞歸)
nodes++;//結點數加1
Node(T-rchild);//看右子樹是不是個結點(遞歸)
}
return nodes;//返回結點數
}
//求二叉樹的葉子數
int Leaf(BinTree T)
{ int static leaves=0;//定義一個變量存儲二叉樹的葉子數
if(T)//如果二叉樹不為空,執行此語句
{
Leaf(T-lchild);//看左子樹是不是葉子(遞歸)
if(!(T-lchild||T-rchild))//如果二叉樹T的左、右結點都為空,則執行此語句(即是葉子)
leaves++;//葉子數加1
Leaf(T-rchild);//看右子樹是不是葉子(遞歸)
}
return leaves;//返回葉子數
}
#include stdio.h
void main()
{ BinTree root;
CreatBinTree(root);//構建二叉樹
int nodes=Node(root);//求此二叉樹的結點數
int leaves=Leaf(root);//求此二叉樹的葉子數
printf(“\nnodes=%d leaves=%d”,nodes,leaves);
}
上面是我的理解,好久沒有寫過代碼了,如有錯誤,請指出。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249468.html