- 浏览: 128643 次
- 性别:
- 来自: 成都
最新评论
-
yi_chao_jiang:
你好,多谢分享,问个问题,在上传数据的时候判断文件是否有上传记 ...
断点续传和下载原理分析 -
a41606709:
为什么我的tabhost显示不出来? 怎么设置在全部页面中让他 ...
TabActivity中的Tab标签详细设置 -
Zero颴:
大神篇,思路,配图都很清晰,perfect!
Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解 -
QAZ503602501:
牛死人了!!!
B-树 -
mengsina:
很牛的文档。数学功底好啊
Android Matrix理论与应用详解
1.算法描述
就是简单的线索二叉树的建立,遍历,查找等基本操作,具体什么是线索二叉树,百度一下!
2.算法说明
具体说明有四点:如下图
注:尤其要注意第4点,我在写的时候就没注意这个问题,结果遍历的时候出现了无限循环,找了半天才找到!主要是建立二叉树判断的惯性思维,故而容易出现错误!
3.代码实现
头文件
#ifndef BITHRTREE_H #define BITHRTREE_H enum flag{CHILD, THREAD};//枚举类型,枚举常量Child=0,Thread=1 template<class T> struct Node{ //二叉线索树的结点结构 Node<T> *rchild, *lchild; int lflag, rflag; T data; }; template<class T> class BiThrTree{ public: BiThrTree(); ~BiThrTree(); Node<T>* getRoot( ); //获取根结点 Node<T>* next(Node<T>* p); //查找结点p的中序后继结点 void inOrder(Node<T>* root); //中序遍历线索链表 Node<T>* inPreNode(Node<T>* p); //查找结点p的中序前驱结点 Node<T>* searchNode(T x); //查找结点x private: Node<T>* root; //指向线索链表的头指针 void creatThrTree(Node<T>* &root); //创建二叉树 void biThrTree(Node<T>* &root, Node<T>* &pre); //二叉树的线索化(中序线索二叉树) void release(Node<T>* root); //析构函数调用 }; #endif
bithrtree.cpp
标//<-------------notice------------>就是容易出错的地方!!
#include <iostream> #include "bithrtree.h" using namespace std; template<class T> BiThrTree<T>::BiThrTree(){ Node<T>* pre = NULL; creatThrTree(root); biThrTree(root, pre); } template<class T> BiThrTree<T>::~BiThrTree(){ release(root); } //获取根结点 template<class T> Node<T>* BiThrTree<T>::getRoot( ){ return root; } //查找结点p的中序后继结点 template<class T> Node<T>* BiThrTree<T>::next(Node<T>* p){ Node<T>* t; //如果有右子树,则后继就是右子树的第一个结点最左节点,如果第一个结点没有左子树,则就是右子树第一个结点 if(p->rflag == CHILD){ //<-------------notice------------> t = p->rchild; while(t->lflag == CHILD){//即存在左子树.注:不能使用t->lchild判断,因为二叉树已经线索化,故而也不为空 t = t->lchild; } return t; }else{ return p->rchild; } } //中序遍历线索链表 template<class T> void BiThrTree<T>::inOrder(Node<T>* root){ if(root){ //先找到最左的结点 while(root->lflag == CHILD){//不能是root->lchild<-------------notice------------> root = root->lchild; } //开始中序遍历 do{ cout<<root->data<<" "; root = next(root); }while(root); } } //查找结点p的中序前驱结点 template<class T> Node<T>* BiThrTree<T>::inPreNode(Node<T>* p){ Node<T>* t; if(p->lflag == THREAD){//<-------------notice------------> return p->lchild; }else{ //如果p有左孩子,那么左孩子的最右结点就是其前驱 t = p->lchild; while(t->rflag == CHILD){//有右孩子(不要用t->rchild)<-------------notice------------> t = t->rchild; } return t; } } //查找结点x template<class T> Node<T>* BiThrTree<T>::searchNode(T x){ Node<T>* t = NULL; if(root){ //先找到最左的结点 while(root->lflag == CHILD){//<-------------notice------------> root = root->lchild; } //开始中序遍历 do{ if(root->data == x){ t = root; break; } root = next(root); }while(root); } return t; } //创建二叉树 template<class T> void BiThrTree<T>::creatThrTree(Node<T>* &root){ T ch; cout<<"请输入创建一棵二叉树的结点数据"<<endl; cin>>ch; if(ch == "#") root = NULL; else{ root = new Node<T>; root->data = ch; creatThrTree(root->lchild); creatThrTree(root->rchild); } } //二叉树的线索化(中序线索二叉树) template<class T> void BiThrTree<T>::biThrTree(Node<T>* &root, Node<T>* &pre){ if(root){ biThrTree(root->lchild, pre); //<--------------------------notice-------------------------> //左孩子处理(lflag和前驱) if(!root->lchild){ root->lflag = THREAD; root->lchild = pre; }else{ root->lflag = CHILD; } //右孩子处理(rflag不能处理后序,因为还没遍历到) if(!root->rchild){ root->rflag = THREAD; }else{ root->rflag = CHILD; } //pre的后续(rflag之前已经处理,如果设置为了索引,设置右索引为root) if(pre){ if(pre->rflag == THREAD) pre->rchild = root; } //<--------------------------notice-------------------------> //记录前驱 pre = root; biThrTree(root->rchild, pre); } } //析构函数调用 template<class T> void BiThrTree<T>::release(Node<T>* root){ if(root){ release(root->lchild); release(root->rchild); delete root; } }
main.cpp
#include <iostream> #include <string> #include "bithrtree.cpp" using namespace std; int main(){ BiThrTree<string> bt; Node<string>* t = bt.getRoot(); cout<<"------中序遍历-------"<<endl; bt.inOrder(t); cout<<endl; Node<string>* r; cout<<"查找结点b:"; r = bt.searchNode("b"); if(r) cout<<r->data<<endl; else cout<<"无"<<endl; cout<<"b的前驱:"; r = bt.inPreNode(r); if(r) cout<<r->data<<endl; else cout<<"无"<<endl; cout<<"a的前驱:"; r = bt.searchNode("a"); r = bt.inPreNode(r); if(r) cout<<r->data<<endl; else cout<<"无"<<endl; cout<<"d的前驱:"; r = bt.searchNode("d"); r = bt.inPreNode(r); if(r) cout<<r->data<<endl; else cout<<"无"<<endl; return 0; }
4.总结
基本上只要明白了二叉树的基本原理,线索二叉树就非常的简单,就多了一个线索化的过程,简化了搜索前序和后继结点的时间,提高了效率!
如果有什么不对的地方,欢迎指正!
发表评论
-
排序方法总结
2012-08-12 11:34 1131这里面包含了所有常见的排序操作 1.性能等比较分析 2 ... -
常见字符串操作大全
2012-08-12 11:34 14161.常见的字符串操作 如计算长度,求子串等都是考验基本功的, ... -
KMP算法解析
2012-08-12 11:35 2773一.理论基础 1.什么 ... -
二叉堆的实现
2012-08-12 11:35 11721.堆的概念 这里只需要注意两点: a.堆的存储方式:就是 ... -
set和map的简单实现
2012-08-10 11:35 12751.Set的简单实现 set是利用二叉查找树来实现的,而且为 ... -
红黑树的插入总结
2012-08-10 11:25 9051.红黑树 这个在july的博客中有详尽的说明,我就不在赘述 ... -
B-树实现
2012-08-10 11:03 15861.什么是B-树? 这个在我的前一篇博客中已经详细的阐释过: ... -
平衡二叉树
2012-08-10 10:39 27931.问题描述 什么是平衡 ... -
二叉排序树
2012-08-10 10:25 14151.基本概念 二叉排 ... -
B-树
2012-07-04 22:48 56091.B-树的概念 是一种多 ... -
构造哈夫曼树
2012-07-04 10:40 11871.算法说明 就是建造哈夫曼树树,从而使得构造出的树带权路径 ... -
二叉树基本操作大全
2012-07-03 18:22 25541.二叉树的基本操作 这里我有一个疑问: 在使用构造 ... -
多种队列的实现
2012-06-29 10:09 14911.算法描述 a.数据结构与算法(Mark Allen We ... -
栈的各种实现
2012-06-28 16:34 10731.算法描述 a.实现二个栈,在一个数组里面,除非没有任何空 ... -
中缀表达式转换为后缀
2012-06-28 11:10 17501.算法描述 例如a+b*c这是常见的中缀表达式,但是 ... -
后缀表达式的值
2012-06-27 16:33 12881.算法描述 计算后缀表达式的值 2.事例 如:( ... -
Josephus问题
2012-06-21 15:30 9531.算法描述 简单的游戏:有N个人坐成一圈,编号1-N、从编 ... -
关于最长递增子序列的实际应用--动态规划
2012-06-07 11:35 1788参考链接: a.http://www.programfan. ... -
线性查找二维数组
2012-06-05 17:23 9991.算法描述 有序(行有序,列有序,且每行从左至右递增 ... -
整数的随机置换
2012-06-05 15:10 2230算法描述:生成前N个整 ...
相关推荐
线索二叉树的一些基本功能 遍历 二叉树线索化 等等
中序线索二叉树(建立二叉树,线索化,输出二叉树)
自己做的线索二叉树,数据结构课设的时候可以参考哦!编译过的cpp文件。欢迎下载!
线索二叉树的建立 删除 插入 恢复线索
数据结构 线索二叉树 严蔚敏 数据结构 线索二叉树 严蔚敏 数据结构 线索二叉树 严蔚敏 数据结构 线索二叉树 严蔚敏 数据结构 线索二叉树 严蔚敏 数据结构 线索二叉树 严蔚敏
线索二叉树的先序,中序,后序遍历完整代码,在vs2013下编译通过
此程序需要完成如下要求:建立线索二叉树,并实现线索二叉树的插入、删除和恢复线索的实现。
数据结构 树与二叉树 线索二叉树 关于树与二叉树的线索二叉树小节课件
设计算法,在先序后继线索二叉树T中,查找给定结点*p在先序序列中的后继(假设二叉树T的根结点未知)。
线索二叉树的实现 C#实现 线索二叉树的实现 C#实现
线索二叉树的存储结构、线索化和遍历,其中的代码很不错
线索二叉树 算法 建立 输出等。。。 关于二叉树 包括非栈 非递归 的算法
线索二叉树的数据结构课程设计 包括需求分析,说明书,源代码
线索二叉树的数据结构课程设计 包括需求分析,说明书,源代码
09-线索二叉树.cpp
数据结构线索二叉树严蔚敏 树方面的知识很难 但只要把递归方面的机制搞懂即可
这是用c写的线索二叉树,是数据结构书上的,然后我又实现的。