Node.h
/
#pragma once
#include<iostream>
using namespace std;
//节点要素:索引 数据 左孩子指针 右孩子指针, 父节点指针
class Node
{
public:
Node(void);
~Node(void);
//搜索节点 这个函数是最先需要实现的
Node* SerchNode(int nodeIdex);
//添加节点
bool AddNode(int nodeIndex, int direction, Node* pNode);
//删除节点,删掉此节点同时它的所有子节点都要删除
void DeleteNode();
//前序遍历
void PreOrderTravers();
//中序遍历
void InOrderTravers();
//后序遍历
void PostOrderTravers();
int index;
int data;
Node* pLchild;
Node* pRchild;
Node* pParent;
};
Node.cpp
#include "StdAfx.h"
#include "Node.h"
Node::Node(void)
{
index = 0;
data = 0;
pLchild = NULL;
pRchild = NULL;
pParent = NULL;
}
Node::~Node(void)
{
}
//没找到,就没在这个树上
Node * Node::SerchNode(int nodeIdex)
{
if (this->index == nodeIdex)
{
return this;
}
if (this->pLchild != NULL)
{
if (this->pLchild->index == nodeIdex)
{
return this->pLchild;
}
}
if (this->pRchild != NULL)
{
if (this->pRchild->index == nodeIdex)
{
return this->pRchild;
}
}
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::PreOrderTravers()
{
cout << this->data<<endl;
if (this->pLchild != NULL)
{
this->pLchild->PreOrderTravers();
}
if (this->pRchild != NULL)
{
this->pRchild->PreOrderTravers();
}
}
void Node::InOrderTravers()
{
if (this->pLchild != NULL)
{
this->pLchild->InOrderTravers();
}
cout << this->data << endl;
if (this->pRchild != NULL)
{
this->pRchild->InOrderTravers();
}
}
void Node::PostOrderTravers()
{
if (this->pLchild != NULL)
{
this->pLchild->PostOrderTravers();
}
if (this->pRchild != NULL)
{
this->pRchild->PostOrderTravers();
}
cout << this->data << endl;
}
//
Tree.h
#pragma once
#include "Node.h"
/*
链表实现二叉树
节点要素:索引 数据 左孩子指针 右孩子指针, 父节点指针
(0)
5(1) 8(2)
2(3) 6(4) 9(5) 7(6)
前序遍历:0 1 3 4 2 5 6
中序遍历:3 1 4 0 5 2 6
后续遍历:3 4 1 5 6 2 0
*/
class Tree
{
public:
Tree(void);
//销毁树, 需要销毁所有节点
~Tree(void);
//搜索节点 这个函数是最先需要实现的
Node* SerchNode(int nodeIdex);
//添加节点
bool AddNode(int nodeIndex, int direction, Node* pNode);
//删除节点,删掉此节点同时它的所有子节点都要删除
bool DeleteNode(int nodeIndex, Node* pNode);
//前序遍历
void PreOrderTravers();
//中序遍历
void InOrderTravers();
//后序遍历
void PostOrderTravers();
private:
Node* m_pRoot;
};
Tree.cpp
#include "StdAfx.h"
#include "Tree.h"
Tree::Tree(void)
{
m_pRoot = new Node();
}
Tree::~Tree(void)
{
}
Node * Tree::SerchNode(int nodeIdex)
{
return m_pRoot->SerchNode(nodeIdex);
}
bool Tree::AddNode(int nodeIndex, int direction, Node * pNode)
{
Node* temp = SerchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
else
{
Node* node = new Node();
if (node == NULL)
{
return false;
}
node->data = pNode->data;
node->index = pNode->index;
if (direction == 0)
{
temp->pLchild = node;
}
if (direction == 1)
{
temp->pRchild = node;
}
}
return true;
}
bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
Node* temp = SerchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
pNode->data = temp->data;
temp->DeleteNode();
return false;
}
void Tree::PreOrderTravers()
{
this->m_pRoot->PreOrderTravers();
}
void Tree::InOrderTravers()
{
this->m_pRoot->InOrderTravers();
}
void Tree::PostOrderTravers()
{
this->m_pRoot->PostOrderTravers();
}