av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

C語言

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

時間:2025-04-22 15:14:52 C語言 我要投稿
  • 相關(guān)推薦

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

  二叉樹作為樹的一種,其節(jié)點(diǎn)至多有兩個子節(jié)點(diǎn)。本文是百分網(wǎng)小編搜索整理的關(guān)于C語言中二叉樹的鏈?zhǔn)酱鎯?shí)例分析,感興趣的朋友一起學(xué)習(xí)吧!!想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

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

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

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

  輸入H:計算二叉樹的高度;

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

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

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

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

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

  輸入F:查找值=x的節(jié)點(diǎn)的個數(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);  /*計算總結(jié)點(diǎn)數(shù)*/

  int leafs(BinTree T);  /*計算總?cè)~子數(shù)*/

  int hight(BinTree T);  /*計算二叉樹的高度*/

  int find(BinTree T,char x); //查找值=x的節(jié)點(diǎn)的個數(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;

  }

  }

  }

  }

  /*計算總結(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;

  }

  /*計算總?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;

  }

  /*計算樹的高度*/

  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;

  }

【C語言中二叉樹的鏈?zhǔn)酱鎯?shí)例分析】相關(guān)文章:

C/C++的浮點(diǎn)數(shù)在內(nèi)存中的存儲方式分析及實(shí)例08-13

C語言條件編譯分析實(shí)例08-18

C語言的結(jié)構(gòu)與聯(lián)合的實(shí)例分析06-30

C++二叉樹的鏡像實(shí)例09-02

C語言double和float 實(shí)例分析用法06-14

C語言程序的存儲區(qū)域09-07

C語言順序存儲結(jié)構(gòu)07-10

C語言變量存儲布局07-05