二叉树线索化
来源:互联网 发布:nginx动静分离原理 编辑:程序博客网 时间:2024/06/08 04:38
转自:http://blog.csdn.net/algorithm_only/article/details/6991254
// TrreadBiTree.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#define ERROR -1
#define OVERFLOW -2
#define OK 1
typedef int elemtype;
typedef enum {link, thread}pointer_tag;
typedef struct tnode {
elemtype data;
struct tnode *lchild, *rchild;
pointer_tag ltag, rtag;
}bithrnode, *bithrtree;
bithrtree pre;
void init_bithrtree(bithrtree *);
int create_bithrtree(bithrtree *);
int inorder_threading(bithrtree *, bithrtree );
void in_threading(bithrtree);
int inorder_traverse_thr(bithrtree, int (*visit)(bithrtree ));
int output_data(bithrtree);
void free_bithrtree(bithrtree *);
void init_bithrtree(bithrtree *bt)
{
*bt = NULL;
}
int create_bithrtree(bithrtree *bt)
{
elemtype data;
scanf("%d", &data);
if (0 == data) {
*bt = NULL;
// return OK;
}
else {
*bt = (bithrtree)malloc(sizeof(bithrnode));
if (!(*bt))
exit(OVERFLOW);
(*bt)->data = data;
create_bithrtree(&(*bt)->lchild);
create_bithrtree(&(*bt)->rchild);
}
return OK;
}
int inorder_threading(bithrtree *thrt, bithrtree bt)
{
*thrt = (bithrtree) malloc (sizeof(bithrnode));
if (!*thrt)
exit(OVERFLOW);
(*thrt)->ltag = link;
(*thrt)->rtag = thread;
(*thrt)->rchild = (*thrt);
if (!bt)
(*thrt)->lchild = (*thrt);
else {
(*thrt)->lchild = bt;
pre = (*thrt);
in_threading(bt);
pre->rchild = *thrt;
pre->rtag = thread;
(*thrt)->rchild = pre;
}
return OK;
}
void in_threading(bithrtree t)
{
if (t) {
in_threading(t->lchild);
if (!t->lchild) {
t->ltag = thread;
t->lchild = pre;
}
if (!pre->rchild) {
pre->rtag = thread;
pre->rchild = t;
}
pre = t;
in_threading(t->rchild);
}
}
int inorder_traverse_thr(bithrtree thrt, int (*visit)(bithrtree t))
{
bithrtree p;
p = thrt->lchild;
while (p != thrt) {
while (p->ltag == link)
p = p->lchild;
visit(p);
while (p->rtag == thread && p->rchild != thrt) {
p = p->rchild;
visit(p);
}
p = p->rchild;
}
return OK;
}
int output_data(bithrtree t)
{
printf("%s\t%d\t%s\n", t->ltag==0 ? "link" : "thread",
t->data,
t->rtag==0 ? "link" : "thread");
return OK;
}
void free_bithrtree(bithrtree *bt)
{
if (*bt) {
if ((*bt)->lchild)
free_bithrtree(&(*bt)->lchild);
if ((*bt)->rchild)
free_bithrtree(&(*bt)->rchild);
free(*bt);
*bt = NULL;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bithrtree thrt, bt;
init_bithrtree(&bt);
printf("Input elemtype to create bithrtree with preorder:\n"
"(for exemple: 1 2 3 0 0 4 5 0 6 0 0 7 0 0 0) > ");
if ( OK != create_bithrtree(&bt))
return -1;
if (OK != inorder_threading(&thrt, bt))
return -1;
printf("After inorder threading, traverse the threading bitree:\n");
if (OK != inorder_traverse_thr(thrt, output_data))
return -1;
return 0;
}
0 0
- 线索化二叉树
- 二叉树线索化
- 线索化二叉树
- 线索化二叉树
- 线索化二叉树
- 线索化二叉树
- 线索化二叉树
- 二叉树线索化
- 线索化二叉树
- 线索化二叉树
- 二叉树线索化
- 线索化二叉树
- 线索化二叉树
- 线索化二叉树
- 线索化二叉树
- 二叉树线索化
- 线索化二叉树
- 线索化二叉树
- redis eval命令
- XSS遇到后台登录限制或者HTTPONLY时
- Eclipse Java EE IDE for Web Developers 配置Tomcat
- 学习NodeMCU的低功耗休眠
- UIApplicationDelegate协议解读
- 二叉树线索化
- linux运维职业路线
- Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法
- Binary Tree Maximum Path Sum
- 后海日记(1)
- ArcGIS教程:3D表面的基础知识(二)
- Android中Service类onStartCommand
- leetcode 203 Remove Linked List Elements
- yum-axelget setup in centos 6.x