資料結構之樹的操作
阿新 • • 發佈:2018-12-07
話不多少 q:690217293 歡迎交流
//_____________________________demo.cpp
//=================================Node.h#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; }
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
//________________________________________Tree.h#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; }
#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();
}