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;
}