求二叉樹節點個數、葉子節點、節點層次與寬度

需實現:(1)輸出二叉樹b的節點個數

? ? ? ? ? ? ? (2)輸出二叉樹b的葉子節點個數

? ? ? ? ? ? ? (3)求二叉樹b中指定節點值(假設所有節點值不同)的節點的層次。

? ? ? ? ? ? ? (4)利用層次遍歷求二叉樹b的寬度

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
const int MaxSize=10000;
using namespace std;
typedef struct node
{char data;struct node * lchild;struct node * rchild;
}BTNode;
typedef struct qnode
{BTNode *data;struct qnode *next;
}DataNode;
typedef struct
{int Size;DataNode *front;DataNode *rear;
}SqQueue;
void InitQueue(SqQueue * &q)
{q=(SqQueue *)malloc(sizeof(SqQueue));q->front=q->rear=NULL;q->Size=0;
}
bool QueueEmpty(SqQueue *q)
{return (q->rear==NULL);
}
void enQueue(SqQueue * &q,BTNode *e)
{DataNode *p;p=(DataNode *)malloc(sizeof(DataNode));p->data=e;p->next=NULL;if(q->rear==NULL)q->front=q->rear=p;else{q->rear->next=p;q->rear=p;}q->Size++;
}
bool deQueue(SqQueue * &q,BTNode * &e)
{DataNode * t;if(q->rear==NULL)return false;t=q->front;if(q->front==q->rear)q->front=q->rear=NULL;elseq->front=q->front->next;e=t->data;free(t);q->Size--;return true;
}
int QueueSize(SqQueue * q)
{return q->Size;
}
void CreatBTree(BTNode * &b,char * str)         //建立二叉樹
{BTNode *St[MaxSize],*p;int top=-1,k,j=0;char ch;b=NULL;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];}
}
int yznum;
char yz[MaxSize];
void YZjiedian(BTNode *b)    //找葉子節點的個數
{if(b!=NULL){if(b->lchild==NULL&&b->rchild==NULL) {yznum++;yz[yznum]=b->data;}YZjiedian(b->lchild);YZjiedian(b->rchild);}
}
int jdnum;
char jd[MaxSize];
void JieDian(BTNode *b)      //找節點的個數
{if(b!=NULL){jdnum++;jd[jdnum]=b->data;JieDian(b->lchild);JieDian(b->rchild);}
}
void PreOrder(BTNode *b)       //先序遍歷
{if(b!=NULL){printf("%c ",b->data);PreOrder(b->lchild);PreOrder(b->rchild);}
}
void InOrder(BTNode *b)      //中序遍歷
{if(b!=NULL){InOrder(b->lchild);printf("%c ",b->data);InOrder(b->rchild);}
}
void PostOrder(BTNode *b)       //后序遍歷
{if(b!=NULL){PostOrder(b->lchild);PostOrder(b->rchild);printf("%c ",b->data);}
}
int result;
void LevelOrder(BTNode *b)         //求二叉樹的最大寬度
{BTNode *nape;nape=b;int cnt;BTNode *p;SqQueue *qu;InitQueue(qu);enQueue(qu,b);while(!QueueEmpty(qu)){cnt=QueueSize(qu);result=max(result,cnt);while(cnt--){deQueue(qu,p);if(p->lchild!=NULL)enQueue(qu,p->lchild);if(p->rchild!=NULL)enQueue(qu,p->rchild);}}
}
int FindJieDian(BTNode *b,char c)    //尋找節點層次
{BTNode *nape;nape=b;int cnt;int level=0;BTNode *p;SqQueue *qu;InitQueue(qu);enQueue(qu,b);while(!QueueEmpty(qu)){cnt=QueueSize(qu);level++;while(cnt--){deQueue(qu,p);if(p->data==c)return level;if(p->lchild!=NULL)enQueue(qu,p->lchild);if(p->rchild!=NULL)enQueue(qu,p->rchild);}}return -1;
}
int main()
{char str[MaxSize]={"A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))"};printf("此棵二叉樹為:%s\n",str);BTNode *b;CreatBTree(b,str);JieDian(b);printf("此二叉樹的結點個數為:%d\n",jdnum);printf("此二叉樹的節點為:");for(int i=1;i<=jdnum;i++)printf(" %c",jd[i]);printf("\n");printf("先序遍歷輸出節點為:");PreOrder(b);printf("\n");printf("中序遍歷輸出節點為:");InOrder(b);printf("\n");printf("后序遍歷輸出節點為:");PostOrder(b);printf("\n");YZjiedian(b);printf("此二叉樹的葉子結點個數為:%d\n",yznum);printf("此二叉樹的葉子節點為:");for(int i=1;i<=yznum;i++)printf(" %c",yz[i]);printf("\n");LevelOrder(b);printf("此二叉樹的最大寬度為:%d\n",result);char ch;while(printf("請輸入你要查詢的節點:")!=EOF){scanf(" %c",&ch);int nape=FindJieDian(b,ch);if(nape==-1)printf("二叉樹中無此節點\n");elseprintf("節點%c在二叉樹中的層數為:%d\n",ch,nape);}return 0;
}

