1.编写程序任意输入二叉树的结点个数和结点值,构造一棵二叉树,采用三种递归遍历算法(前序、中序、后序) 1.编写递归算法,计算二叉树中叶子结点的数目
\u7f16\u5199\u7a0b\u5e8f\uff0c\u7528\u5148\u5e8f\u9012\u5f52\u904d\u5386\u6cd5\u5efa\u7acb\u4e8c\u53c9\u6811\u7684\u4e8c\u53c9\u94fe\u8868\u5b58\u50a8\u7ed3\u6784\uff0c\u8f93\u51fa\u5176\u5148\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7b2ck\u4e2a\u8bbf\u95ee\u7ed3\u70b9#include "stdio.h"
#include "malloc.h"
#define ELEMTYPE char
BiTNode *bulid() /*\u5efa\u6811*/
{ BiTNode *q;
BiTNode *s[20];
int i,j;
char x;
printf("\u8bf7\u6309\u987a\u5e8f\u8f93\u5165\u4e8c\u53c9\u6811\u7684\u7ed3\u70b9\u4ee5\u8f93\u51650\u548c*\u53f7\u7ed3\u675f\n");
printf("\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u4e3a\u7b2c\u51e0\u4e2a\u7ed3\u70b9i=\n");
scanf("%d",&i);
printf("\u8bf7\u8f93\u5165\u4f60\u8981\u8f93\u5165\u8be5\u7ed3\u70b9\u7684\u6570\u4e3ax=");
getchar();
scanf("%c",&x);
while(i!=0&&x!='*')
{q=(BiTNode*)malloc(sizeof(BiTNode));
q->data=x;
q->rchild=NULL;
q->lchild=NULL;
case(1): preoder(bt); goto k1;
case(2): InOrder(bt); goto k1;
case(3): postOrder(bt); goto k1;
case(0): break;
root=(struct lbtree *)malloc(sizeof(struct lbtree));
root->data=ch;
root->lchild=createbtree();
root->rchild=createbtree();
return(root);
}
dl=treedepth(root->lchild);
dr=treedepth(root->rchild);
if(dl>dr)depth=dl+1;
else depth=dr+1;
\u6269\u5c55\u8d44\u6599\uff1a
(1)\u5b8c\u5168\u4e8c\u53c9\u6811\u2014\u2014\u82e5\u8bbe\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\u4e3ah\uff0c\u9664\u7b2c h \u5c42\u5916\uff0c\u5176\u5b83\u5404\u5c42 (1\uff5eh-1) \u7684\u7ed3\u70b9\u6570\u90fd\u8fbe\u5230\u6700\u5927\u4e2a\u6570\uff0c\u7b2ch\u5c42\u6709\u53f6\u5b50\u7ed3\u70b9\uff0c\u5e76\u4e14\u53f6\u5b50\u7ed3\u70b9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u6392\u5e03\uff0c\u8fd9\u5c31\u662f\u5b8c\u5168\u4e8c\u53c9\u6811\u3002
(2)\u6ee1\u4e8c\u53c9\u6811\u2014\u2014\u9664\u4e86\u53f6\u7ed3\u70b9\u5916\u6bcf\u4e00\u4e2a\u7ed3\u70b9\u90fd\u6709\u5de6\u53f3\u5b50\u53f6\u4e14\u53f6\u5b50\u7ed3\u70b9\u90fd\u5904\u5728\u6700\u5e95\u5c42\u7684\u4e8c\u53c9\u6811\u3002
(3)\u5e73\u8861\u4e8c\u53c9\u6811\u2014\u2014\u5e73\u8861\u4e8c\u53c9\u6811\u53c8\u88ab\u79f0\u4e3aAVL\u6811\uff08\u533a\u522b\u4e8eAVL\u7b97\u6cd5\uff09\uff0c\u5b83\u662f\u4e00\u68f5\u4e8c\u53c9\u6392\u5e8f\u6811\uff0c\u4e14\u5177\u6709\u4ee5\u4e0b\u6027\u8d28\uff1a\u5b83\u662f\u4e00\u68f5\u7a7a\u6811\u6216\u5b83\u7684\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u7684\u9ad8\u5ea6\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71\uff0c\u5e76\u4e14\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u90fd\u662f\u4e00\u68f5\u5e73\u8861\u4e8c\u53c9\u6811\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u4e8c\u53c9\u6811
#include
using namespace std;
typedef struct TNode//\u4e8c\u53c9\u6811\u7ed3\u6784
{
char nodeValue;//\u7ed3\u70b9\u7684\u503c
TNode* left;//\u5de6\u5b50\u6811
TNode* right;//\u53f3\u5b50\u6811
}*BiTree;
void CreateBiTree(BiTree &T)//\u4e2d\u5e8f\u904d\u5386\u65b9\u5f0f\u521b\u5efa\u4e8c\u53c9\u6811 ,\u8f93\u5165#\u4ee3\u8868\u8be5\u7ed3\u70b9\u4e3a\u7a7a
{
char nodeValue;
cin>> nodeValue;
if(nodeValue!='#')//\u7ed3\u70b9\u975e\u7a7a
{
T=new TNode;
T->nodeValue=nodeValue;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
else T=NULL;
}
int CountLeaf(BiTree T)
{
static int LeafNum=0;//\u53f6\u5b50\u521d\u59cb\u6570\u76ee\u4e3a0\uff0c\u4f7f\u7528\u9759\u6001\u53d8\u91cf
if(T)//\u6811\u975e\u7a7a
{
if(T->left==NULL&&T->right==NULL)//\u4e3a\u53f6\u5b50\u7ed3\u70b9
LeafNum++;//\u53f6\u5b50\u6570\u76ee\u52a01
else//\u4e0d\u4e3a\u53f6\u5b50\u7ed3\u70b9
{
CountLeaf(T->left);//\u9012\u5f52\u7edf\u8ba1\u5de6\u5b50\u6811\u53f6\u5b50\u6570\u76ee
CountLeaf(T->right);//\u9012\u5f52\u7edf\u8ba1\u53f3\u5b50\u6811\u53f6\u5b50\u6570\u76ee
}
}
return LeafNum;
}
//\u7528\u6765\u6d4b\u8bd5\u7684main\u51fd\u6570\uff0c
int main()
{
BiTree T;
int leafNum;
cout<<"\u8bf7\u8f93\u5165\u4e2d\u5e8f\u904d\u5386\u7684\u4e8c\u53c9\u6811\u5e8f\u5217(#\u53f7\u4ee3\u8868\u8be5\u7ed3\u70b9\u4e3a\u7a7a)\uff1a\u5982(ABC##DE#G##F###)"<<endl;
CreateBiTree(T);
leafNum=CountLeaf(T);
cout<<"\u8be5\u4e8c\u53c9\u6811\u4e2d\u53f6\u5b50\u7ed3\u70b9\u6570\u4e3a\uff1a"<<leafNum<<endl;
return 0;
}
#include<stdio.h>
#include<malloc.h>
#define MAX_NODE 50
typedef struct BiTNode
{
char data;
BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree CreateBiTree();
void InorderTraverse( BiTree T);
creatTree.cpp
#include"tree.h"
BiTree CreateBiTree()
{
BiTree T;
char ch;
scanf("%c",&ch);
if (ch=='#')
{
T=NULL;
return T;
}
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
if (T==NULL)
{
printf("树创建失败!");
T=NULL;
return T;
}
T->data=ch;
T->lchild=CreateBiTree();
T->rchild=CreateBiTree();
return T;
}
}
InorderTraverse.cpp
#include"tree.h"
void InorderTraverse( BiTree T)
{
BiTree stack[MAX_NODE] ,p=T ;
int top=0 , bool1=1 ;
if (T==NULL)
printf("Binary Tree is Empty!\n");
else
{
do
{
while (p!=NULL)
{
stack[++top]=p ;
p=p->lchild ;
}
if (top==0)
{
bool1=0 ;
}else{
p=stack[top] ;
top-- ;
printf("%c",p->data);
p=p->rchild ;
}
} while (bool1!=0) ;
}
}
main_fun.cpp
#include"tree.h"
int main()
{
BiTree T= CreateBiTree();
InorderTraverse(T);
return 0;
}
traverse.cpp
#include"tree.h"
void PreorderTraverse(BiTree T)
{
if (T!=NULL)
{
/* 访问根结点 */
PreorderTraverse(T->lchild) ;
printf("%c",T->data);
PreorderTraverse(T->rchild) ;
}
}
例如 输入 AB###
输出BA
先序输入 中序输出
可以修改遍历方式 来改变输出结果。
你这个问题不对吧?任意输入二叉树的结点个数和结点值,可能能构造很多种二叉树
绛旓細1.缂栧啓绋嬪簭浠绘剰杈撳叆浜屽弶鏍戠殑缁撶偣涓暟鍜岀粨鐐瑰,鏋勯犱竴妫典簩鍙夋爲,閲囩敤涓夌閫掑綊閬嶅巻绠楁硶(鍓嶅簭銆佷腑搴忋佸悗搴) 40 瀵硅繖妫典簩鍙夋爲杩涜閬嶅巻骞惰绠楀嚭浜屽弶鏍戠殑楂樺害銆傛!!鍙戦偖绠[email protected] 娉℃场XX鐞崇惓 | 娴忚1424 娆 |涓炬姤 鎴戞湁鏇村ソ鐨勭瓟妗堟帹鑽愪簬2017-12-16 10:19:15 鏈浣崇瓟妗 tree.h#include<stdio.h>#incl...
绛旓細1. 鎸夊厛搴忓簭鍒楁瀯閫犱竴妫典簩鍙夐摼琛ㄨ〃绀虹殑浜屽弶鏍慣锛2. 瀵硅繖妫典簩鍙夋爲杩涜閬嶅巻锛氬厛搴忋佷腑搴忋佸悗搴忎互鍙婂眰娆¢亶鍘嗗簭鍒楋紝鍒嗗埆杈撳嚭缁撶偣鐨勯亶鍘嗗簭鍒楋紱3. 姹浜屽弶鏍戠殑娣卞害/缁撶偣鏁扮洰/鍙剁粨鐐规暟鐩紱4. 灏嗕簩鍙夋爲姣忎釜缁撶偣鐨勫乏鍙冲瓙鏍戜氦鎹綅缃傘愭暟鎹弿杩般//- - - - - - 浜屽弶鏍戠殑浜屽弶閾捐〃瀛樺偍琛ㄧず - - - ...
绛旓細void search(TreeNode t)//閫掑綊鍚庡簭閬嶅巻浜屽弶鏍 { search(t.leftchild);//閬嶅巻鑺傜偣宸﹀瓙鏍 search(t.rightchild);//閬嶅巻鑺傜偣鍙冲瓙鏍 printf(t.data);//鎵撳嵃褰撳墠鑺傜偣 } 宸笉澶氬氨杩欎釜鎰忔濆惂
绛旓細BiTNode *bulid() /*寤烘爲*/ { BiTNode *q;BiTNode *s[20];int i,j;char x;printf("璇锋寜椤哄簭杈撳叆浜屽弶鏍戠殑缁撶偣浠ヨ緭鍏0鍜*鍙风粨鏉焅n");printf("璇疯緭鍏ヨ杈撳叆鐨勪负绗嚑涓粨鐐筰=\n");scanf("%d",&i);printf("璇疯緭鍏ヤ綘瑕佽緭鍏ヨ缁撶偣鐨勬暟涓簒=");getchar();scanf("%c",&x);while(i!=...
绛旓細printf("绛夊緟杈撳叆绗%d涓厓绱,濡傛灉杈撳叆缁撴潫璇疯緭鍏0鍥炶溅\n",++cnt);scanf("%d",&data);if(data==0) end = true;else { insert(*root,data);} } printf("鍏堝簭閬嶅巻\n");preorder(*rootbak);printf("\n");printf("鍚庡簭閬嶅巻\n");lastorder(*rootbak);printf("\n");system("pause");...
绛旓細include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef int ElemType; //鏁版嵁绫诲瀷//瀹氫箟浜屽弶鏍缁撴瀯锛屼笌鍗曢摼琛ㄧ浉浼硷紝澶氫簡涓涓彸瀛╁瓙缁撶偣typedef struct BiTNode{ElemType data; //鏁版嵁鍩焥truct BiTNode*lChild, *rChlid; //宸﹀彸瀛愭爲鍩焳BiTNode, *BiTree;//鍏堝簭鍒涘缓浜屽弶鏍慽nt CreateBi...
绛旓細travel(p->l,layer+1);printf("node :%c(c\t%d)(l\t%d)\n",p->item,p->counter,layer);travel(p->r,layer+1);} int count(link t){ if(!t)return 0;return 1 + count(t->l) +count(t->r);} int depth(link t){ int dl,dr;if(!t)return 0;dl = depth(t->l);...
绛旓細//1. 鍒涘缓涓涓妭鐐筺ode //2. 灏嗚鎻掑叆鐨勬暟鎹繚瀛樺埌node //3. 鎵ц鎻掑叆鎿嶄綔 //4. 杩斿洖0 int insert_linklist(linklist_t *ll, data_t *data){ linknode_t *node = (linknode_t *)malloc(sizeof(linknode_t));node->data = *data;node->next = ll->next;ll->next = node;retu...
绛旓細8 1 6 / int main(){ int x=1,m=0;do { cin>>a[n++];//鍌ㄥ瓨缁撶偣鍊 } while(getchar()!='\n');//鍥炶溅缁撴潫杈撳叆 build_tree(1,x);//鏋勫缓浜屽弶鏍(缁撴瀯浣撴暟缁勬ā鎷)cin>>m;//鏌ヨ娆℃暟 for(int i=0;i<m;i++){ int num,y;cin>>num;//鏌ヨ鍊 y=mp[num];//mp[num]鏄...
绛旓細include include typedef struct node { char data;struct node lchild;struct node rchild;}tnode;tnode createtree(){ tnode t;char ch;ch=getchar();if(ch=='0')t=null;else { t=(tnode )malloc(sizeof(tnode));t->data=ch;t->lchild=createtree();t->rchild=createtree();} re...