求二叉树中每一个节点对应的层次
2015-09-21 15:58
337 查看
转载自:http://wenku.baidu.com/view/b9c8c4115f0e7cd1842536da.html
#include "stdafx.h" #include<iostream> using namespace std; int i=1; typedef struct bnode { char data; struct bnode*lchild,*rchild; }bnode; class bitree { public: void preorder(bnode*t); void inorder(bnode*t); void postorder(bnode*t); bnode * get_root(){return root;} void creat(bnode*&t); void inorder_printjie(bnode*t,int i); private: bnode * root; }; void bitree::preorder (bnode*t) { if(t!=NULL) { cout<<t->data<<" "; preorder(t->lchild ); preorder(t->rchild ); } } void bitree::inorder (bnode*t) { if(t!=NULL) { inorder(t->lchild ); cout<<t->data<<" " ; inorder(t->rchild ); } } void bitree::creat (bnode*&t) { char x; cin>>x; if(x=='.')t=NULL; else { t=new bnode; t->data =x; creat(t->lchild ); creat(t->rchild ); } } void bitree::inorder_printjie (bnode*t,int i) { if(t!=NULL) { inorder_printjie(t->lchild ,i+1); cout<<" ("<<t->data <<","<<i<<")"; inorder_printjie(t->rchild ,i+1); } } void main() { bitree a; bnode*t=a.get_root (); cout<<"请输入A二叉树,以.表示没有孩子数目:"<<endl; a.creat (t); cout<<endl; cout<<"以中序的方式输出A二叉树:"<<endl; a.inorder(t); cout<<endl; cout<<"以中序输出二叉树各结点的值及对应的层次:"<<endl; a.inorder_printjie (t,i); getchar(); }
<span style="font-size:24px;">虽然能够求得每个节点对应的层次,但还是不能求得每一层的节点,还需要好好研究才行。</span>
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- C++非递归队列实现二叉树的广度优先遍历
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#非递归先序遍历二叉树实例
- C++非递归建立二叉树实例
- C语言实现找出二叉树中某个值的所有路径的方法
- C++实现二叉树遍历序列的求解方法
- C语言实现二叉树遍历的迭代算法
- 用C语言判断一个二叉树是否为另一个的子结构
- C++实现二叉树非递归遍历方法实例总结
- C++二叉树结构的建立与基本操作
- 深入遍历二叉树的各种操作详解(非递归遍历)
- JavaScript数据结构和算法之二叉树详解
- java使用归并删除法删除二叉树中节点的方法
- Java中二叉树数据结构的实现示例
- python数据结构之二叉树的建立实例
- python数据结构树和二叉树简介
- python数据结构之二叉树的统计与转换实例