#inclu?de #inclu?de #inclu?de #defin?e TRUE 1 #defin?e FLASE? 0 #defin?e OK 1 #defin?e ERROR? 0 #defin?e INFEA?SIBLE? -1 #defin?e OVERF?LOW -2 typed?ef int Statu?s; typed?ef int TElem?Type; typed?ef struc?t BiTNo?de { TElem?Type data; struc?t BiTNo?de *lchil?d,*rchil?d; } BiTNo?de,*BiTre?e; Statu?s Creat?eBiTr?ee(BiTre?e &T) { TElem?Type e; scanf?("%d",&e); if(e==0) T=NULL; else { T=(BiTre?e)mallo?c(sizeo?f(BiTNo?de)); if(!T) exit(OVERF?LOW); T->data =e; Creat?eBiTr?ee(T->lchil?d ); Creat?eBiTr?ee(T->rchil?d ); } retur?n OK; } int BTNod?eCoun?t (BiTre?e T) { int m=0,n=0; if(T==NULL) retur?n 0; else { m=BTNod?eCoun?t(T->lchil?d ); n=BTNod?eCoun?t(T->rchil?d ); retur?n (m+n+1); } } void main() { BiTre?e T; print?f("请输入二叉?树中节点的?值(int型),0表示空树?:\n"); Creat?eBiTr?ee(T); print?f("该树的节点?数是%d\n",BTNod?eCoun?t(T)); } //求叶子数 #inclu?de #inclu?de #inclu?de #defin?e TRUE 1 #defin?e FLASE? 0 #defin?e OK 1 #defin?e ERROR? 0 #defin?e INFEA?SIBLE? -1 #defin?e OVERF?LOW -2 typed?ef int Statu?s; typed?ef int TElem?Type; typed?ef struc?t BiTNo?de { TElem?Type data; struc?t BiTNo?de *lchil?d,*rchil?d; } BiTNo?de,*BiTre?e; Statu?s Creat?eBiTr?ee(BiTre?e &T) { TElem?Type e; scanf?("%d",&e); if(e==0) T=NULL; else { T=(BiTre?e)mallo?c(sizeo?f(BiTNo?de)); if(!T) exit(OVERF?LOW); T->data =e; Creat?eBiTr?ee(T->lchil?d ); Creat?eBiTr?ee(T->rchil?d ); } retur?n OK; } int LeftB?iTree?(BiTre?e T) { int m,n; if(!T) retur?n 0; else if(!T->lchil?d &&!T->rchil?d ) retur?n 1; else { m=LeftB?iTree?(T->lchil?d ); n=LeftB?iTree?(T->rchil?d ); retur?n (m+n); } } void main() { BiTre?e T; print?f("请输入树中?节点的值(int型),0表示空树?:\n"); Creat?eBiTr?ee(T); print?f("该二叉树的?树叶是%d\n",LeftB?iTree?(T)); } 如果需要两?个程序合并?,自己修改一?下就可以了? #inclu?de #inclu?de typed?ef char ElemT?ype ; #inclu?de"BiTre?e.h" typed?ef BiTre?eNode?* DataT?ype; #inclu?de"LinQu?eue.h" void Lever?Order?(BiTre?eNode? * root) //层序遍历的?函数,形参是一个?二叉树的头?指针,如果该二叉?树有头结点?则为其头结?点的左子树? { int m=0,n=0; //m为叶子节?点的计数器?,n为总节点? BiTre?eNode? *q; LQueu?e head; //创建一个队?列,用以存放二?叉树的结点?地址 Queue?Initi?ate(&head); //初始化队列? Queue?Appen?d(&head, root); //二叉树第一?个有用结点?入队列 while?(Queue?NotEm?pty(head)) { Queue?Delet?e(&head,&q); print?f("%c ",q->data); n++; //输出一个结?点就进行一?次计数,总计为总结?点数目 if(q->leftC?hild!=NULL) Queue?Appen?d(&head, q->leftC?hild); if(q->right?Child?!=NULL) Queue?Appen?d(&head, q->right?Child?); if(q->leftC?hild==NULL&&q->right?Child?==NULL) m++; //叶子 节点为?没有子树的?结点,利用这个特?点进行筛选? } print?f("\n"); print?f("总结点数目?为:%d\n",n); print?f("叶子节点数?目为:%d\n",m); } void main() { BiTre?eNode? *root, *p, *pp; Initi?ate(&root); p = Inser?tLeft?Node(root, 'A'); p = Inser?tLeft?Node(p, 'B'); p = Inser?tLeft?Node(p, 'D'); p = Inser?tRigh?tNode?(p, 'G'); p = Inser?tRigh?tNode?(root->leftC?hild, 'C'); pp = p; Inser?tLeft?Node(p, 'E'); Inser?tRigh?tNode?(pp, 'F'); Lever?Order?(root->leftC?hild); Destr?oy(&root); } #include #include typedef struct node{ char data; struct node *lchild,*rchild; }Bitree; Bitree *Q[maxsize]; Bitree *creatree(){ Bitree *T,*S; char ch; int front,rear; T=NULL; front=1;rear=0; printf("请输入二叉树的元素:"); scanf("%c",&ch); while(ch!='#'){ S=NULL; if(ch!='@') { S=(Bitree *)malloc(sizeof(Bitree)); S->data=ch; S->lchild=S->rchild=NULL; } rear++; Q[rear]=S; if(rear==1) T=S; else { if(S!=NULL&&Q[front]!=NULL) if(rear%2==0) Q[front]->lchild=S; else Q[front]->rchild=S; if(rear%2==1) front++; } scanf("%c",&ch); } return T; } int num(Bitree *T){ int x=0; if(T) { if(T->lchild==NULL&&T->rchild==NULL&&T!=NULL) { x++; } x=x+num(T->lchild); x=x+num(T->rchild); } return x; } void Front(Bitree *T) { if(T) { printf("%c ",T->data); Front(T->lchild); Front(T->rchild); } } void main() { Bitree *T; int x; T=creatree(); printf("先序遍历输出为:"); Front(T); printf("\n"); x=num(T); printf("该树的叶子节点数为:"); printf("%d",x); printf("\n"); } 1.节节节节节节节节点可以通在xml 中指定icon 节节节属性。 例如: [Embed(source="/res/img/icons/dot1.png")] public static var feedIcon:Class TreeNode.xml 的内容: <?xml version='1.0' encoding='utf-8'?> 2.节节节节节除按的 首先节Tree指定Render节 public class TreeRender extends TreeItemRenderer { public function TreeRender() { super(); } protected var delShape:Image; override protected function createChildren():void { super.createChildren(); delShape= new Image(); delShape.source="close.png"; delShape.addEventListener(MouseEvent.CLICK, changeHandler); addChild(delShape); } protected function changeHandler( event:Event ):void { Alert.show("节节节确定要除?", "提示", 3, this, delHandler); } private function delHandler(event:CloseEvent):void { if (event.detail==Alert.YES){ //节除dataProvider节节节节节节节节节节节节节节节节节节节中点,算法根据自身情况去写,我儿 只是个例子,不一定适合你 var tree:Tree=this.owner as Tree; var list:XMLListCollection=tree.dataProvider as XMLListCollection; var dataSource:XML=list.source.parent(); for(var i:int=0;i<> var xml:XML=dataSource.elements("node")[i]; if(xml.attribute("label")==this.label.text){ delete dataSource.elements("node")[i]; } } } //节节节节节节将除点后的XMLListCollection重新写入到文件 var file:File=new File(File.applicationDirectory.nativePath+"/TreeNode.xml"); var fileIn:FileStream=new FileStream(); fileIn.open(file,FileMode.WRITE); fileIn.writeUTFBytes("<?xml version='1.0' encoding='utf-8'?>\n"+dataSource.toXMLString()); fileIn.close(); Alert.show("节除成功!", "提示"); } } override protected function commitProperties():void { super.commitProperties(); } override protected function measure():void { super.measure(); measuredWidth += delShape.getExplicitOrMeasuredWidth(); } override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { super.updateDisplayList(unscaledWidth, unscaledHeight); var startx:Number = data ? TreeListData( listData ).indent : 0; if (disclosureIcon) { disclosureIcon.x = startx; startx = disclosureIcon.x + disclosureIcon.width; disclosureIcon.setActualSize(disclosureIcon.width,disclosureIcon.heig ht); disclosureIcon.visible = data ? TreeListData( listData ).hasChildren : false; } if (icon) { icon.x = startx; startx = icon.x + icon.measuredWidth; icon.setActualSize(icon.measuredWidth, icon.measuredHeight); } delShape.move(unscaledWidth-delShape.width, ( unscaledHeight - delShape.height ) / 2 ); delShape.setActualSize(delShape.measuredWidth, delShape.measuredHeight); } } 云南大学学报(自然科学版),2002,24(3):167~171JournalofYunnanUniversity
转载请注明出处范文大全网 » 求树的节点数和叶子C代码计算叶子节点数
flex动态添加树的叶子节点的实现
度序列与树中的叶子数
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....