中序遍历二叉树的非递归的两种实现、线序遍历统计二叉树的叶子数、二叉树的深度
2013-05-12 15:55
190 查看
在此内容之上增加了中序遍历二叉树的非递归的两种实现、线序遍历统计二叉树的叶子数、二叉树的深度
//中序遍历二叉树非递归
public void InOrderTraverse(BinaryNode<T> node){
Stack<BinaryNode> stack=new Stack<BinaryNode>();
if(node!=null)
stack.add(node);
while(!stack.isEmpty()){
while(stack.peek()!=null){
node=stack.peek().left;
stack.push(node);
}
stack.pop();
if(!stack.isEmpty()){
node=stack.pop();
System.out.print(node.data+" ");
stack.push(node.right);
}
}
}
//中序遍历二叉树非递归
public void InOrderTraverse2(BinaryNode<T> node){
Stack<BinaryNode> stack=new Stack<BinaryNode>();
BinaryNode<T> p=node;
while(p!=null || !stack.isEmpty()){
if(p!=null){
p=stack.push(p);
p=p.left;
}else{
p=stack.pop();
System.out.print(p.data+" ");
p=p.right;
}
}
}
int count=0;
//先序遍历统计二叉树中的叶子节点
public int countLeaf(BinaryNode<T> node){
if(node!=null){
if(node.left==null&&node.right==null)
count++;
countLeaf(node.left);
countLeaf(node.right);
}
return count;
}
//统计二叉树的深度
public int treeDepth(BinaryNode<T> node){
int depthral=0;
int depthLeft=0;
int depthRight=0;
if(node==null)
depthral=0;
else{
depthLeft=treeDepth(node.left);
depthRight=treeDepth(node.right);
depthral=1+(depthLeft>depthRight? depthLeft:depthRight);
}
return depthral;
}
//中序遍历二叉树非递归
public void InOrderTraverse(BinaryNode<T> node){
Stack<BinaryNode> stack=new Stack<BinaryNode>();
if(node!=null)
stack.add(node);
while(!stack.isEmpty()){
while(stack.peek()!=null){
node=stack.peek().left;
stack.push(node);
}
stack.pop();
if(!stack.isEmpty()){
node=stack.pop();
System.out.print(node.data+" ");
stack.push(node.right);
}
}
}
//中序遍历二叉树非递归
public void InOrderTraverse2(BinaryNode<T> node){
Stack<BinaryNode> stack=new Stack<BinaryNode>();
BinaryNode<T> p=node;
while(p!=null || !stack.isEmpty()){
if(p!=null){
p=stack.push(p);
p=p.left;
}else{
p=stack.pop();
System.out.print(p.data+" ");
p=p.right;
}
}
}
int count=0;
//先序遍历统计二叉树中的叶子节点
public int countLeaf(BinaryNode<T> node){
if(node!=null){
if(node.left==null&&node.right==null)
count++;
countLeaf(node.left);
countLeaf(node.right);
}
return count;
}
//统计二叉树的深度
public int treeDepth(BinaryNode<T> node){
int depthral=0;
int depthLeft=0;
int depthRight=0;
if(node==null)
depthral=0;
else{
depthLeft=treeDepth(node.left);
depthRight=treeDepth(node.right);
depthral=1+(depthLeft>depthRight? depthLeft:depthRight);
}
return depthral;
}
相关文章推荐
- 用非递归实现二叉树的前序、中序、后序、层次遍历,用递归实现查找、统计个数、比较、求深度
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 由先序遍历序列和中序遍历序列恢复二叉树以及统计叶子节点个数和树的深度
- 二叉树的遍历C实现(非递归)—中序遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 【基础备忘】二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构 ——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 遍历|统计叶子节点|求深度|交换左右子树|查找是否存在某个特定叶子节点练习题
- 八.二叉树各种操作的C语言实现 深度遍历求深度,广度遍历求深度,交换左右子树,求叶子节点数
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)