使用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实现二叉树的定义
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;
}
#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;
}
相关文章推荐
- 使用C++数组实现二叉树的存储和基本操作
- 使用C++实现链表的基本操作
- bo3-4.c 用单链表的基本操作实现链队列(存储结构由c3-2.h定义)的基本操作(9个)
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- 链表的基本操作函数算法(C/C++实现)
- C++实现链表基本操作
- 二叉树的基本操作的c++实现
- 二叉树基本操作实现(二叉树的顺序存储)
- C++实现链表的基本操作及测试用例
- C++实现二叉树的基本操作(递归+非递归)
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- C/C++基本数据结构:链表的基本使用和实现
- c++实现链表的基本操作
- C++实现二叉树的基本操作
- 二叉树的先序、中序、后序遍历等基本操作c++实现
- 【代码】C++实现二叉树基本操作及测试用例
- 二叉树(二叉链式存储)8种基本操作的实现
- c++学习笔记—二叉树基本操作的实现
- 使用C++结合文件操作和链表实现学生成绩管理系统
- C++ 实现链表的基本操作之一:链表插入