1 # include <stdio.h>  2 # include <malloc.h>  3 typedef struct BiTNode{  4     struct BiTNode *lchild;  5     struct BiTNode *rchild;  6 }treeNode, *tNode;  7 void createTree(int a[])  8 {  9 } 10 /** 11  * 插入节点到二叉树中 12  */ 13 void insert(tNode root,int data) 14 { 15     tNode newNode = (tNode)malloc(sizeof(treeNode)); 16     newNode->data = data; 17     newNode->lchild = NULL; 18     newNode->rchild = NULL; 19     tNode current = root; 20     tNode parent; 21     while (1) 22     { 23         parent = current; 24         if (current->data > newNode->data) 25         { 26             current = current->lchild; 27             if (current == NULL) 28             { 29                 parent->lchild = newNode; 30                 return; 31             } 32         } 33         else 34         { 35             current = current->rchild; 36             if (current == NULL) 37             { 38                 parent->rchild = newNode; 39                 return; 40             } 41         } 42     } 43 } 44 /** 45  * 递归中序遍历二叉树 46  */ 47 void preOrder(tNode root) 48 { 49     if (root != NULL) 50     { 51         preOrder(root->lchild); 52         printf("%d  ",root->data); 53         preOrder(root->rchild); 54     } 55 } 56 /** 57  * 求二叉树叶子节点数目 58  */ 59 int getLeaf(tNode root) 60 { 61     if (root == NULL) 62         return 0; 63     else 64         if (root->lchild == NULL && root->rchild == NULL) 65             return 1; 66         else 67             return getLeaf(root->lchild) + getLeaf(root->rchild); 68 } 69 /** 70  * 求二叉树的深度 71  */ 72 int getDepth(tNode root) 73 { 74     if (root == NULL) 75         return 0; 76     else 77         return getDepth(root->lchild) > getLeaf(root->rchild)? 1 + getDepth(root->lchild): 1 + getDepth(root->rchild); 78 //    { 79 //        int depthLchild = 1 + getDepth(root->lchild); 80 //        int depthRchild = 1 + getDepth(root->rchild); 81 //        return depthLchild > depthRchild ? depthLchild: depthRchild; 82 //    } 83 } 84 int main() 85 { 86     tNode root = (tNode)malloc(sizeof(treeNode)); 87     root->data = 10; 88     root->lchild = NULL; 89     root->rchild = NULL; 90 //    insert(root,10); 91     insert(root,5); 92     insert(root,15); 93     insert(root,1); 94     insert(root,8); 95     insert(root,20); 96     insert(root,12); 97     preOrder(root); 98     printf("/n"); 99     int numleaf = getLeaf(root);100     printf("%d/n", numleaf);101     int depth = getDepth(root);102     printf("%d/n",depth);103     return 0;104 }

编辑器加载中...

转载于:https://www.cnblogs.com/kyleada/archive/2011/08/11/2135459.html

转载 二叉树的创建、遍历、深度、叶子节点数相关推荐

  1. 数据结构:二叉树的创建,打印前中后序遍历,节点个数,叶子节点数,销毁,第K层中节点的个数,查找值为x的节点

    二叉树遍历:按照某种特定的规则,依次对二叉树中的节点进行相应的操作,并且每个节点只操作一次.(采用递归思想) 先序遍历:先遍历根节点,再遍历根节点的左子树,最后遍历根节点的右子树. 中序遍历:先遍历左 ...

  2. 【数据结构】——二叉树的创建、计算叶子结点个数、递归遍历

    前面我们讲了关于数据结构中的堆栈问题,这篇文章主要是为大家简要介绍一下二叉树,并实现二叉树的创建.计算叶子结点个数.递归遍历.判断是否是完全二叉树等相关问题~ 一.二叉树的介绍 1.什么是二叉树 一棵 ...

  3. 数据结构:二叉树的深度与叶子节点数

    输入二叉树的先序序列,计算并输出该二叉树的深度与叶子节点个数.请将代码填写完整. 例如,下面二叉树对应的先序序列为"AB#D##C##". A /    \ B      C \ ...

  4. java二叉树的创建,遍历及其他方法

    2019独角兽企业重金招聘Python工程师标准>>> package tree;import java.util.Stack;/**二叉树**/ public class Bina ...

  5. c语言二叉树结点 深度,求二叉树的深度、总结点数和叶子结点数

    二叉树的二叉链表的存储结构: typedef   char   TElemType; typedef   struct    BiTNode { TElemType data;//数据元素 BiTNo ...

  6. C++数据结构与算法—用递归方法求二叉树的叶子结点数

    用递归方法写一个算法,求二叉树的叶子结点数int leafnum(BTREE T). 要求: 1.定义二叉树的抽象数据类型和型BTREE,并定义基本操作. 2.编写函数1eafnum(BTREE T, ...

  7. 二叉树所有节点数、叶子节点数的计算

    一.假设二叉树采用二叉链存储结构,设计一个算法,计算一颗给定二叉树的所有节点数. 解法一 int n; void count(BTNode* p) {if(p){++n;count(p->lef ...

  8. 如何计算给定二叉树中的叶节点数?

    叶子节点的特征是无左孩子也无右孩子,还要注意与树只有一个节点的情况区分. 一.程序计算 int leaf(bitree t) {if(!t) return 0; //空树,无叶子 else if(!t ...

  9. 已知一棵完全二叉树的节点数n,求叶节点数

    题目如题 假设完全二叉树中,度为0的节点(即叶节点)数目为n0,度为1的节点数为n1,度为2的数目为n2,总数为n 首先我们得知道两个公式 结点总数满足: n = n0 + n1 + n2 出度.入度 ...

最新文章

  1. Spring Cloud中Hystrix仪表盘与Turbine集群监控
  2. table 锁定表头
  3. mindspore学习之使用obsutil工具向桶中上传数据
  4. Object overview 页面点击Edit button白屏问题
  5. java 8.0 sinffer_jpcap 配置方法,问题解决,模拟sniffer程序。(附JAVA程序,jar,dll包等环境)...
  6. 电脑无法读取移动硬盘_移动硬盘U盘提示:文件或目录损坏且无法读取如何解决?...
  7. 线上FullGC频繁的排查
  8. 吴恩达机器学习 11.聚类
  9. 有选择的忽略PyCharm 3的PEP8语言风格警告提示信息
  10. 读《Jonathan von Neumann and EDVAC》
  11. 60几行代码绘制丘比特爱情之箭!
  12. 面试常见的功能测试考试题关于测试方法的
  13. 涅槃?高效报表开发人员的五件武器
  14. SwiftUI学习(一)
  15. MIUI13来了,米粉们还期待吗?
  16. AB罗克韦尔plc指示灯详解
  17. 如何将两个路由器连接在一起
  18. 爆改小米3G路由器,openwrt踩坑
  19. 什么是vps?vps和代理ip的本质区别?
  20. T11 Origin绘图模板

热门文章

  1. Oracle中的不等于号
  2. jquery获取表格中特定列
  3. linux基础(十一)--系统初始化的简谈
  4. 深入理解SQL Server的规划和安装
  5. Spring 依赖注入(集合)/util命名空间/自动注入
  6. apache服务 功能错误_如何使用Apache OpenWhisk开发功能即服务
  7. 带通滤波中零相位和最小相位_相位器在Perl 6中的工作方式
  8. 如何使用Emacs Org模式和Reveal.js创建幻灯片
  9. Bootstrap创建按钮工具栏
  10. vrep中remoteAPI 编程中遇到的没有预留命令执行时间遇到的问题