二叉树叶子节点遍历---递归与非递归方法求取树深度
2014-03-09 21:56
501 查看
#include <stdio.h> #include <stdlib.h> #define A 18 //二叉树前序遍历序列,深度5,叶子节点 int buffer[20]={10,6,4,-1,-1,8,-1,-1,14,12,3,-1,4,-1,-1,-1,16,-1,-1,-100}; //二叉树结构体 typedef struct binary_tree_node { int data; struct binary_tree_node* ltree; struct binary_tree_node* rtree; }Btnode; //创建新节点 Btnode* create_node(void) { Btnode* node; node=(Btnode*)malloc(sizeof(Btnode)); return node; } //据前序序列创建二叉树 /* 明确问题: (1)何时进行二叉树分支的切换 ①左分支遍历到叶子节点时 ②右分支有新的节点加入时 (2)何时节点入栈 新加入的非空节点 (3)何时节点出栈 某分支遍历到叶子节点时 */ Btnode* create_tree(int* buf) { Btnode* root; Btnode* pnode,*temp; Btnode* s[A]; bool ltree=true; int index=0; int m=0; root=create_node(); root->data=buf[index++]; s[m++]=root; pnode=root; while(buf[index]!=-100) { if(ltree==true) { if(buf[index]==-1) { pnode->ltree=NULL; index++; ltree=false; pnode=s[--m]; } else { temp=create_node(); temp->data=buf[index++]; pnode->ltree=temp; s[m++]=temp; pnode=temp; } } else { if(buf[index]==-1) { pnode->rtree=NULL; index++; pnode=s[--m]; } else { temp=create_node(); temp->data=buf[index++]; pnode->rtree=temp; s[m++]=temp; pnode=temp; ltree=true; } } } return root; } //遍历叶子节点 void leaf_traversal(Btnode* pnode) { if(pnode!=NULL) { if(pnode->ltree==NULL && pnode->rtree==NULL) { printf("%d ",pnode->data); } leaf_traversal(pnode->ltree); leaf_traversal(pnode->rtree); } return; } typedef struct node_information { Btnode* node; int depth; }ND_INF; //非递归法求取二叉树深度 /* 基本思想: 存储含有深度信息的节点,然后按二叉树的先序遍历进行 入出栈操作,根据栈顶节点确定当前入栈节点的深度信息. 出栈时使用temp保存出战节点的深度信息(用于右子树 入栈时深度信息的获取) 使用一变量进行二叉树深度信息的更新(即与当前的出栈 节点的深度信息进行比较) */ void tree_depth(Btnode* root) { ND_INF s[A]; Btnode* pnode; int m=0,temp=0; int depth=0; bool ltree=true; for(int i=0;i<A;i++) { s[i].depth=0; } pnode=root; s[m].depth=1; s[m++].node=pnode; while(pnode!=NULL) { if(ltree==true) { if(pnode->ltree!=NULL) { pnode=pnode->ltree; s[m].depth=s[m-1].depth+1; s[m++].node=pnode; } else { ltree=false; } } else { if(pnode->rtree!=NULL) { pnode=pnode->rtree; s[m].depth=temp+1; s[m++].node=pnode; ltree=true; } else { pnode=s[--m].node; temp=s[m].depth; if(s[m].depth>depth) { depth=s[m].depth; } } } if(m==-1) { break; } } printf("\n\nThe depth of the tree is %d (non-recursive method).\n\n",depth); } //递归方法求取二叉树的深度 int tree_depth1(Btnode* pnode) { int dep1,dep2; if(pnode!=NULL) { dep1=tree_depth1(pnode->ltree); dep2=tree_depth1(pnode->rtree); return dep1>dep2 ? dep1+1 : dep2+1; } return 0; } int main(void) { Btnode* root; int depth=0; root=create_tree(buffer); printf("The leaf nodes in the tree are:\n"); leaf_traversal(root); tree_depth(root); depth=tree_depth1(root); printf("The depth of the tree is %d (recursive method).\n\n",depth); system("pause"); return 0; }
相关文章推荐
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树,递归、非递归遍历,求深度,输出叶子节点
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- 二叉树的插入,递归遍历和深度节点数
- 不使用递归和堆栈,遍历二叉树所有叶子节点的算法
- 递归遍历 二叉树 求高度 和 节点数 和 叶子节点数
- 二叉树的深度 (递归与非递归方法整理)
- C++算法之 求二叉树的节点个数、深度、四种遍历方法
- 二叉树的遍历与求深度的递归与非递归实现