① 二叉樹能幹嗎

二叉樹應用非常廣泛。首先二叉樹是樹的基礎,利用二叉樹可以構造樹和森林。在操作系統源程序中,樹和森林被用來構造文件系統。我們看到的window和linux等文件管理系統都是樹型結構。在編譯系統中,如C編譯器源代碼中,二叉樹的中序遍歷形式被用來存放C 語言中的表達式。在游戲設計領域,許多棋類游戲的步驟都是按樹型結構編寫。其次二叉樹本身的應用也非常多,如哈夫曼二叉樹用於JPEG編解碼系統(壓縮與解壓縮過程)的源代碼中,甚至於編寫處理器的指令也可以用二叉樹構成變長指令系統,另外二叉排序樹被用於數據的排序。總之,二叉樹應用廣泛,應好好掌握。

html5中怎麼在canvas中畫一個二叉樹

用html5開發隨機生成的大樹,你應該沒想到40+行代碼就可以搞定了吧~接下來就跟大家說說這棵大樹是如何在html5開發中實現的。
同樣必須要有html容器。新建Index.html,代碼如下:
<、html>
1 <、head>
2 <、meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
3 <、title>canvas tree
4 <、/head>
5 <、body>
6 <、script type="text/javascript" src="tree.js">
7 <、/body>
8 <、/html>
接下來咱們開始tree.js:
、var canvas = document.createElement("canvas");
9 var ctx = canvas.getContext("2d");
10 canvas.width = 640;
11 canvas.height = 480;
12 document.body.appendChild(canvas);
代碼很好理解,創建一個canvas畫布,然後選擇為2d畫布,設置長寬,最後將這個畫布添加到body標簽下。
這個腳本最重要的函數在下面,大樹就是遞歸調用這個函數實現的,調用一次畫一條線段:
var drawTree = function (ctx, startX, startY, length, angle, depth, branchWidth){
13 var rand = Math.random,
14 newLength, newAngle, newDepth, maxBranch = 3,
15 endX, endY, maxAngle = 2 * Math.PI / 4,
16 subBraches;
17 ctx.beginPath();
18 ctx.moveTo(startX, startY);
19 endX = startX + length * Math.cos(angle);
20 endY = startY + length * Math.sin(angle);
21 ctx.lineCap = 'round';
22 ctx.lineWidth = branchWidth;
23 ctx.lineTo(endX, endY);
24 if (depth <= 2){
25 ctx.strokeStyle = 'rgb(0,' + (((rand() * 64) + 128) >> 0) + ',0)';
26 } else {
27 ctx.strokeStyle = 'rgb(' + (((rand() * 64) + 64) >> 0) + ',50,25)';
28 }
29 ctx.stroke();
30 newDepth = depth - 1;
31 if (!newDepth)
32 return;
33 subBranches = (rand() * (maxBranch - 1)) + 1;
34 branchWidth *= 0.7;
35 for (var i = 0; i < subBranches; i++){
36 newAngle = angle + rand() * maxAngle - maxAngle * 0.5;
37 newLength = length * (0.7 + rand() * 0.3);
38 drawTree(ctx, endX, endY, newLength, newAngle, newDepth, branchWidth);
39 }
40 }
接下來一點點解釋:
首先,解釋下各個變數的含義。ctx就是前面我們的2d畫布;startX是線段開始的橫坐標,同理startY是縱坐標;length是線段長度;angle是角度;depth是深度,葉子深度為1,樹干為12(可自己設定);branchWidth就線段的粗細。有了這些信息,其實就描述了一個線段,通過這些信息我們才能畫一個線段。
接下來又很可恥地一大段定義:
var rand = Math.random,
41 newLength, newAngle, newDepth, maxBranch = 3,
42 endX, endY, maxAngle = 2 * Math.PI / 4,
43 subBraches;
rand其實就是隨機一個0~1之間的實數,顧名思義,接下來這些new的就是下一節線段的各種參數。maxBranch就是最多有3個分叉,最大的角度 PI/2 即為,下一級調整角度在90%范圍內。subBranches就是分叉的個數。
好了,重要可以畫了:
ctx.beginPath();
44 ctx.moveTo(startX, startY);
45 endX = startX + length * Math.cos(angle);
46 endY = startY + length * Math.sin(angle);
47 ctx.lineCap = 'round';
48 ctx.lineWidth = branchWidth;
49 ctx.lineTo(endX, endY);
beginPath()表示告訴瀏覽器「我要開始畫了!」,把之前的記錄放棄了,這點有點像ps。moveTo()把游標移動到(startX, startY),再計算終點坐標,endX,endY,有點像高中學的參數方程。然後告訴瀏覽器,lineCap要round,線段的兩頭要是圓形的。有多粗呢?等於branchWidth。線段一直畫到(endX, endY)。
if (depth <= 2){
50 ctx.strokeStyle = 'rgb(0,' + (((rand() * 64) + 128) >> 0) + ',0)';
51 } else {
52 ctx.strokeStyle = 'rgb(' + (((rand() * 64) + 64) >> 0) + ',50,25)';
53 }
如果是已經畫到了最後兩級,即為葉子,那麼就rgb就為(0, 128~192, 0)(rgb代表顏色,分別為紅綠藍,red green blue)。還沒的話,就在(64~128, 50 ,25)中取。大家可能發現了,rgb必須為整數,但是rand()只能rand實數。大家其實也注意到了有個」 >> 0″,js當中表示位運算,整體向右移動n位,0就是移動0位。其實它的作用和Math.floor()一樣,但是速度更快。
動手畫!
ctx.stroke();
這個線段就畫好了,是時候准備下它的分叉的時候了。
newDepth = depth - 1;
54 if (!newDepth)
55 return;
如果這個線段是最後一級,就沒有分叉了,也是一個遞歸的終止條件。
subBranches = (rand() * (maxBranch - 1)) + 1;
56 branchWidth *= 0.7;
57 for (var i = 0; i < subBranches; i++){
58 newAngle = angle + rand() * maxAngle - maxAngle * 0.5;
59 newLength = length * (0.7 + rand() * 0.3);
60 drawTree(ctx, endX, endY, newLength, newAngle, newDepth, branchWidth);
61 }
分叉數是1~3中的一個數。然後有多少個分叉,就畫幾條線段,newAngle為原角度調整90度之內,新長度為原長度的0.7~1.0之間。
最後畫出主幹,這棵樹就可以開始畫了。
drawTree(ctx, 320, 470, 60, -Math.PI / 2, 12, 12);
大家可能注意到角度為負,不符合傳統觀念。但你要知道,畫布的縱坐標和傳統的坐標軸正好是相反的。
好了,html5開發隨機生成的大樹代碼就這樣完成了,怎麼樣,一點都難吧!

