當前位置:首頁 » 編程軟體 » 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);
}

熱點內容
python正則compile 發布:2024-11-27 05:19:05 瀏覽:28
資料庫系統概論第四版ppt 發布:2024-11-27 04:59:52 瀏覽:412
開通了免密為什麼還要密碼 發布:2024-11-27 04:17:42 瀏覽:15
excel導入資料庫java 發布:2024-11-27 04:16:21 瀏覽:624
ps不能存儲bpm 發布:2024-11-27 04:04:12 瀏覽:613
jquery代碼加密 發布:2024-11-27 03:54:51 瀏覽:594
數字密碼人格是什麼原理 發布:2024-11-27 03:46:34 瀏覽:428
華為怎麼看手機的配置 發布:2024-11-27 03:27:42 瀏覽:381
php函數作用域 發布:2024-11-27 03:26:11 瀏覽:177
pythonasteval 發布:2024-11-27 03:21:14 瀏覽:563