?

實現圖片:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536328.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536328.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536328.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UVA - 227?Puzzle

Puzzle UVA - 227 題目傳送門 注意點&#xff1a;每兩個輸出點間有一個換行&#xff0c;但最后一個輸出無換行 惡心模擬題&#xff0c;很卡輸入輸出&#xff01;&#xff01;&#xff01; AC代碼1:(自己的代碼&#xff0c;提交時需要選擇C11) #include <cstdio> #i…

UVA - 232????????Crossword Answers

Crossword Answers UVA - 232 題目傳送門 直接按照要求尋找遍歷一遍即可 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <m…

UVA - 1368????????DNA Consensus String

DNA Consensus String UVA - 1368 題目傳送門 解決方法&#xff1a;尋找每列中出現最多的字母。 AC代碼 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #inc…

UVA - 202?Repeating Decimals

Repeating Decimals UVA - 202 題目傳送門 解決方法&#xff1a;模擬一下除法&#xff0c;及時記錄余數&#xff0c;當一個余數第二次出現時證明開始循環 AC代碼 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #…

UVA - 10340????????All in All

All in All UVA - 10340 題目傳送門 將兩個字符串對比一下即可。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> …

UVA - 1587????????Box

Box UVA - 1587 題目傳送門 解決方法&#xff1a;按照邊在12個長寬出現的次數和出現在幾個矩形里來判定就行了 總共出現一個長度&#xff0c;滿足條件 總共出現兩個長度&#xff0c;則其中一個長度在12個數里出現4次&#xff0c;并在四個矩形中出現 總共出現三個長度&#x…

UVA - 1588????????Kickdown

Kickdown UVA - 1588 題目傳送門 解決方法&#xff1a;上板不動&#xff0c;下板向左移&#xff1b;上板不動&#xff0c;下板向右移。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #inclu…

UVA - 1339????????Ancient Cipher

Ancient Cipher UVA - 1339 題目傳送門 解決方法&#xff1a;模擬一下轉換過程即可。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #i…

UVA - 489????????Hangman Judge

Hangman Judge UVA - 489 題目傳送門 PS.此題Udebug有毒&#xff0c;即使100組樣例全過&#xff0c;但還是WA&#xff0c;心塞。 這是我自己的代碼&#xff0c;悲催的WA了 #include <cstdio> #include <iostream> #include <algorithm> #include <cm…

UVA - 133????????The Dole Queue

The Dole Queue UVA - 133 題目傳送門 模擬一遍過程&#xff0c;注&#xff1a;可能會選中同一個人 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <c…

UVA - 213?Message Decoding

Message Decoding UVA - 213 題目傳送門 emmmm&#xff0c;此題按照紫書上的思路來即可&#xff0c;要么太復雜 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #in…

UVA - 512????????Spreadsheet Tracking

Spreadsheet Tracking UVA - 512 題目傳送門 紫書第二個思路十分巧妙&#xff0c;能用很少的代碼解出此題。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #inclu…

UVA - 1589????????Xiangqi

Xiangqi UVA - 1589 題目傳送門 解決方法&#xff1a;判斷黑棋是否能有可以下的地方 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #in…

UVA - 12412????????A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) UVA - 12412 題目傳送門 emmmm&#xff0c;不想表達什么&#xff0c;udbug上的數據全過&#xff0c;可就是WA。。。。 AC了的代碼&#xff08;大佬的代碼&#xff09; #include <bits/stdc.h> using namespace…

【思維】draw!

題目&#xff1a; You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if t…

【數學】Birthday

題目&#xff1a; Cowboy Vlad has a birthday today! There are nn children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a…

【遞推】Ayoub and Lost Array

題目&#xff1a;Ayoub had an array aa of integers of size nn and this array had two interesting properties: All the integers in the array were between ll and rr (inclusive). The sum of all the elements was divisible by 33. Unfortunately, Ayoub has lost hi…

Super-palindrome【字符串+思維】

Super-palindrome 時間限制: 1 Sec 內存限制: 128 MB 提交: 595 解決: 231 [提交] [狀態] [命題人:admin] 題目描述 You are given a string that is consisted of lowercase English alphabet. You are supposed to change it into a super-palindrome string in minimum ste…

Hakase and Nano【博弈】

Hakase and Nano 時間限制: 1 Sec 內存限制: 128 MB 提交: 533 解決: 155 [提交] [狀態] [命題人:admin] 題目描述 Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs of pebbles, and the i-th pack contains ai pebble…

【思維】過分的謎題

題目描述 2060年是云南中醫學院的百年校慶&#xff0c;于是學生會的同學們搞了一個連續猜謎活動&#xff1a;共有10個謎題&#xff0c;現在告訴所有人第一個謎題&#xff0c;每個謎題的答案就是下一個謎題的線索....成功破解最后一個謎題后&#xff0c;答案就是指向獎勵的線索…