一、树的基本概念
树是结点的有限集合
关于二叉树:
- 所有结点的度都小于等于2;
- 二叉树遍历:前序遍历、中序遍历、后序遍历
树的应用:
- 压缩软件-赫夫曼树
- 人机对战
二、二叉树数组实现
#ifndef TREE_H
#define TREE_H
#include<iostream>
using namespace std;
class Tree{
public:
Tree(int size);//构建二叉树
~Tree();//销毁树
int *SearchNode(int nodeIndex);
bool AddNode(int nodeIndex,int direction, int *pNode);//添加结点
bool DeleteNode(int nodeIndex,int *pNode);//删除结点
void TreeTraverse();//遍历树
private:
int *m_pTree;
int m_iSize;
};
Tree::Tree(int size) {
m_pTree = new int[size];
m_iSize = size;
for(int i = 0; i < size; i++) {
m_pTree[i] = 0;
}
}
Tree::~Tree() {
delete []m_pTree;
m_pTree = NULL;
}
int *Tree::SearchNode(int nodeIndex) {
if(nodeIndex < 0 || nodeIndex >= m_iSize) {
return NULL;
}
if(m_pTree[nodeIndex] == 0) {
return NULL;
}
return &m_pTree[nodeIndex];
}
bool Tree::AddNode(int nodeIndex,int direction, int *pNode) {
if(nodeIndex < 0 || nodeIndex >= m_iSize) {
return false;
}
if(m_pTree[nodeIndex] == 0) {
return false;
}
if(direction == 0) {
if(nodeIndex*2+1 >= m_iSize) {
return false;
}
if(m_pTree[nodeIndex*2+1] == 0) {
return false;
}
m_pTree[nodeIndex * 2 + 1] = *pNode;
}
if(direction == 1) {
if(nodeIndex*2+2 >= m_iSize) {
return false;
}
if(m_pTree[nodeIndex*2+2] == 0) {
return false;
}
m_pTree[nodeIndex * 2 + 2] = *pNode;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex,int *pNode) {
if(nodeIndex < 0 || nodeIndex >= m_iSize) {
return false;
}
if(m_pTree[nodeIndex] == 0) {
return false;
}
*pNode = m_pTree[nodeIndex];
m_pTree[nodeIndex] = 0;
return true;
}
void Tree::TreeTraverse() {
for(int i = 0; i < m_iSize; i++) {
cout << m_pTree[i] << endl;
}
}
#endif
三、二叉树链表实现
node.h
#ifndef NODE_H
#define NODE_H
#include<iostream>
using namespace std;
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;
};
Node::Node() {
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
pParent = NULL;
}
Node *Node::SearchNode(int nodeIndex) {
if(this->index == nodeIndex) {
return this;
}
if(this->pLChild != NULL) {
if(this->pLChild->index == nodeIndex) {
return this->pLChild;
}
}
if(this->pRChild != NULL) {
if(this->pRChild->index == nodeIndex) {
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::PreorderTraverse() {
cout << this->index << " " << this->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 << this->index << " " << this->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 << this->index << " " << this->data << endl;
}
#endif
Tree.h
#ifndef TREE_H
#define TREE_H
#include<iostream>
#include "Node.h"
using namespace std;
class Tree{
public:
Tree(int size);
~Tree();
Node *SearchNode(int nodeIndex);
bool AddNode(int nodeIndex,int direction, Node *pNode);
bool DeleteNode(int nodeIndex,Node *pNode);
void PreorderTraverse();
void InorderTraverse();
void PostorderTraverse();
private:
Node *m_pRoot;
};
Tree::Tree(int size) {
m_pRoot = new Node();
}
Tree::~Tree() {
DeleteNode(0,NULL);
}
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 == NULL) {
return false;
}
Node *node = new Node();
if(node == NULL) {
return false;
}
node->index = pNode->index;
node->data = pNode->data;
if(direction == 0) {
temp->pLChild = node;
}
if(direction == 1) {
temp->pRChild = node;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex,int *pNode) {
Node *temp = SearchNode(nodeIndex);
if(temp == NULL) {
return false;
}
if(pNode == NULL) {
return false;
}
if(pNode != NULL) {
pNode->data = temp->data;
}
temp->DeleteNode();
return true;
}
void Tree::PreorderTraverse() {
m_pRoot->PreorderTraverse();
}
void Tree::InorderTraverse() {
m_pRoot->InorderTraverse();
}
void Tree::PostorderTraverse() {
m_pRoot->PostorderTraverse();
}
#endif