@这篇文档是由C++代码实现的二叉树
二叉树就是每个结点至多只有两棵子树(即二叉树中不存在度大于2的结点),并且二叉树的子树有左右之分,其次序不能任意颠倒。
二叉树的实现可以采用数组或者链表,数组实现比较简单,本文是以链表形式实现的,其中每个结点内容包括父结点指针,左孩子指针,右孩子指针,位序,数据域。
本文是以Visual Studio中新建的C++win32的控制台应用程序实现的,其中建立了头文件和源文件以及实现main()的demo文件,分别是Tree.h和Tree.cpp和Node.h和Node.cpp和demo.cpp文件。
其中Tree.h定义了二叉树的这种数据结构,包括它其中要用到的一些成员函数,例如添加结点,删除结点,搜索结点,以及前序遍历、中序遍历和后序遍历。前中后都是相对根结点来说的,例如前序遍历就是根左右,中序就是左根右,后序遍历就是左右根。而Node.h中定义了一个结点中的指针指向的结点所要包含的东西。
Node.h文件
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
int index;
int data;
Node *pLChild;
Node *pRChild;
Node *pParent;
};
#endif
Node.cpp文件
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
pParent = 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);
if (temp != NULL)
{
return temp;
}
}
}
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::PreorderTraversal()
{
cout << this->index << ":" << this->data << endl;
if (this->pLChild != NULL)
{
this->pLChild->PreorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PreorderTraversal();
}
}
void Node::InorderTraversal()
{
if (this->pLChild != NULL)
{
this->pLChild->InorderTraversal();
}
cout << this->index << ":" << this->data << endl;
if (this->pRChild != NULL)
{
this->pRChild->InorderTraversal();
}
}
void Node::PostorderTraversal()
{
if (this->pLChild != NULL)
{
this->pLChild->PostorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PostorderTraversal();
}
cout << this->index << ":" << this->data << endl;
}
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);
bool DeleteNode(int nodeIndex, Node *pNode);
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
private:
Node *m_pRoot;
};
#endif
Tree.cpp文件
#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree()
{
m_pRoot = new Node();
}
Tree::~Tree()
{
m_pRoot->DeleteNode();
}
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 *newNode = new Node();
if (newNode == NULL)
{
return false;
}
newNode->index = pNode->index;
newNode->data = pNode->data;
newNode->pParent = temp;
if (direction == 0)
{
temp->pLChild = newNode;
}
if (direction == 1)
{
temp->pRChild = newNode;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
Node *temp = SearchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
if (pNode != NULL)
{
pNode->data = temp->data;
}
temp->DeleteNode();
return true;
}
void Tree::PreorderTraversal()
{
m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
m_pRoot->PostorderTraversal();
}
demo.cpp文件
#include "Tree.h"
#include "Node.h"
#include <iostream>
using namespace std;
int main()
{
Tree *tree = new Tree();
Node node1;
node1.index = 1;
node1.data = 5;
tree->AddNode(0,0,&node1);
Node node2;
node2.index = 2;
node2.data = 8;
tree->AddNode(0,1,&node2);
Node node3;
node3.index = 3;
node3.data = 2;
tree->AddNode(1,0,&node3);
Node node4;
node4.index = 4;
node4.data = 6;
tree->AddNode(1,1,&node4);
Node node5;
node5.index = 5;
node5.data = 9;
tree->AddNode(2,0,&node5);
Node node6;
node6.index = 6;
node6.data = 7;
tree->AddNode(2,1,&node6);
cout << "前序遍历结果为:" << endl;
tree->PreorderTraversal();
cout << "中序遍历结果为:" << endl;
tree->InorderTraversal();
cout << "后序遍历结果为:" << endl;
tree->PostorderTraversal();
tree->DeleteNode(2,NULL);
cout << "前序遍历结果为:" << endl;
tree->PreorderTraversal();
delete tree;
tree = NULL;
system("pause");
return 0;
}