根据二叉树的先序,中序,后序遍历序列将其线索化并在线索二叉树中查找前驱后继结点

#include "stdafx.h"
#include
#include
#include
typedef char type;
typedef struct threaded_binary_tree_node
{
	type data;
	bool ltag;
	threaded_binary_tree_node *lchild;
	bool rtag;
	threaded_binary_tree_node *rchild;

}threaded_binary_tree_node;
//通过先序的顺序输入根据其值创建二叉树,如果某个节点其左子树为空,
//则用 * 代替其左子树节点值,如果某个节点其右子树为空,则用 * 代替其右子树节点值
threaded_binary_tree_node* pre_createtree(threaded_binary_tree_node *t, type *value) 
{
	static int i = 0;
	char temp = value[i++];
	t = (threaded_binary_tree_node*)malloc(sizeof(threaded_binary_tree_node));
	if (temp == '*')
	{
		t = NULL;
		return NULL;
	}
	else
	{
		t->data = temp;
		t->ltag = t->rtag = false;
	}
	t->lchild = pre_createtree(t->lchild, value);
	t->rchild = pre_createtree(t->rchild, value);
	return t;
}
//通过中序的顺序输入根据其值创建二叉树,如果某个节点其左子树为空,
//则用 * 代替其左子树节点值,如果某个节点其右子树为空,则用 * 代替其右子树节点值
threaded_binary_tree_node* mid_createtree(threaded_binary_tree_node *t, type *value)
{
	static int i = 0;
	char temp = value[i++];
	t = (threaded_binary_tree_node*)malloc(sizeof(threaded_binary_tree_node));
		t->lchild = mid_createtree(t->lchild, value);
		if (temp == '*')
		{
			t = NULL;
			return NULL;
		}
		else
		{
			t->data = temp;
			t->ltag = t->rtag = false;
		}
		t->rchild = mid_createtree(t->rchild, value);
	
	return t;
}
//通过后序的顺序输入根据其值创建二叉树,如果某个节点其左子树为空,
//则用 * 代替其左子树节点值,如果某个节点其右子树为空,则用 * 代替其右子树节点值
threaded_binary_tree_node* post_createtree(threaded_binary_tree_node *t, type *value)
{
	static int i = 0;
	char temp = value[i++];
	t = (threaded_binary_tree_node*)malloc(sizeof(threaded_binary_tree_node));
		t->lchild = post_createtree(t->lchild, value);
		t->rchild = post_createtree(t->rchild, value);
		if (temp == '*')
		{
			t = NULL;
			return NULL;
		}
		else
		{
			t->data = temp;
			t->ltag = t->rtag = false;
		}
	return t;
}
//二叉树的中序线索化
void midorderthread(threaded_binary_tree_node* root)
{
	static threaded_binary_tree_node*pre = NULL;
	if (root)
	{
		midorderthread(root->lchild);
		if (root->lchild == NULL)
		{
			root->lchild = pre;
			root->ltag = true;
		}
		if (pre&&pre->rchild == NULL)
		{
			pre->rchild = root;
			pre->rtag = true;
		}
		pre = root;
		midorderthread(root->rchild);
	}
}
//二叉树的先序线索化
void preorderthread(threaded_binary_tree_node* root)
{
	static threaded_binary_tree_node*pre = NULL;
	if (root)
	{
		if (root->lchild == NULL)
		{
			root->lchild = pre;
			root->ltag = true;
		}
		if (pre&&pre->rchild == NULL)
		{
			pre->rchild = root;
			pre->rtag = true;
		}
		pre = root;
		midorderthread(root->lchild);
		midorderthread(root->rchild);
	}
}
//二叉树的后序线索化
void postorderthread(threaded_binary_tree_node* root)
{
	static threaded_binary_tree_node*pre = NULL;
	if (root)
	{
		midorderthread(root->lchild);
		midorderthread(root->rchild);
		if (root->lchild == NULL)
		{
			root->lchild = pre;
			root->ltag = true;
		}
		if (pre&&pre->rchild == NULL)
		{
			pre->rchild = root;
			pre->rtag = true;
		}
		pre = root;
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	threaded_binary_tree_node *tree1 = NULL;
	threaded_binary_tree_node *tree2 = NULL;
	threaded_binary_tree_node *tree3 = NULL;
	char value1[1024];
	printf("请输入先序序列:\n");
	gets_s(value1);
	char value2[1024];
	printf("请输入中序序序列:\n");
	gets_s(value2);
	char value3[1024];
	printf("请输入后序序列:\n");
	gets_s(value3);
	tree1=pre_createtree(tree1, value1);
	preorderthread(tree1);
	tree2=mid_createtree(tree2, value2);
	midorderthread(tree2);
	tree3=post_createtree(tree3, value3);
	postorderthread(tree3);
	return 0;
}

你可能感兴趣的:(数据结构与算法)