--Node的头文件:
#pragma once
#ifndef NODE_H_
#define NODE_H_
#include<iostream>
using namespace std;
class Node
{
public:
Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraverse();
void InorderTraverse();
void PostorderTraverse();
int index;
int data;
Node *pLChild;
Node *pRChild;
Node *pParent;
};
#endif // !NODE_H_
--Node的CPP文件:
#include "Node.h"
Node::Node()
{
m_idata = 0;
m_iIndex = 0;
m_pLChild = NULL;
m_pRChild = NULL;
m_pParent = NULL;
}
Node * Node::SearchNode(int index)
{
if (this->m_iIndex == index)
return this;
if (this->m_pLChild != NULL)
{
if (this->m_pLChild->m_iIndex==index)
{
return this->m_pLChild;
}
Node *temp = m_pLChild->SearchNode(index);
if (temp != NULL)
return temp;
}
if (this->m_pRChild != NULL)
{
if (this->m_pRChild->m_iIndex == index)
{
return this->m_pRChild;
}
Node *temp = m_pRChild->SearchNode(index);
if (temp != NULL)
return temp;
}
return NULL;
}
void Node::DeleteNode()
{
if (this->m_pLChild != NULL)
{
this->m_pLChild->DeleteNode();
}
if (this->m_pRChild != NULL)
{
this->m_pRChild->DeleteNode();
}
if (this->m_pParent != NULL)
{
if (this->m_pParent->m_pLChild == this)
this->m_pParent->m_pLChild = NULL;
if (this->m_pParent->m_pRChild == this)
this->m_pParent->m_pRChild = NULL;
}
delete this;
}
void Node::PreTraverse()
{
cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;
if (this->m_pLChild != NULL)
{
this->m_pLChild->PreTraverse();
}
if (this->m_pRChild != NULL)
{
this->m_pRChild->PreTraverse();
}
}
void Node::InTraverse()
{
if (this->m_pLChild != NULL)
{
this->m_pLChild->PreTraverse();
}
cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;
if (this->m_pRChild != NULL)
{
this->m_pRChild->PreTraverse();
}
}
void Node::ProTraverse()
{
if (this->m_pLChild != NULL)
{
this->m_pLChild->PreTraverse();
}
if (this->m_pRChild != NULL)
{
this->m_pRChild->PreTraverse();
}
cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;
}
--Tree的头文件:
#pragma once
#ifndef TREE_H_
#define TREE_H_
#include"Node.h"
#include<stdlib.h>
using namespace std;
class Tree
{
public:
Tree();
~Tree();
Node* SearchNode(int index);
bool AddNode(int index, bool direction, Node *pNode);
bool DeleteNode(int index, Node *pNode);
void PreTraverse();
void InTraverse();
void ProTraverse();
private:
Node* m_pRoot;
};
#endif // !TREE_H_
--Tree的CPP文件:
#include "Tree.h"
Tree::Tree()
{
m_pRoot = new Node;
}
Tree::~Tree()
{
DeleteNode(0, NULL);
}
Node * Tree::SearchNode(int index)
{
return m_pRoot->SearchNode(index);
}
bool Tree::AddNode(int index, bool direction, Node * pNode)
{
Node *temp = SearchNode(index);
if (temp == NULL)
return false;
Node *node = new Node;
node->m_idata = pNode->m_idata;
node->m_iIndex = pNode->m_iIndex;
node->m_pParent = temp;
if (direction == true)
{
if (temp->m_pLChild != NULL)
return false;
temp->m_pLChild = node;
}
else
{
if (temp->m_pRChild != NULL)
return false;
temp->m_pRChild = node;
}
return true;
}
bool Tree::DeleteNode(int index, Node * pNode)
{
Node *temp = SearchNode(index);
if (temp == NULL)
{
return false;
}
if (pNode != NULL)
{
pNode->m_idata = temp->m_idata;
}
temp->DeleteNode();
return true;
}
void Tree::PreTraverse()
{
m_pRoot->PreTraverse();
}
void Tree::InTraverse()
{
m_pRoot->InTraverse();
}
void Tree::ProTraverse()
{
m_pRoot->ProTraverse();
}