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

熱點內容
文件夾怎麼打開 發布:2025-01-18 18:47:07 瀏覽:296
杉德卡卡號和密碼看哪裡 發布:2025-01-18 18:43:27 瀏覽:712
android返回退出 發布:2025-01-18 18:43:26 瀏覽:601
linux採集視頻 發布:2025-01-18 18:38:38 瀏覽:638
差異度演算法 發布:2025-01-18 18:34:27 瀏覽:698
電腦全套配置有哪些 發布:2025-01-18 18:32:39 瀏覽:145
新項目源碼 發布:2025-01-18 18:14:48 瀏覽:517
腳本設計圖 發布:2025-01-18 18:06:17 瀏覽:601
內部存儲空間不足總是跳出來 發布:2025-01-18 17:56:22 瀏覽:951
安卓光遇更新後魔法商店去哪裡了 發布:2025-01-18 17:55:47 瀏覽:133