树--二叉树的链表实现
Tree.cpp
Node.h
Node.cpp
demo.cpp
//控制台运行结果(前序遍历):
索引:0 数据:0
索引:1 数据:3
索引:3 数据:7
索引:4 数据:9
索引:2 数据:5
删除索引为3处的节点数据为:7
再次遍历:
索引:0 数据:0
索引:1 数据:3
索引:4 数据:9
二叉树的链表实现:
Tree.h
#ifndef TREE_H
#define TREE_H
#include"Node.h"
//-----------------------------------
//-----二叉树的数组表示--------------
class Tree{
public:
Tree();//创建树
~Tree();//销毁
Node *searchNode(int nodeIndex);//搜索节点
bool addNode(int nodeIndex, int direction, Node *pNode);//增加节点,direction为0是左节点,为1是右节点
bool deleteNode(int nodeIndex, Node *pNode);//根据索引删除节点,并拿出来给*pNode
void preorderTraverse();//前序遍历
void inorderTraverse();//中序遍历
void postorderTraverse();//后序遍历
private:
Node *m_pRoot;//节点指针
//int m_iSize;//大小
};
#endif
Tree.cpp
#include"Tree.h"
#include<iostream>
#include<stdio.h>
using namespace std;
Tree::Tree(){
m_pRoot = new Node();
}
Tree::~Tree(){
m_pRoot->deleteNode();//deleteNode(0, NULL)
}
Node *Tree::searchNode(int nodeIndex){
return m_pRoot->searchNode(nodeIndex);
}
bool Tree::addNode(int nodeIndex, int direction, Node *pNode){
Node *t = searchNode(nodeIndex);
if(NULL == t){
return false;
}
Node *tNode = new Node();//新生节点
if(NULL == tNode){
return false;
}
tNode->data = pNode->data;//数据复制给新生节点
tNode->index = pNode->index;
tNode->pParent = t;
//--------------------------------------
if(0 == direction){//左节点
if(NULL != t->pLChild){
return false;
}
t->pLChild = tNode;
return true;
}
//---------------------------------------
if(1 == direction){//右节点
if(NULL != t->pRChild){
return false;
}
t->pRChild = tNode;
return true;
}
return false;
}
bool Tree::deleteNode(int nodeIndex, Node *pNode){
Node *t = searchNode(nodeIndex);
if(NULL == t){
return false;
}
if(pNode != NULL){
pNode->data = t->data;
}
t->deleteNode();
return true;
}
void Tree::preorderTraverse(){
m_pRoot->preorderTraverse();
}
void Tree::inorderTraverse(){
m_pRoot->inorderTraverse();
}
void Tree::postorderTraverse(){
m_pRoot->postorderTraverse();
}
Node.h
#ifndef NODE_H
#define NODE_H
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.cpp
#include"Node.h"
#include<iostream>
#include<stdio.h>
using namespace std;
Node::Node(){
int index = 0;
int data = 0;
Node *pLChild = NULL;
Node *pRChild = NULL;
Node *pParent = NULL;
}
Node *Node::searchNode(int nodeIndex){
if(this->index == nodeIndex){//判断当前结点是否符合
return this;
}
Node *t = NULL;
if(this->pLChild != NULL){
if(this->pLChild->index == nodeIndex){//判断当前结点是否符合
return this->pLChild;//太奇怪了,调试时,符合条件后返回没有成功,反而跳转到else里面才返回成功,这是为什么??Dev-C++上不行,vs 2015可以,又是为什么?
}
else{
t = this->pLChild->searchNode(nodeIndex);
if(t != NULL){
return t;//这里才返回成功
}
}
}
if(this->pRChild != NULL){
if(this->pRChild->index == nodeIndex){//判断当前结点是否符合
return this->pRChild;
}
else{
t = this->pRChild->searchNode(nodeIndex);
if(t != NULL){
return t;
}
}
}
//return NULL;
}
void Node::deleteNode(){
if(this->pLChild != NULL){
this->pLChild->deleteNode();
}
if(this->pRChild != NULL){
this->pRChild->deleteNode();
}
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 << "索引:" << index << " 数据:" << data << endl;
if(this->pLChild != NULL){
this->pLChild->preorderTraverse();
}
if(this->pRChild != NULL){
this->pRChild->preorderTraverse();
}
}
void Node::inorderTraverse(){
if(this->pLChild != NULL){
this->pLChild->inorderTraverse();
}
cout << "索引:" << index << " 数据:" << data << endl;
if(this->pRChild != NULL){
this->pRChild->inorderTraverse();
}
}
void Node::postorderTraverse(){
if(this->pLChild != NULL){
this->pLChild->postorderTraverse();
}
if(this->pRChild != NULL){
this->pRChild->postorderTraverse();
}
cout << "索引:" << index << " 数据:" << data << endl;
}
demo.cpp
#include"Tree.h"
#include<iostream>
#include<stdio.h>
using namespace std;
//-------------------
int main(){
//测试开始
Node *node = new Node[4];
node[0].index = 1;//索引
node[0].data = 3;
node[1].index = 2;//索引
node[1].data = 5;
node[2].index = 3;//索引
node[2].data = 7;
node[3].index = 4;//索引
node[3].data = 9;
Tree *tree = new Tree();
tree->addNode(0, 0, &node[0]);
tree->addNode(0, 1, &node[1]);
tree->addNode(1, 0, &node[2]);
tree->addNode(1, 1, &node[3]);
tree->preorderTraverse();
Node t;
tree->deleteNode(3, &t);
cout << "\n删除索引为3处的节点数据为:" << t.data <<endl;
cout << "再次遍历:" << endl;
tree->preorderTraverse();
delete tree;
tree = NULL;
system("pause");
return 0;
}
//控制台运行结果(前序遍历):
索引:0 数据:0
索引:1 数据:3
索引:3 数据:7
索引:4 数据:9
索引:2 数据:5
删除索引为3处的节点数据为:7
再次遍历:
索引:0 数据:0
索引:1 数据:3
索引:4 数据:9
索引:2 数据:5
//---------------------------------------------------------------
//-------------------------------------------------------------------------