二叉树的创建、遍历、深度、叶子节点数
2011-04-17 20:18
447 查看
回忆一下,呵呵
# include <stdio.h> # include <malloc.h> typedef struct BiTNode{ <mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script> int data; struct BiTNode *lchild; struct BiTNode *rchild; }treeNode, *tNode; void createTree(int a[]) { } /** * 插入节点到二叉树中 */ void insert(tNode root,int data) { tNode newNode = (tNode)malloc(sizeof(treeNode)); newNode->data = data; newNode->lchild = NULL; newNode->rchild = NULL; tNode current = root; tNode parent; while (1) { parent = current; if (current->data > newNode->data) { current = current->lchild; if (current == NULL) { parent->lchild = newNode; return; } } else { current = current->rchild; if (current == NULL) { parent->rchild = newNode; return; } } } } /** * 递归中序遍历二叉树 */ void preOrder(tNode root) { if (root != NULL) { preOrder(root->lchild); printf("%d ",root->data); preOrder(root->rchild); } } /** * 求二叉树叶子节点数目 */ int getLeaf(tNode root) { if (root == NULL) return 0; else if (root->lchild == NULL && root->rchild == NULL) return 1; else return getLeaf(root->lchild) + getLeaf(root->rchild); } /** * 求二叉树的深度 */ int getDepth(tNode root) { if (root == NULL) return 0; else return getDepth(root->lchild) > getLeaf(root->rchild)? 1 + getDepth(root->lchild): 1 + getDepth(root->rchild); // { // int depthLchild = 1 + getDepth(root->lchild); // int depthRchild = 1 + getDepth(root->rchild); // return depthLchild > depthRchild ? depthLchild: depthRchild; // } } int main() { tNode root = (tNode)malloc(sizeof(treeNode)); root->data = 10; root->lchild = NULL; root->rchild = NULL; // insert(root,10); insert(root,5); insert(root,15); insert(root,1); insert(root,8); insert(root,20); insert(root,12); preOrder(root); printf("/n"); int numleaf = getLeaf(root); printf("%d/n", numleaf); int depth = getDepth(root); printf("%d/n",depth); return 0; }
相关文章推荐
- 二叉树的字符串创建和遍历,求深度,叶子节点数
- 转载 二叉树的创建、遍历、深度、叶子节点数
- 基本数据结构——二叉树的建立,遍历,求叶子节点,深度计算
- 八.二叉树各种操作的C语言实现 深度遍历求深度,广度遍历求深度,交换左右子树,求叶子节点数
- 一个关于二叉树的创建、先序遍历、中序遍历、后序遍历、求叶子节点的完整函数的c语言完整程序。
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
- 二叉树,递归、非递归遍历,求深度,输出叶子节点
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- 二叉树的创建,遍历,求高度,算出节点数,算出叶子节点数,左右子树的交换,二叉树的销毁。
- 二叉树 遍历|统计叶子节点|求深度|交换左右子树|查找是否存在某个特定叶子节点练习题
- 二叉树的创建,遍历,叶节点数,总结点数,深度
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- 二叉树的建立、遍历、叶子节点计数、深度计算
- 二叉树基本操作--创建,三种遍历,叶子节点
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- 由先序遍历序列和中序遍历序列恢复二叉树以及统计叶子节点个数和树的深度
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- 二叉树创建以及遍历(j简单创建、遍历、叶子计数、深度计数、销毁)之1
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)