二叉树C语言算法,急!!!!

作者&投稿:照君 (若有异议请与网页底部的电邮联系)
如何才能C语言编程实现求一棵二叉树的结点总数?急!!!~

(1)求结点数的递归定义为:
若为空树,结点数为0
若只有根结点,则结点数为1;
否则,结点数为根结点的左子树结点数+右子树结点数+1

(2)求叶子数的递归定义为:
若为空树,叶子数为0
若只有根结点,则叶子数为1;
否则,叶子数为根结点的左子树叶子数+右子树叶子数

typedef char DataType;//定义DataType类型
typedef struct node{
DataType data;
struct node *lchild, *rchild;//左右孩子子树
}BinTNode; //结点类型
typedef BinTNode *BinTree;//二叉树类型

int Node(BinTree T)
{//算结点数
if(T)
if (T-> lchild==NULL )&&( T --> rchild==NULL )
return 1;
else return Node(T-> lchild ) +Node ( T --> rchild )+1;
else return 0;
}

int Leaf(BinTree T)
{ //算叶子数
if(T)
if (T-> lchild==NULL )&&( T --> rchild==NULL )
return 1;
else return Leaf(T-> lchild ) +Node ( T --> rchild );
else return 0;
}

这个问题是需要输入序列的,不同的输入序列输出是不同的。下面仅给出一种可能的情况。

清华大学 严蔚敏 的<数据结构里>都有完整的代码,解释的也很清楚

#include<iostream>
#include<stdlib.h>
#include<stdio.h>

typedef struct tree
{
struct tree *left;
int date;
struct tree *right;
}treenode,*b_tree;
///////插入节点/////////////////////

b_tree insert(b_tree root,int node)
{
b_tree newnode;
b_tree currentnode;
b_tree parentnode;

newnode=(b_tree)malloc(sizeof(treenode));

newnode->date=node;
newnode->right=NULL;
newnode->left=NULL;

if(root==NULL)
return newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->date>node)
currentnode=currentnode->left;
else
currentnode=currentnode->right;
}
if(parentnode->date>node)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
return root;
}

//////建立树///////////////////
b_tree creat(int *date,int len)
{
b_tree root=NULL;
int i;
for(i=0;i<len;i++)
root=insert(root,date[i]);
return root;
}

//////中序打印////////////////
void print1(b_tree root)
{if(root!=NULL)
{
print1(root->left);
printf("%d->",root->date);
print1(root->right);
}
}

//////后序打印////////////////
void print2(b_tree root)
{if(root!=NULL)
{
print2(root->left);
print2(root->right);
printf("%d->",root->date);
}
}

//////前序打印////////////////
void print3(b_tree root)
{if(root!=NULL)
{ printf("%d->",root->date);
print3(root->left);
print3(root->right);

}
}
//////////在二叉树中查找给定关键字 ////////////
b_tree lookfor(b_tree root,int e)
{
b_tree p1,p2;
if(root!=NULL)
{
if(root->date==e)
return root;
else
p1=lookfor(root->left,e);
p2=lookfor(root->right,e);
if(p1!=NULL)
return p1;
else
if(p2!=NULL)
return p2;
else
return NULL;
}
else return NULL;
}

///////测试函数//////////////////
void main()
{
b_tree root=NULL;
int i,index;
int value;
int nodelist[20];

cout<<"输入树的节点,输入0结束\n";
index=0;
cin>>value;
while(value!=0)
{
nodelist[index]=value;
index=index+1;
cin>>value;
}
root=creat(nodelist,index);
printf("\n中序打印\n");
print1(root);
printf("\n后序打印\n");
print2(root);
printf("\n前序打印\n");
print3(root);

printf("\n查找的词:\n");
int a;
scanf("%d",&a);
b_tree p3=lookfor(root,a);
if(p3!=NULL)
printf("%d\n",p3->date);
else
printf("没你要找的词"); }

