c语言的二叉树
① 计算机c语言中 什么是二叉树
在计算机科学中,二叉树是每个结点最多有两个子树的有序树。通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用作二叉查找树和二叉堆或是二叉排序树。
二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。二叉树的第i层至多有2的 i -1次方个结点;深度为k的二叉树至多有2^(k) -1个结点;对任何一棵二叉树T,如果其终端结点数(即叶子结点数)为n0,度为2的结点数为n2,则n0 = n2 + 1。
树是由一个或多个结点组成的有限集合,其中:
⒈必有一个特定的称为根(ROOT)的结点;二叉树
⒉剩下的结点被分成n>=0个互不相交的集合T1、T2、......Tn,而且, 这些集合的每一个又都是树。树T1、T2、......Tn被称作根的子树(Subtree)。
树的递归定义如下:(1)至少有一个结点(称为根)(2)其它是互不相交的子树
1.树的度——也即是宽度,简单地说,就是结点的分支数。以组成该树各结点中最大的度作为该树的度,如上图的树,其度为2;树中度为零的结点称为叶结点或终端结点。树中度不为零的结点称为分枝结点或非终端结点。除根结点外的分枝结点统称为内部结点。
2.树的深度——组成该树各结点的最大层次。
3.森林——指若干棵互不相交的树的集合,如上图,去掉根结点A,其原来的二棵子树T1、T2、T3的集合{T1,T2,T3}就为森林;
4.有序树——指树中同层结点从左到右有次序排列,它们之间的次序不能互换,这样的树称为有序树,否则称为无序树。
② 关于c语言二叉树
完全二叉树除了第一抄层和最后袭一层外,其余各层的结点数都是2的幂,所以都是偶数,因此,当最后一层的结点数为偶数时,树的总结点数才可能是奇数.
而完全二叉树只有最后一层的结点数为奇数时,树中才可能存在唯一的度为1的结点,即最后一个结点的父结点.但现在最后一层结点数为偶数,所以树中不存在度为1的结点,即n1=0.
所以n=n0+n2=2n0-1=699,n0=350
③ C语言 二叉树的建立
从键盘输入字符,然后回车,字符会停留在缓冲区内,之后你每次scanf("%c", &ch)就会从缓冲区取出一个来
④ C语言 二叉树
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int e;
struct Node *l, *r;
} Node;
Node *init() //先序遍历构造二叉树
{
char n;
Node *p;
scanf("%c", &n);
if (n=='0')
return NULL;
p = (Node*)malloc(sizeof(Node));
if (!p)
exit(0);
p->e = n-'0';
p->l = init();
p->r = init();
return p;
}
void DLR(Node *head) //先序遍历二叉树(递归算法)
{
if (head)
{
printf("%d", head->e);
DLR(head->l);
DLR(head->r);
}
}
void destory(Node *head) //销毁二叉树
{
Node *l, *r;
if (!head)
return;
l = head->l;
r = head->r;
free(head);
destory(l);
destory(r);
}
int main()
{
Node *head = init();
DLR(head);
destory(head);
return 0;
}
⑤ 二叉树c语言实现
#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree &T)
{
char ch;
ch=getchar();
if (ch == ' ')
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;//生成根节点
CreatBiTree(T->lchild);//构造左子树
CreatBiTree(T->rchild);//构造右子树
}
}
void preorder(BiTree T)//前序遍历
{
if (T!=NULL){
printf ("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(BiTree T)//中序遍历
{
if (T!=NULL){
inorder(T->lchild);
printf ("%c",T->data);
inorder(T->rchild);
}
}
void postorder(BiTree T)//后序遍历
{
if (T!=NULL){
postorder(T->lchild);
postorder(T->rchild);
printf ("%c",T->data);
}
}
void main ()
{
cout<<"请输入要创建的二叉树包括空格:"<<endl ;
BiTree T;
CreatBiTree(T);//创建二叉树
cout<<"前序遍历的结果为:"<<endl;
preorder(T);
cout<<endl;
cout<<"中序遍历的结果为:"<<endl;
inorder(T);
cout<<endl;
cout<<"后序遍历的结果为:"<<endl;
postorder(T);
}
⑥ c语言二叉树
#include <stdlib.h>
#include<malloc.h>
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();
}
return t;
}
void listtree(tnode *t)
{
if (t!=NULL)
{
printf("%c",t->data);
if(t->lchild!=NULL||t->rchild!=NULL)
{
printf("(");
listtree(t->lchild);
if(t->rchild!=NULL)
printf(",");
listtree(t->rchild);
printf(")");
}
}
}
void inorder(tnode *t)
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%c\t",t->data);
inorder(t->rchild);
}
}
void leve(tnode *t)
{
tnode *quee[100];
int front,rear;
front=-1;
rear=0;
quee[rear]=t;
while(front!=rear)
{
front++;
printf("%c\t",quee[front]->data);
if(quee[front]->lchild!=NULL)
{
rear++;
quee[rear]=quee[front]->lchild;
}
if(quee[front]->rchild!=NULL)
{
rear++;
quee[rear]=quee[front]->rchild;
}
}
}
main()
{
tnode *t=NULL;
printf("请输入二叉树元素:");
t=createtree();
printf("广义表的输出:");
listtree(t);
printf("\n");
printf("二叉树的中序遍历:");
inorder(t);
printf("\n");
printf("二叉树的层次遍历:");
leve(t);
printf("\n");
system("pause");
}
/*
输入:AB00CD00E00F000
输出:A(B,C((D,E))
中序遍历: B A D C E
层次遍历:A B C D E
*/
另外,团IDC网上有许多产品团购,便宜有口碑
⑦ c语言绘制二叉树
你那里是打印出的啥?不会是没有存下数据打印了乱码吧?:)
[修改]
比如,我把和你的二叉树相关的代码去掉,改了一下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <graphics.h>
int main()
{
char str[10];
int x = 100, y = 100;
int e = 9;
/* select a driver and mode that supports */
/* multiple drawing colors. */
int gdriver = DETECT, gmode = VGA, errorcode;
detectgraph(&gdriver, &gmode);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(x,y,9,9);
setcolor(WHITE);
circle(x,y,10);
sprintf(str,"%d",e);
outtextxy(x-3,y-2,str);
/* clean up */
getch();
/* colse */
closegraph();
return 0;
}
就能在圈圈里打印出"9"
⑧ 二叉树的基本操作 C语言版的
#include <iostream.h>
typedef struct BiTNode
{
char data;
int bit;
struct BiTNode *lchild,*rchild,*parent;
}BiTNode;
void InitBT(BiTNode *&t)//1、初始化,不带头结点
{
t=NULL;
}
/*void InitBT(BiTNode *t)//初始化,带头结点
{
t=new BiTNode;
t->lchild=t->rchild=t->parent=NULL;
}*/
int EmptyBT(BiTNode *t)//判断队空
{
if(t==0)
return 1;
else
return 0;
}
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、先序遍历
{
if(!EmptyBT(t))
{
cout<<t->data;
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BiTNode *t)//中序遍历
{
if(!EmptyBT(t))
{
inorder(t->lchild);
cout<<t->data;
inorder(t->rchild);
}
}
void postorder(BiTNode *t)//后序遍历
{
if(!EmptyBT(t))
{
postorder(t->lchild);
postorder(t->rchild);
cout<<t->data;
}
}
void coutBT(BiTNode *t,int &m,int &n,int &i)//4、计算二叉树中叶子结点、度为2的结点和度为1的结点的个数
{
if(!EmptyBT(t))
{
if((t->lchild==0) && (t->rchild==0))
m++;//叶子结点
else if((t->lchild!=0) && (t->rchild!=0))
i++;//度为2的结点
else
n++;//度为1的结点
coutBT(t->lchild,m,n,i);
coutBT(t->rchild,m,n,i);
}
}
void coutNode(BiTNode *t,int &k)//5、求二叉树中结点个数
{
if(!EmptyBT(t))
{
k++;
coutNode(t->lchild,k);
coutNode(t->rchild,k);
}
}
int BTdepth(BiTNode *t)//6、求二叉树的深度
{
int i,j;
if(EmptyBT(t))
return 0;
else
{
i=BTdepth(t->lchild);
j=BTdepth(t->rchild);
return (i>j?i:j)+1;
}
}
int Xdepth(BiTNode *t,char x)//7、查找x的层数
{
int num1,num2,n;
if(t==NULL)
return 0;
else{
if(t->data==x)
return 1;
num1=Xdepth(t->lchild,x);
num2=Xdepth(t->rchild,x);
n=num1+num2;
if(num1!=0||num2!=0)
n++;
return n;
}
}
static int flag;
void SearchChild(BiTNode *t,int k)//8、查找第k个结点的左右孩子
{
if(!EmptyBT(t))
{
if(k==0)
{
cout<<"位置不能为0!"<<endl;
return;
}
else
{
flag++;
if(flag==k)
{
if(t->lchild==0)
cout<<"无左孩子! ";
else
cout<<"左孩子为:"<<(t->lchild->data)<<" ";
if(t->rchild==0)
cout<<"无右孩子!"<<endl;
else
cout<<"右孩子为:"<<(t->rchild->data)<<endl;
}
else
{
SearchChild(t->lchild,k);
SearchChild(t->rchild,k);
}
}
}
}
int Xancestor(BiTNode *t,char x)//9、查找x结点祖先
{
int n,num1,num2;
if(t==NULL)
return 0;
else
{
if(t->data==x)
return 1;
num1=Xancestor(t->lchild,x);
num2=Xancestor(t->rchild,x);
n=num1+num2;
if(n!=0)
{
n++;
cout<<t->data<<" "<<endl;
}
}
}
void BTNodePath(BiTNode *t)//10、输出所有叶子结点路径
{
if(!EmptyBT(t))
{
if((t->lchild==0) && (t->rchild==0))
{
cout<<t->data<<"的路径为:";
for(BiTNode *p=t;p!=0;p=p->parent)
cout<<p->data;
cout<<endl;
}
else
{
BTNodePath(t->lchild);
BTNodePath(t->rchild);
}
}
}
void BTNodebit(BiTNode *t)//11、输出所有叶子结点编码
{
if(!EmptyBT(t))
{
if((t->lchild==0) && (t->rchild==0))
{
cout<<t->data<<"的编码为:";
for(BiTNode *p=t;p->parent!=0;p=p->parent)
cout<<p->bit;
cout<<endl;
}
else
{
BTNodebit(t->lchild);
BTNodebit(t->rchild);
}
}
}
void main()
{
BiTNode *t;
int m,n,i,d,q,k;
char x;
cout<<"1、初始化..."<<endl;
InitBT(t);
cout<<"2、创建二叉树..."<<endl;
t=creatBT(t,0);
cout<<"3.1、先序遍历..."<<endl;
preorder(t);
cout<<endl;
cout<<"3.2、中序遍历..."<<endl;
inorder(t);
cout<<endl;
cout<<"3.3、后序遍历..."<<endl;
postorder(t);
cout<<endl;
m=n=i=0;
cout<<"4、计算叶子结点,度为1的结点和度为2的结点的个数..."<<endl;
coutBT(t,m,n,i);
cout<<"叶子结点个数为:"<<m<<endl;
cout<<"度为1的结点个数为:"<<n<<endl;
cout<<"度为2的结点个数为:"<<i<<endl;
q=0;
cout<<"5、计算结点个数..."<<endl;
coutNode(t,q);
cout<<"结点个数为:"<<q<<endl;
d=0;
cout<<"6、计算深度..."<<endl;
d=BTdepth(t);
cout<<"深度为:"<<d<<endl;
cout<<"7、求x的层数..."<<endl;
cout<<"输入x:";
cin>>x;
if(Xdepth(t,x)==0)
cout<<"x不存在!"<<endl;
else
cout<<Xdepth(t,x)<<endl;
cout<<"8、输入要查找孩子的结点在先序遍历中的位置k(不等于0):";
cin>>k;
SearchChild(t,k);
if(k>flag)
cout<<"位置超出长度!"<<endl;
cout<<"9、查询结点的所有祖先,请输入结点x:";
cin>>x;
int num;
num=Xancestor(t,x);
if(num==0)
cout<<"结点不存在!"<<endl;
if(num==1)
cout<<"根结点无祖先!"<<endl;
cout<<"10、输出所有叶子结点路径(叶→根):"<<endl;
BTNodePath(t);
cout<<"11、输出所有叶子结点编码(叶→根):"<<endl;
BTNodebit(t);
}
⑨ c语言 二叉树
void previsit(BiTree &T)
{
if(T!=NULL){
printf("%d\n",T->data);
previsit(T->lchild);
previsit(T->rchild);
}
}
前序遍历少了判断是否回为答NULL的判断