① 計算機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的判斷