大学数据结构实验(四.树型结构及其应用四)

网友投稿 529 2022-11-24

大学数据结构实验(四.树型结构及其应用四)

大学数据结构实验(四.树型结构及其应用四)

大学程序实验.数据结构.树型结构及其应用四.算子树数

​​0 目录​​​​4 树型结构及其应用​​

​​4.4 算子树数​​

​​4.4.1 题目​​​​4.4.2 源码​​​​1.1.3 -​​

​​2 下一章​​

0 目录

4 树型结构及其应用

4.4 算子树数

4.4.1 题目

编写算法求二叉树中某个结点的子孙结点(不包括该结点)为多少个。

4.4.2 源码

// 算子树数.cpp : Defines the entry point for the console application.//#include "stdio.h"#include "stdlib.h"#include "string.h"#include "malloc.h"#define OK 1#define ERROR 1typedef int Status;typedef char TElemType;typedef struct BiTNode{ TElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;void BiTreeMenu();Status CreateBiTree(BiTree *T);Status DescNumCount(BiTree T,int &DescNum);BiTree PreOrderLOC(BiTree T,TElemType e);void BiTreeMenu(){ printf("========算子孙结点算法========\n"); printf("二叉树创建\n"); printf("请输入二叉树各节点数:");}Status CreateBiTree(BiTree *T) { TElemType ch; TElemType temp; scanf("%c",&ch); temp=getchar(); if(ch == '#') { *T = NULL; } else { *T=(BiTree)malloc(sizeof(BiTNode) ); if(!(*T)) { return ERROR; } else { (*T)->data=ch; printf("请输入%c的左节点的值:",ch); CreateBiTree(&(*T)->lchild); printf("请输入%c的右节点的值:",ch); CreateBiTree(&(*T)->rchild); } } return OK; }Status DescNumCount(BiTree T,int &DescNum){ if(!T) { return ERROR; } if(T->lchild) { DescNum++; } if(T->rchild) { DescNum++; } DescNumCount(T->lchild,DescNum); DescNumCount(T->rchild,DescNum); return DescNum;} BiTree PreOrderLOC(BiTree T,TElemType e){ if(!T) { return NULL; } if(T->data == e) { return T; } BiTree pTNode = PreOrderLOC(T->lchild,e); if (pTNode) { return pTNode; } pTNode = PreOrderLOC(T->rchild,e); if (pTNode) { return pTNode; } else { return NULL; }} Status main(){ int DescNum=0; TElemType value; BiTree pRoot; BiTree *p=(BiTree*)malloc(sizeof(BiTree)); BiTreeMenu(); printf("请输入第一个节点的值,'#'代表没有叶节点:\n"); CreateBiTree(&pRoot); printf("\n"); printf("请输入你要计算子孙的结点:"); scanf("%c",&value); pRoot = PreOrderLOC(pRoot,value); DescNum = DescNumCount(pRoot,DescNum); printf("该节点的子孙结点个数为:"); printf("%d",DescNum); printf("\n"); return OK;}

1.1.3 -

链接地址: ​​4.4_算子树数.CPP​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:大学数据结构实验(五.图型结构的应用一)
下一篇:大学C语言实验(一.数据类型入门汇总)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~