数据结构造轮子3.1-----二叉树的创建,递归遍历,线索化,统计叶子数,节点数,深度

BinaryTree.h

#pragma once

#include "stdafx.h"

#define Link 1
#define Thread 0

class BinaryTree
{
public:
	struct node
	{
		int data;
		node * lchild;
		node * rchild;
		int ltag;
		int rtag;
	} *root;
	
	node * in_thrt;
	node * pre_thrt;
	node * post_thrt;

	node * pre;//前驱

	void InitTree();

	void CreateTree(node * & root);
	
	void Pre(node * root);
	void In(node * root);
	void Post(node * root);

	int CountLeaf(node * root);
	int CountNode(node * root);
	int CountDepth(node * root);

	bool InThreading(node * & root);
	bool PreThreading(node * & root);
	bool PostThreading(node * & root);

	bool In_Thrt();
	bool Pre_Thrt();
	bool Post_Thrt();

protected:
	void InThread(node * & root);
	void PreThread(node * & root);
	void PostThread(node * & root);
};

BinaryTree.cpp

#include "stdafx.h"
#include "Stack.h"
#include "BinaryTree.h"
#include "iostream"

using namespace std;

/*
	初始化
*/
void BinaryTree::InitTree()
{
	root = NULL;
}

/*
	建树
*/
void BinaryTree::CreateTree(node * & root)
{
	int i;
	cin >> i;
	if (i != 0)
	{
		root = (node *)malloc(sizeof(node));
		root->data = i;
		root->lchild = NULL;
		root->rchild = NULL;
		CreateTree(root->lchild);
		CreateTree(root->rchild);
	}
}

/*
	前中后序递归遍历
*/
void BinaryTree::Pre(node * root)
{
	if (root != NULL)
	{
		cout << " " << root->data << " ";
		Pre(root->lchild);
		Pre(root->rchild);
	}
}

void BinaryTree::In(node * root)
{
	if (root != NULL)
	{
		In(root->lchild);
		cout << " " << root->data << " ";
		In(root->rchild);
	}
}

void BinaryTree::Post(node * root)
{
	if (root != NULL)
	{
		Post(root->lchild);
		Post(root->rchild);
		cout << " " << root->data << " ";
	}
}

/*
	特殊节点统计
*/
int BinaryTree::CountLeaf(node * root)
{
	if (root)
	{
		if (root->rchild == NULL && root->lchild == NULL)return 1;
		return CountLeaf(root->lchild) + CountLeaf(root->rchild);
	}
	return 0;
}

int BinaryTree::CountNode(node * root)
{
	if (!root)return 0;
	int i = CountNode(root->lchild);
	int j = CountNode(root->rchild);
	return i + j + 1;
}

int BinaryTree::CountDepth(node * root)
{
	if (!root)return 0;
	int i = CountDepth(root->lchild);
	int j = CountDepth(root->rchild);
	if (i > j) return i + 1;
	else return j + 1;
}

/*
	线索化操作
*/
bool BinaryTree::InThreading(node * & root)
{
	in_thrt = (node *)malloc(sizeof(node));
	in_thrt->ltag = Link;
	in_thrt->rtag = Thread;
	in_thrt->rchild = in_thrt;
	if (!root)in_thrt->lchild = in_thrt;
	else
	{
		in_thrt->lchild = root;
		pre = in_thrt;
		InThread(root);
		pre->rchild = in_thrt;
		pre->rtag = Thread;
		in_thrt->rchild = pre;
	}

	return true;
}

void BinaryTree::InThread(node * & root)
{
	if (root)
	{
		InThread(root->lchild);
		if (!root->lchild)
		{
			root->lchild = pre;
			root->ltag = Thread;
		}
		if (!pre->rchild)
		{
			pre->rchild = root;
			pre->rtag = Thread;
		}
		pre = root;
		InThread(root->rchild);
	}
}

bool BinaryTree::PreThreading(node * & root)
{
	pre_thrt = (node *)malloc(sizeof(node));
	pre_thrt->ltag = Link;
	pre_thrt->rtag = Thread;
	pre_thrt->rchild = pre_thrt;
	if (!root)pre_thrt->lchild = pre_thrt;
	else
	{
		pre_thrt->lchild = root;
		pre = pre_thrt;
		PreThread(root);
		pre->rchild = pre_thrt;
		pre->rtag = Thread;
		pre_thrt->rchild = pre;
	}
	return true;
}

void BinaryTree::PreThread(node * & root)
{
	if (root)
	{
		if (!root->lchild)
		{
			root->lchild = pre;
			root->ltag = Thread;
		}
		if (!pre->rchild)
		{
			pre->rchild = root;
			pre->rtag = Thread;
		}
		pre = root;
		if (root->ltag != Thread)
		{
			PreThread(root->lchild);
		}
		if (root->rtag != Thread)
		{
			PreThread(root->rchild);
		}
	}
}

bool BinaryTree::PostThreading(node * & root)
{
	post_thrt = (node *)malloc(sizeof(node));
	post_thrt->ltag = Link;
	post_thrt->rtag = Thread;
	post_thrt->rchild = post_thrt;
	if (!root)post_thrt->lchild = post_thrt;
	else
	{
		post_thrt->lchild = root;
		pre = post_thrt;
		PostThread(root);
		pre->rchild = post_thrt;
		pre->rtag = Thread;
		post_thrt->rchild = pre;
	}

	return true;
}

void BinaryTree::PostThread(node * & root)
{
	if (root)
	{
		PostThread(root->lchild);
		PostThread(root->rchild);
		if (!root->lchild)
		{
			root->lchild = pre;
			root->ltag = Thread;
		}
		if (!pre->rchild)
		{
			pre->rchild = root;
			pre->rtag = Thread;
		}
		pre = root;
	}
}

bool BinaryTree::In_Thrt()
{
	node * pointer = in_thrt->lchild;
	while (pointer != in_thrt)
	{
		while (pointer->ltag != Thread)
		{
			pointer = pointer->lchild;
		}
		cout << " " << pointer->data << " ";
		while (pointer->rtag == Thread && pointer != in_thrt)
		{
			pointer = pointer->rchild;
			if (pointer != in_thrt)
			{
				cout << " " << pointer->data << " ";
			}
		}
		if (pointer != in_thrt)
		{
			pointer = pointer->rchild;
		}
	}
	return true;
}

bool BinaryTree::Pre_Thrt()
{
	node * pointer = pre_thrt->lchild;
	while (pointer != pre_thrt)
	{
		while (pointer->ltag != Thread)
		{
			cout << " " << pointer->data << " ";
			pointer = pointer->lchild;
		}
		cout << " " << pointer->data << " ";
		while (pointer->rtag == Thread && pointer != pre_thrt)
		{
			pointer = pointer->rchild;
			if (pointer != pre_thrt)
			{
				cout << " " << pointer->data << " ";
			}
		}
		if (pointer != pre_thrt)
		{
			pointer = pointer->rchild;
		}
	}
	return true;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值