③ 二叉樹遍歷的動態顯示用哪種語言做比較好,flash編程還是HTML5

都不行。

二叉樹涉及到指針。
目前有指針的語言只有C語言、C++語言、Pascal 語言、C# 語言。
其中在C#中使版用指針必須框在unsafe塊中權,所以功能不強。
Pascal 現在與不常用了。建議C語言和C++語言。

另外,Flash可以編程,而HTML語言不能編程。
在HTML中,頂多插入一些JavaScript或VBScript,進行少量編程。

④ 二叉樹的作用

樹,連通但沒有迴路的圖
二叉樹是一類非常重要的樹形結構,它可以遞歸地定義如下:

二叉樹T是有限個結點的集合,它或者是空集,或者由一個根結點u以及分別稱為左子樹和右子樹的兩棵互不相交的二叉樹u(1)和u(2)組成。若用n,n1和n2分別表示T,u(1)和u(2)的結點數,則有n=1+n1+n2 。u(1)和u(2)有時分別稱為T的第一和第二子樹。

⑤ 二叉樹是度為2的有序樹()

二叉樹是度為2的有序樹,這個說法錯誤。二叉樹的度不大於2。

有序樹的結點次序是相對於另一結點而言的,若有序樹的子樹中只有一個孩子時,這個孩子的結點無須區分左右次序;二叉樹無論孩子樹是否為2,均需確定左右次序。

樹結構通常結合了另外兩種數據結構的優點:一種是有序數組,另外一種是鏈表。 樹結構的查詢的速度和有序數組一樣快,樹結構的插入數據和刪除數據的速度也和鏈表一樣快。

(5)html二叉樹擴展閱讀

在任意一顆非空樹中:

1)有且僅有一個特定的稱為根(Root)的結點;

2)當n>1時,其餘結點可分為m(m>0)個互不相交的有限集T1、T2、......、Tn,其中每一個集合本身又是一棵樹,並且稱為根的子樹。

此外,樹的定義還需要強調以下兩點:

1)n>0時根結點是唯一的,不可能存在多個根結點,數據結構中的樹只能有一個根結點。

2)m>0時,子樹的個數沒有限制,但它們一定是互不相交的。

⑥ 求用js做成的二叉樹代碼,高分重謝!

網路搜ztree就是你想要的

用ztree就更簡單了

URL:http://www.ztree.me

查看源代碼發現其實自己實現其實也很簡單的

有圖為證:

⑦ 到底什麼是前端二叉樹的遍歷

二叉樹遍歷代碼
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include<stack>
using namespace std;
#define NULL 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}*bitree;
int k=0;
int depth(bitree T)//樹的高度
{
if(!T)return 0;
else
{
int m=depth(T->lchild); int n=depth(T->rchild); return (m>n?m:n)+1;
}
}
//先序,中序 建樹
struct node *create(char *pre,char *ord,int n) {
struct node * T;
int m;
T=NULL;
if(n<=0)
{
return NULL;
}
else
{

⑧ 二叉樹的實現演示

範例二叉樹:
A
B C
D E
此樹的順序結構為:ABCD##E intmain(){node*p=newnode;node*p=head;head=p;stringstr;cin>>str;creat(p,str,0)//默認根節點在str下標0的位置return0;}//p為樹的根節點(已開辟動態內存),str為二叉樹的順序存儲數組ABCD##E或其他順序存儲數組,r當前結點所在順序存儲數組位置voidcreat(node*p,stringstr,intr){p->data=str[r];if(str[r*2+1]=='#'||r*2+1>str.size()-1)p->lch=NULL;else{p->lch=newnode;creat(p->lch,str,r*2+1);}if(str[r*2+2]=='#'||r*2+2>str.size()-1)p->rch=NULL;else{p->rch=newnode;creat(p->rch,str,r*2+2);}}

⑨ 二叉樹的基本操作 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);
}