您的位置:首页 > 编程语言 > C语言/C++

使用C++链表来实现二叉树的存储和基本操作

2016-10-07 23:07 771 查看
1、创建Node.h 定义链表的节点

#ifndef NODE_H
#define NODE_H
class Node{
public:
Node();
Node* searchNode(int nodeIndex);
void delNode();
void preOrderTraverse();
void midOrderTraverse();
void postOrderTraverse();

int index;
int data;
Node* pLChild;
Node* pRChild;
Node* pParent;
};
#endif // NODE_H<strong>
</strong>
2、创建Node.cpp 实现节点的定义
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node(){
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
}
Node* Node::searchNode(int nodeIndex){
if(this->index == nodeIndex)
return this;
Node *temp = NULL;
if(this->pLChild != NULL){
if(this->pLChild->index == nodeIndex)
return this->pLChild;
else{
temp = this->pLChild->searchNode(nodeIndex);
if(temp != NULL){ /**在左孩子中找到了要找的节点返回,没找到不返回,继续寻找右孩子**/
return temp;
}

}
}
if(this->pRChild != NULL){
if(this->pRChild->index == nodeIndex)
return this->pRChild;
else{
temp = this->pRChild->searchNode(nodeIndex);
return temp;
}
}
return NULL;
}

void Node::delNode(){
if(this->pLChild != NULL)
this->pLChild->delNode();
if(this->pRChild != NULL)
this->pRChild->delNode();
/****/
if(this->pParent != NULL){
if(this->pParent->pLChild == this)
this->pParent->pLChild = NULL;
if(this->pParent->pRChild == this)
this->pParent->pRChild = NULL;
}
delete this;
}
void Node::preOrderTraverse(){
cout<<this->index<<" "<<this->data<<endl;

if(this->pLChild != NULL)
this->pLChild->preOrderTraverse();

if(this->pRChild != NULL)
this->pRChild->preOrderTraverse();
}
void Node::midOrderTraverse(){

if(this->pLChild != NULL)
this->pLChild->midOrderTraverse();

cout<<this->index<<" "<<this->data<<endl;

if(this->pRChild != NULL)
this->pRChild->midOrderTraverse();
}
void Node::postOrderTraverse(){

if(this->pLChild != NULL)
this->pLChild->postOrderTraverse();

if(this->pRChild != NULL)
this->pRChild->postOrderTraverse();

cout<<this->index<<" "<<this->data<<endl;
}<strong>
</strong>

3、创建文件ListTree.h 定义二叉树的数据结构
#ifndef LISTTREE_H
#define LISTTREE_H
#include "Node.h"
class ListTree{
public:
ListTree();
~ListTree();
Node* searchNode(int nodeIndex);
bool addNode(int nodeIndex,int direction,Node *pNode); //direction=0代表插入到左节点,direction=1代表插入到右节点
bool delNode(int nodeIndex,Node *pNode);
void preOrderTraverse();
void midOrderTraverse();
void postOrderTraverse();
private:
Node *m_pRoot;
};
#endif // LISTTREE_H

4、创建文件ListTree.cpp实现二叉树的定义
#include "ListTree.h"
#include <iostream>  /**使用NULL要引入iostream或者stdio.h头文件**/
using namespace std;
ListTree::ListTree(){
m_pRoot = new Node;
}
Node* ListTree::searchNode(int nodeIndex){
return m_pRoot->searchNode(nodeIndex);
}
ListTree::~ListTree(){
m_pRoot->delNode();
}
bool ListTree::addNode(int nodeIndex,int direction,Node *pNode){
Node* temp = searchNode(nodeIndex);
if(temp == NULL)
return false;
Node* node = new Node();
node->index = pNode->index;
node->data = pNode->data;
node->pParent = temp;
if(node == NULL)
return false;
if(direction == 0)
temp->pLChild = node;
if(direction == 1)
temp->pRChild = node;

return true;
}
bool ListTree::delNode(int nodeIndex,Node *pNode){
Node* temp = searchNode(nodeIndex);
if(temp == NULL)
return false;
if(pNode != NULL)
pNode->data = temp->data;
temp->delNode();
return true;
}
void ListTree::preOrderTraverse(){
m_pRoot->preOrderTraverse();
cout<<"---------------------------"<<endl;
}
void ListTree::midOrderTraverse(){
m_pRoot->midOrderTraverse();
cout<<"---------------------------"<<endl;
}
void ListTree::postOrderTraverse(){
m_pRoot->postOrderTraverse();
cout<<"---------------------------"<<endl;
}

5、创建文件main.cpp 测试前面实现的操作
#include <iostream>
#include "ListTree.h"
using namespace std;
/**
3[0]

5[1] 8[2]
2[3] 6[4] 9[5] 7[6]
**/
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();
node6->index = 6;
node6->data = 7;

ListTree *listTree = new ListTree();
listTree->addNode(0,0,node1);
listTree->addNode(0,1,node2);
listTree->addNode(1,0,node3);
listTree->addNode(1,1,node4);
listTree->addNode(2,0,node5);
listTree->addNode(2,1,node6);

listTree->preOrderTraverse();
listTree->midOrderTraverse();
listTree->postOrderTraverse();
//listTree->delNode(6,NULL);
listTree->delNode(2,NULL);

listTree->preOrderTraverse();
delete listTree;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: