雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言 二叉樹的鏈?zhǔn)酱鎯?shí)例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊:

二叉樹的鏈?zhǔn)酱鎯?/strong>

實(shí)現(xiàn)二叉樹的基本操作:建立、遍歷、計(jì)算深度、結(jié)點(diǎn)數(shù)、葉子數(shù)等。

輸入C,先序創(chuàng)建二叉樹,#表示空節(jié)點(diǎn);

輸入H:計(jì)算二叉樹的高度;

輸入L:計(jì)算二叉樹的葉子個(gè)數(shù);

輸入N:計(jì)算二叉樹節(jié)點(diǎn)總個(gè)數(shù);

輸入1:先序遍歷二叉樹;

輸入2:中序遍歷二叉樹;

輸入3:后續(xù)遍歷二叉樹;

輸入F:查找值=x的節(jié)點(diǎn)的個(gè)數(shù);

輸入P:以縮格文本形式輸出所有節(jié)點(diǎn)。

很簡單就不需要多解釋了,代碼貼上

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/*二叉樹的鏈?zhǔn)酱鎯Ρ硎?/
typedef char DataType; /*應(yīng)由用戶定義DataType的實(shí)際類型*/
typedef struct node
{
 DataType data;
 node *lchild, *rchild; /*左右孩子指針*/
} BinTNode;   /*結(jié)點(diǎn)類型*/
typedef BinTNode *BinTree;
int sum=0;
void DisplayBinTree(BinTree T); /*用格文本形式表示二叉樹*/
void CreateBinTree(BinTree *T); /*構(gòu)造二叉鏈表*/
void Preorder(BinTree T); /*前序遍歷二叉樹*/
void Inorder(BinTree T); /*中序遍歷二叉樹*/
void Postorder(BinTree T); /*后序遍歷二叉樹*/
int nodes(BinTree T);  /*計(jì)算總結(jié)點(diǎn)數(shù)*/
int leafs(BinTree T);  /*計(jì)算總?cè)~子數(shù)*/
int hight(BinTree T);  /*計(jì)算二叉樹的高度*/
int find(BinTree T,char x); //查找值=x的節(jié)點(diǎn)的個(gè)數(shù);
int main()
{
 BinTree T;
 char flg;
 while(cin>>flg)
 switch(flg)
 {
 case'C':
  getchar();
  CreateBinTree(&T);
  cout<<"Created success!"<<endl;
  break;
 case'H':
  cout<<"Height="<<hight(T)<<"."<<endl;
  break;
 case'L':
  cout<<"Leaf="<<leafs(T)<<"."<<endl;
  break;
 case'N':
  cout<<"Nodes="<<nodes(T)<<"."<<endl;
  break;
 case'1':
  printf("Preorder is:");
  Preorder(T);
  cout<<"."<<endl;
  break;
 case'2':
  printf("Inorder is:");
  Inorder(T);
  cout<<"."<<endl;
  break;
 case'3':
  printf("Postorder is:");
  Postorder(T);
  cout<<"."<<endl;
  break;
 case'F':
  char x;
  int ko;
  getchar();
  cin>>x;
  ko=find(T,x);
  cout<<"The count of "<<x<<" is "<<ko<<"."<<endl;
  break;
 case'P':
  cout<<"The tree is:"<<endl;
  DisplayBinTree(T);
  break;
 default:
  cout<<"輸入有誤,請重新輸入"<<endl;
 }
}

/*構(gòu)造二叉鏈表*/
void CreateBinTree(BinTree *T)
{
 char ch;
 if ((ch=getchar())=='#')
 *T=NULL;
 else
 {
 /*讀入非空格*/
 *T=(BinTNode *)malloc(sizeof(BinTNode));/*生成結(jié)點(diǎn)*/
 (*T)->data=ch;
 CreateBinTree(&(*T)->lchild );  /*構(gòu)造左子樹*/
 CreateBinTree(&(*T)->rchild );  /*構(gòu)造右子樹*/
 }
}
/*用縮格文本形式表示二叉樹*/
void DisplayBinTree(BinTree T)
{
 BinTree stack[100],p;
 int level[100],top,n,i;
 if (T)
 {
 top=1;
 stack[top]=T;
 level[top]=0;
 while(top>0)
 {
  p=stack[top];
  n=level[top];
  for (i=1; i<=n; i++)
  cout<<" ";
  printf("%c\n",p->data);
  top--;
  if (p->rchild!=NULL)
  {
  top++;
  stack[top]=p->rchild;
  level[top]=n+2;
  }
  if (p->lchild!=NULL)
  {
  top++;
  stack[top]=p->lchild;
  level[top]=n+2;
  }
 }
 }
}
/*計(jì)算總結(jié)點(diǎn)數(shù)*/
int nodes(BinTree T)
{
 if(T)
 {
 if( (T->lchild==NULL)&&(T->rchild==NULL))
  return 1;
 else
  return nodes(T->lchild)+nodes(T->rchild)+1;
 }
 return 0;
}
/*計(jì)算總?cè)~子數(shù)*/
int leafs(BinTree T)
{
 if(T)
 {
 if ((T->lchild==NULL)&&(T->rchild==NULL))
  return 1;
 else
  return leafs(T->lchild)+leafs(T->rchild);
 }
 return 0;
}
/*計(jì)算樹的高度*/
int hight(BinTree T)
{
 if(T)
 {
 if ((T->lchild==NULL)&&(T->rchild==NULL))
  return 1;

 else if((T->lchild==NULL)&&(T->rchild))
  return 1+hight(T->rchild);

 else if((T->lchild)&&(T->rchild==NULL))
  return 1+hight(T->lchild);

 else
  return hight(T->lchild)+hight(T->rchild);
 }
 return 0;
}
/*前序遍歷二叉樹*/
void Preorder(BinTree T)
{
 if(T)
 {
 printf("%c ",T->data); /*訪問結(jié)點(diǎn)*/
 Preorder(T->lchild);
 Preorder(T->rchild);
 }
}
/*中序遍歷二叉樹*/
void Inorder(BinTree T)
{
 if(T)
 {
 Inorder(T->lchild);
 printf("%C ",T->data);
 Inorder(T->rchild);
 }
}
/*后序遍歷二叉樹*/
void Postorder(BinTree T)
{
 if(T)
 {
 Postorder(T->lchild);
 Postorder(T->rchild);
 printf("%C ",T->data);
 }
}
int find(BinTree T,char x)
{
 if(T)
 {
 if((T->data)==x)
  sum++;
 find(T->lchild,x);
 find(T->rchild,x);

 }
 return sum;
}

以上就是二叉樹鏈?zhǔn)酱鎯Φ囊粋€(gè)小實(shí)例,需學(xué)習(xí)要的同學(xué)請參考,謝謝支持

上一篇:C++11的新特性簡單匯總介紹 (二)

欄    目:C語言

下一篇:C語言二進(jìn)制思想以及數(shù)據(jù)的存儲

本文標(biāo)題:C語言 二叉樹的鏈?zhǔn)酱鎯?shí)例

本文地址:http://m.jygsgssxh.com/a1/Cyuyan/2168.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有