呵呵,楼主都“倾家荡产”了
估计这个程序都简单得没人愿意写了,而且网上到处都是,自己用百度google一下吧,很多的


使用c语言写一个二叉树,具体要求如下
语句较多,但比较简单,所以不一一介绍了,难理解的i主要编程思想,你可以输入abc**e*hj***cf**g** 然后回车 试试 看看结果。

c语言实现二叉树的先序,中序,后序的递归和非递归算法和层次遍历算法
{ \/\/ 操作结果:构造空二叉树T T=NULL;} void CreateBiTree(BiTree &T){ \/\/ 算法6.4:按先序次序输入二叉树中结点的值(可为字符型或整型,在主程中定义),\/\/ 构造二叉链表表示的二叉树T。变量Nil表示空(子)树。修改 int number;scanf("%d",&number); \/\/ 输入结点的值 if(number==...

二叉树先序非递归遍历C语言算法
\/*---递归---先序建立二叉树---*\/ void CreateBiTree(bitree **T) { \/\/按先序次序输入二叉树中的结点的值(一个字符),空格字符表示空树,\/\/构造二叉链表表示二叉树 char ch;scanf("%c",&ch);if(ch=='#') *T=NULL;else{ ...

完全二叉数操作(用顺序方式存储),c语言编程,一定要顺序存储!急求高人...
\/ 实验任务:(1) 创建二叉树,实现二叉树前序、中序、后序遍历算法。(2)查找指定结点。(3)设计算法统计二叉树中结点的个数、度为1的结点个数。(4)设计算法求出二叉树的高度。\/ include<stdio.h> include<stdlib.h> include<conio.h> define MAXSIZE 100 typedef char datatype;typedef...

二叉树的基本操作 C语言版的
BiTNode *creatBT(BiTNode *t,int b)\/\/2、创建二叉树 { BiTNode *p;char ch;cin>>ch;if(ch=='#')return 0;else { p=new BiTNode;p->data=ch;p->parent=t;p->bit=b;t=p;t->lchild=creatBT(t,0);t->rchild=creatBT(t,1);} return t;} void preorder(BiTNode *t)\/\/3、...

数据结构C语言 关于二叉树的基本问题
} \/\/ 如果右子树不为空,则递归右子树 if (lpNode->right) { Swap(lpNode->right); }}\/\/ 非递归的算法如下:list<Bitree *> lstNode;if (lpRoot != NULL){ lstNode.push_back(lpRoot); \/\/ 首先将二叉树的根节点放到队列中}while (!lstNode.empty()){ \/...

C语言二叉树
我试着来解答一下。这是一个递归函数。首先要理解T、L、R的含义。假如L[i]=x1,R[i]=x2,那么节点i的左右孩子分别就是x1,x2.那么T[x1]=i,T[x2]=i,就是指x1,x2的双亲节点就是i。Status Dencend(Array1D L, Array1D R, int n, int u, int v, Array1D T)\/***\/ { int i...

C语言问题
第一层1个 第二层2个 第三层4个 第四层8个 第五层16个 ...第n层 有2倍(n-1)层的个数 但是不知道这棵树是不是满二叉树,所以要计算第几层,有个公式(看图)将700带入,得到n层,然后计算n-1层有多少个节点,700减去得到的节点数,就是叶子节点了。。。

C语言二叉树的创建和遍历
printf("%c",T->data);if((T->lchild)||(T->rchild)){ if(T->lchild){ printf("%c",'(');DisTree(T->lchild);} if(T->rchild){ printf("%c",',');DisTree(T->rchild);printf("%c",')');} } } } \/\/===基于先序遍历算法创建二叉树=== \/\/===要求输入先序序列,...

c语言数据结构与算法。下边的二叉树题中“度为1,2,3,4的结点个数”度...
度为i的每个结点关联i个分支,所以ni个度为i个结点关联i*ni个分支,i=0,1,2,3,4)n=0*n0+1*n1+2*n2+3*n3+4*n4+1=n0+n1+n2+n3+n4 n0=n2+2*n3+3*n4+1=2+2*1+3*1+1=8。答案A)其中,ni(i=0,1,2,3,4)表示度为i的结点数,叶子结点数为n0,B为树的分支总数。

巴彦淖尔市18281128130: 急急急:关于二叉树的算法 遍历 左右子树交换 用类C语言 要详细代码 -
逮泽独一: (1)编写建立二叉树的算法. (2)验证二叉树的先序、中序、后序遍历算法 (3)编写二叉树的左右子树交换算法 上面这些都比较简单,程序如下:#include <stdio.h> #include <malloc.h>typedef struct tree {char data;struct tree *l;/*左儿子*/...

巴彦淖尔市18281128130: 求数据结构(C语言版)建立二叉树的代码~~急~~谢谢了 -
逮泽独一: BT.H文件 #include <stdio.h> #include <malloc.h> #include <conio.h> #define TRUE 1 #define FALSE 0 #define ERROR 0 #define OK 1 #define Stack_Size 50 #define NUM 50 #define MAXSIZE 50 //队列的最大长度 //定义二叉树 typedef char ...

巴彦淖尔市18281128130: 急求二叉树C语言代码 -
逮泽独一: BinaryTree.h: /******************************************************************** created: 2006/07/04 filename: BinaryTree.h author: 李创 purpose: 演示二叉树的算法 *********************************************************************/ #ifndef BinaryTree_H ...

巴彦淖尔市18281128130: 用C语言实现二叉树的操作 -
逮泽独一: #include#define STACK_MAX_SIZE 30 #define QUEUE_MAX_SIZE 30 #ifndef elemType typedef char elemType; #endif /************************************************************************/ /* 以下是关于二叉树操作的11个简单算法...

巴彦淖尔市18281128130: 如何才能C语言编程实现求一棵二叉树的结点总数?急!!! -
逮泽独一: int countnode(bt *h) //其中bt是二叉树的结点(结构体) {if(!bt)return 0; int a,b; a=countnode(h^.lchild); b=countnode(h^.rchild); return a+b+1; }

巴彦淖尔市18281128130: 求二叉树遍历算法C语言实现的?
逮泽独一: 下面是c语言的前序遍历二叉树的算法,在这里假设的节点元素值假设的为字符型, 说明:算法中用到了结构体,也用到了递归的方法,你看看怎么样,祝你好运! #include"stdio.h" typedef char ElemType; typedef struct node //定义链表结构 ...

巴彦淖尔市18281128130: 实现二叉排序树建立和检索算法(C语言)【急求】 -
逮泽独一: #include <stdlib.h>#include <time.h>#include <stdio.h>#define INFMT "%d"#define OUTFMT "%d "/* #define NULL 0L */#define BOOL int#define TRUE 1#define FALSE 0#define LEN 10000 typedef int ElemType; typedef struct BSTNode { ...

巴彦淖尔市18281128130: 二叉树遍历(c语言实现) -
逮泽独一: #include <stdio.h>#include <malloc.h> typedef struct node{ int data; struct node *lchild,*rchild; }*treetp,tree; treetp create (treetp t,int c); void print1(treetp); void print2(treetp); void print3(treetp); int number=0; void main() { treetp t=0,r;r=create (t,0);...

巴彦淖尔市18281128130: 有关二叉树的简单C语言程序. -
逮泽独一: 先序输入 如 abc##d##e## (#表示空) 输出 cbdae#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2#include "cstdlib"#include "malloc.h" typedef int Status; typedef char TElemType;#include #include ...

巴彦淖尔市18281128130: c语言二叉树代码求解 -
逮泽独一: #include <stdio.h>#include <stdlib.h>#include <malloc.h>#define MAX 20#define OK 1#define ERROR 0#define NULL 0#define OVERFLOW 0 typedef char TElemType; typedef int Status; typedef struct BiTNode{ TElemType data; struct BiTNode *...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网