话不多少 q:690217293 欢迎交流
//_____________________________demo.cpp
#include <cstdio>
#include "Tree.h"
#include <iostream>
using namespace std;
int main()
{
Node *node1 =new Node();
node1->index=1;
node1->data=5;
Node *node2=new Node();
node2->index=2;
node2->data=8;
Node *node3=new Node();
node3->index=3;
node3->data=2;
Node *node4=new Node();
node4->index=4;
node4->data=6;
Node *node5=new Node();
node5->index=5;
node5->data=9;
Node *node6=new Node();
node5->index=6;
node5->data=7;
Tree *tree=new Tree();
tree->AddNode(0,0,node1);
tree->AddNode(0,1,node2);
tree->AddNode(1,0,node3);
tree->AddNode(1,1,node4);
tree->AddNode(2,0,node5);
tree->AddNode(2,1,node6);
tree->PreorderTraversal();
delete tree;
}
//=================================Node.h
class Node
{
public :
Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
int index;//节点下标
int data;//节点的值
Node *pLChild;
Node *pRChild;
Node *pParent;
};
//______________________________________Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
index=0;
data=0;
pLChild=0;
pRChild=0;
pParent=0;
}
Node * Node::SearchNode(int nodeIndex)
{
if(this->index==nodeIndex)
{
return this;
}
Node *temp=0;
if(this->pLChild!=0)
{
if(this->pLChild->index==nodeIndex)
{
return this->pLChild;
}
else
{
temp=this->pLChild->SearchNode(nodeIndex);//更换节点进行递归搜索,this指针指向变换
if(temp!=0)
{
return temp;
}
}
}
if(this->pRChild!=0)
{
if(this->pRChild->index==nodeIndex)
return this->pRChild;
else
{
temp=this->pRChild->SearchNode(nodeIndex);
if(temp!=0)
return temp;
}
}
return 0;
}
void Node::DeleteNode()//递归操作,反复调用,删除节点,跟树无关,单纯节点操作,一直到最先面的节点
{
if(this->pLChild!=0)
{
this->pLChild->DeleteNode();
}
if(this->pRChild!=0)
{
this->pRChild->DeleteNode();
}
if(this->pParent!=0)
{
if(this->pParent->pLChild==this)
{
this->pParent->pLChild=0;
}
if(this->pParent->pRChild==this)
{
this->pParent->pRChild=0;
}
}
delete this;
}
void Node::PreorderTraversal()//前序遍历
{
cout<<this->index<<" "<<this ->data<<endl;
if(this->pLChild!=0)
{
this->pLChild->PreorderTraversal();
}
if(this->pRChild!=0)
{
this->pRChild->PreorderTraversal();
}
}
void Node::InorderTraversal()//中序遍历
{
if(this->pLChild!=0)
{
this->pLChild->InorderTraversal();
}
cout<<this->index<<" "<<this->data<<endl;
if(this->pRChild!=0)
{
this->pRChild->InorderTraversal();
}
}
void Node::PostorderTraversal()//后续遍历
{
if(this->pLChild!=0)
{
this->pLChild->PostorderTraversal();
}
if(this->pRChild!=0)
{
this->pRChild->PostorderTraversal();
}
cout<<this->index<<" "<<this->data<<endl;
}
//________________________________________Tree.h
#include "Node.h"
class Tree
{
public:
Tree();
~Tree();
Node *SearchNode(int nodeIndex);
bool AddNode(int nodeIndex,int direction,Node *pNode);
bool DeleteNode(int nodeIndex,Node *pNode);
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
private:
Node * m_pRoot;
};
//=====================================Tree.cpp
#include "Tree.h"
Tree::Tree()
{
m_pRoot=new Node();
}
Tree::~Tree()
{
//DeleteNode(0,0);//两种方法
m_pRoot->DeleteNode();
}
Node *Tree::SearchNode(int nodeIndex)
{
return m_pRoot->SearchNode(nodeIndex);
}
bool Tree::AddNode(int nodeIndex,int direction,Node *pNode)
{
Node *temp=SearchNode(nodeIndex);
if(temp==0)
return false;
Node *node=new Node();
if(node==0)
return false;
node->index=pNode->index;
node->data=pNode->data;
node->pParent=temp;
if(direction==0)
{
temp->pLChild=node;
}
if(direction==1)
{
temp->pRChild=node;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex,Node *pNode)
{
Node *temp=SearchNode(nodeIndex);
if(temp==0)
return false;
if(pNode!=0)//删除 顶点的时候
{
pNode->data=temp->data;
}
temp->DeleteNode();//调用Node里面的删除函数
return true;
}
void Tree::PreorderTraversal()
{
m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
m_pRoot->PostorderTraversal();
}