#include "stdio.h"
#include "stdlib.h" #define OK 1
#define ERROR 0 typedef char TElemType;
typedef char Elemtype;
typedef int Status;
typedef enum PointerTag {Link,Thread}; //link==0:pointer,Thread==1:thread
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode,*BiThrTree; BiThrTree pre; // 全局变量,始终指向刚刚访问过的结点 void InThreading(BiThrTree p)
{
if(p)
{
InThreading(p->lchild);//左子树线索化
if(!p->lchild){p->LTag=Thread;p->lchild=pre;}//前驱线索
if(!pre->rchild){pre->RTag=Thread;pre->rchild=p;}//后续线索
pre=p; //保持pre指向p的前驱
InThreading(p->rchild);//右子树线索化
}//if
}//InThreading Status InOrderThreading(BiThrTree &Thrt,BiThrTree T)
{
//中序遍历二叉树,并将其中序线索化,Thrt指向头结点
//BiThrTree Thrt;
if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(-1);
Thrt->LTag=Link; //建立头结点
Thrt->RTag=Thread; //右指针回指
Thrt->rchild=Thrt;
if(!T) Thrt->rchild=Thrt; //若二叉树为空,则左指针回指
else {
Thrt->lchild=T;
pre=Thrt;
InThreading(T); //中序遍历进行中序线索化
pre->rchild=Thrt;//最后一个结点线索化
pre->RTag=Thread;
Thrt->rchild=pre;
}
return OK;
}//InOrderThreading Status InOrderTraverse_Thr(BiThrTree T)
{
//T指向头结点,头结点的左链lchild指向根结点,非递归算法
BiThrTree p;
p=T->lchild;
while(p!=T) //空树或遍历结束时,T==p
{
while(p->LTag==Link) p=p->lchild;
printf("%c",p->data); //访问其左子树为空的结点
while(p->RTag==Thread&&p->rchild!=T)
{
p=p->rchild;
printf("%c",p->data); //访问后续结点
}//while
p=p->rchild;
}//while
printf("\n");
return OK;
}//InOrderT_Thr BiThrTree InOrderSearch_Thr(BiThrTree T,Elemtype ch)
{
//T指向头结点,头结点的左链lchild指向根结点,非递归算法,查找指定字符ch,找到返回,未找到返回NULL
BiThrTree p;
p=T->lchild;
while(p!=T) //空树或遍历结束时,T==p
{
while(p->LTag==Link) p=p->lchild;
if(p->data==ch) return p; //找到,返回该结点指针
while(p->RTag==Thread&&p->rchild!=T)
{
p=p->rchild;
if(p->data==ch) return p; //找到,返回该结点指针
}//while
p=p->rchild;
}//while
return NULL;
}//InOrderT_Thr Status CreateBitree(BiThrTree &T)
{//按先序次序输入二叉树
char ch;
scanf("%c",&ch);
if(ch==' ') T=NULL;
else{
if(!(T=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(1);
T->data=ch;T->LTag=Link;T->RTag=Link;
CreateBitree(T->lchild);
CreateBitree(T->rchild);
}//else
return OK;
}//CreateBitree Status printelemt(Elemtype e)
{
printf("%c",e);
return OK;
} Status preordertraverse(BiThrTree t,Status (*visit)(Elemtype e))
{
if(t)
{
if(visit(t->data))
if(preordertraverse(t->lchild,visit))
if(preordertraverse(t->rchild,visit)) return OK;
return ERROR;
}
else
return OK;
} Status inordertraverse(BiThrTree t,Status (*visit)(Elemtype e))
{
if(t)
{
if(inordertraverse(t->lchild,visit))
if(visit(t->data))
if(inordertraverse(t->rchild,visit)) return OK;
return ERROR;
}
else
return OK;
} Status postordertraverse(BiThrTree t,Status (*visit)(Elemtype e))
{
if(t)
{
if(postordertraverse(t->lchild,visit))
if(postordertraverse(t->rchild,visit))
if(visit(t->data)) return OK;
return ERROR;
}
else
return OK;
} BiThrTree InNext(BiThrTree p)
/*在中序线索二叉树中查找p的中序后继结点,并用next指针返回结果*/
{
BiThrTree Next;
BiThrTree q;
if (p->RTag==1)
Next = p->rchild;
else
{
if(p->rchild!=NULL)
{
for(q=p->rchild; q->LTag==0 ;q=q->lchild);
Next=q;
}
else
Next = NULL;
}
return(Next);
} BiThrTree InPre(BiThrTree p)
/* 在中序线索二叉树中查找p的中序前驱, 并用pre指针返回结果 */
{
BiThrTree q;
if(p->LTag==1)
pre = p->lchild;
else
{
for(q = p->lchild;q->RTag==0;q=q->rchild);
pre=q;
}
return(pre);
} int main()
{
char ch;
BiThrTree T,Thrt,p,q;
CreateBitree(T);//创建
preordertraverse(T,printelemt);
printf("\n");
inordertraverse(T,printelemt);
printf("\n");
postordertraverse(T,printelemt);
printf("\n");
InOrderThreading(Thrt,T);//线索化
printf("中序遍历该线索二叉树得到的序列为:\n");
InOrderTraverse_Thr(Thrt);//遍历访问
getchar();
printf("请输入树中的一个字符");
scanf("%c",&ch); p=InOrderSearch_Thr(T,ch);
q=InNext(p);
printf("后继结点:%c\n",q->data);
q=InPre(p);
printf("先趋结点:%c\n",q->data);
return OK;
}

bithrtree的更多相关文章

  1. 二叉树的创建和遍历(C版和java版)

    以这颗树为例:#表示空节点前序遍历(根->左->右)为:ABD##E##C#F## 中序遍历(左->根->右)为:#D#B#E#A#C#F# 后序遍历(左->右-> ...

  2. Tree

    //Header.h #ifndef _HEAD_ #define _HEAD_ #include <queue> #include <iostream> using name ...

  3. (原创)Python文件与文件系统系列(5)——stat模块

    stat模块中定义了许多的常量和函数,可以帮助解释 os.stat().os.fstat().os.lstat()等函数返回的 st_result 类型的对象. 通常使用 os.path.is*() ...

  4. Data Structure 之 二叉树

          在计算机科学中,二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用于实现二叉查找树和二叉堆 ...

  5. 线索二叉树Threaded binary tree

    摘要   按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...

  6. 数据结构中,几种树的结构表示方法(C语言实现)

    //***************************************** //树的多种结构定义 //***************************************** # ...

  7. 树和二叉树->线索二叉树

    文字描述 从二叉树的遍历可知,遍历二叉树的输出结果可看成一个线性队列,使得每个结点(除第一个和最后一个外)在这个线形队列中有且仅有一个前驱和一个后继.但是当采用二叉链表作为二叉树的存储结构时,只能得到 ...

  8. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  9. Chapter 6(树)

    1.树的储存方式 //****************双亲表示法************************ #define Max_TREE_SIZE 100 typedef int TElem ...

随机推荐

  1. Oracle11g数据库导入到oracle10g的解决方法

    我想有很多人在工作和学习中遇到这样的一个问题,Oracle数据库服务器版本和本机版本不一致问题,你的本机要是比服务器的版本要高的话还好,如果你本机是10g服务器是11g的话,从11g导出来的数据库是导 ...

  2. Orace数据库锁表的处理与总结<摘抄与总结三>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  3. Tensor神经网络进行知识库推理

    本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...

  4. UIViewAnimationOptions类型

    一个非常强大的博客 http://www.cnblogs.com/kenshincui/    像我这种新手确实应该多看看   常规动画属性设置(可以同时选择多个进行设置) UIViewAnimati ...

  5. javascript社交平台分享-新浪微博、QQ微博、QQ好友、QQ空间、人人网

    整理的五个社交平台的分享 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  6. c#中设置按钮Button为透明

    方法一:代码实现 /// <summary> /// 设置透明按钮样式 /// </summary> private void SetBtnStyle(Button btn) ...

  7. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...

  8. sqlserver-事务处理

    事务的概念:简单说就访问并可能更新数据库中各种数据项的一个程序执行单元,一旦开启事务,所有对数据的操作要么全部执行,要么全部都不执行.单条sql语句本身就是一个事务. 事务的属性: 事务是作为单个逻辑 ...

  9. 【POJ3468】【树状数组区间修改】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  10. Java实现Linux下服务器程序的双守护进程

    作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.简介 现在的服务器端程序很多都是基于Java开发,针对于Java开发的Socket程序,这样的服务器端上线后出现问 ...