當前位置:首頁 » 編程軟體 » tree編程

tree編程

發布時間: 2024-05-22 19:07:18

① C++: 緙栧啓紼嬪簭錛屽壋寤轟竴涓浜屽弶鏍戙傚疄鐜扮粺璁′簩鍙夋爲鍙跺瓙緇撶偣鐨勪釜鏁板拰浜屽弶鏍戠殑娣卞害

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


typedefintElemType;//鏁版嵁綾誨瀷

//瀹氫箟浜屽弶鏍緇撴瀯錛屼笌鍗曢摼琛ㄧ浉浼礆紝澶氫簡涓涓鍙沖╁瓙緇撶偣
typedefstructBiTNode{
ElemTypedata;//鏁版嵁鍩
structBiTNode*lChild,*rChlid;//宸﹀彸瀛愭爲鍩
}BiTNode,*BiTree;


//鍏堝簭鍒涘緩浜屽弶鏍
intCreateBiTree(BiTree*T)
{
ElemTypech;
ElemTypetemp;


scanf("%d",&ch);
temp=getchar();


if(ch==-1)
*T=NULL;
else
{
*T=(BiTree)malloc(sizeof(BiTNode));
if(!(*T))
exit(-1);


(*T)->data=ch;
printf("杈撳叆%d鐨勫乏瀛愯妭鐐癸細",ch);
CreateBiTree(&(*T)->lChild);
printf("杈撳叆%d鐨勫彸瀛愯妭鐐癸細",ch);
CreateBiTree(&(*T)->rChlid);
}


return1;
}


//鍏堝簭閬嶅巻浜屽弶鏍
voidTraverseBiTree(BiTreeT)
{
if(T==NULL)
return;
printf("%d",T->data);
TraverseBiTree(T->lChild);
TraverseBiTree(T->rChlid);


}


//涓搴忛亶鍘嗕簩鍙夋爲
voidInOrderBiTree(BiTreeT)
{
if(T==NULL)
return;
InOrderBiTree(T->lChild);
printf("%d",T->data);
InOrderBiTree(T->rChlid);


}


//鍚庡簭閬嶅巻浜屽弶鏍
voidPostOrderBiTree(BiTreeT)
{
if(T==NULL)
return;
PostOrderBiTree(T->lChild);
PostOrderBiTree(T->rChlid);
printf("%d",T->data);


}
//浜屽弶鏍戠殑娣卞害


intTreeDeep(BiTreeT)
{
intdeep=0;
if(T)
{
intleftdeep=TreeDeep(T->lChild);
intrightdeep=TreeDeep(T->rChlid);
deep=leftdeep>=rightdeep?leftdeep+1:rightdeep+1;
}
returndeep;
}


//奼備簩鍙夋爲鍙跺瓙緇撶偣涓鏁


intLeafcount(BiTreeT,int&num)
{
if(T)
{
if(T->lChild==NULL&&T->rChlid==NULL)
num++;
Leafcount(T->lChild,num);
Leafcount(T->rChlid,num);


}
returnnum;
}
//涓誨嚱鏁
intmain(void)
{
BiTreeT;
BiTree*p=(BiTree*)malloc(sizeof(BiTree));
intdeepth,num=0;
printf("璇瘋緭鍏ョ涓涓緇撶偣鐨勫,-1琛ㄧず娌℃湁鍙剁粨鐐: ");
CreateBiTree(&T);
printf("鍏堝簭閬嶅巻浜屽弶鏍: ");
TraverseBiTree(T);
printf(" ");
printf("涓搴忛亶鍘嗕簩鍙夋爲: ");
InOrderBiTree(T);
printf(" ");
printf("鍚庡簭閬嶅巻浜屽弶鏍: ");
PostOrderBiTree(T);
printf(" ");
deepth=TreeDeep(T);
printf("鏍戠殑娣卞害涓:%d",deepth);
printf(" ");
Leafcount(T,num);
printf("鏍戠殑鍙跺瓙緇撶偣涓鏁頒負:%d",num);
printf(" ");
return0;
}

② 求java圖形界面樹類編程源碼舉例。類似windows資源管理器那樣的。如附圖,2層2項即可。

publicvoidinit(){
ContainercontentPane=null;
=newDefaultMutableTreeNode("我的電腦");
1=newDefaultMutableTreeNode("網路");
2=newDefaultMutableTreeNode("硬碟");
1_1=newDefaultMutableTreeNode("無限");
1_2=newDefaultMutableTreeNode("有限");
2_1=newDefaultMutableTreeNode("A");
2_2=newDefaultMutableTreeNode("B");
treeNode.add(treeNode1);
treeNode.add(treeNode2);
treeNode1.add(treeNode1_1);
treeNode1.add(treeNode1_2);
treeNode2.add(treeNode2_1);
treeNode2.add(treeNode2_2);
JTreetree=newJTree(treeNode);
contentPane=getContentPane();
JPaneljp=newJPanel();
jp.add(tree);
contentPane.add(jp);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}

熱點內容
存儲計算邏輯 發布:2024-11-08 13:49:35 瀏覽:543
java演算法排序演算法 發布:2024-11-08 13:42:20 瀏覽:883
u盤隨身系統linux 發布:2024-11-08 13:34:34 瀏覽:411
b1422壓縮機鎖定 發布:2024-11-08 13:32:43 瀏覽:635
上傳按鈕圖片 發布:2024-11-08 13:30:57 瀏覽:920
安卓手機相機如何拍攝短視頻 發布:2024-11-08 13:28:42 瀏覽:411
網站的並發訪問 發布:2024-11-08 13:27:56 瀏覽:514
脈沖壓縮調制 發布:2024-11-08 12:49:56 瀏覽:126
松茸菌存儲 發布:2024-11-08 12:49:05 瀏覽:333
超市wifi密碼大概都是什麼 發布:2024-11-08 12:48:19 瀏覽:590