树,主要内容:用数组和线性表分别实现二叉树,线性表二叉树中包含前序遍历,中序遍历,后续遍历。文中代码均已在VS2015上测试,空指针均为nullptr(C++11)。参考来源:慕课网
树
树是节点的有限集合。
节点的度:一个节点含有的子树的个数称为该节点的度;
叶节点或终端节点:度为0的节点称为叶节点;
非终端节点或分支节点:度不为0的节点;
双亲节点或父节点:若一个节点含有子节点,则这个节点称为其子节点的父节点;
孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点;
兄弟节点:具有相同父节点的节点互称为兄弟节点;
树的度:一棵树中,最大的节点的度称为树的度;
节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推;
树的高度或深度:树中节点的最大层次;
堂兄弟节点:双亲在同一层的节点互为堂兄弟;
节点的祖先:从根到该节点所经分支上的所有节点;
子孙:以某节点为根的子树中任一节点都称为该节点的子孙;
森林:由m(m>=0)棵互不相交的树的集合称为森林。
二叉树
所有节点的度都小于等于2
遍历:前序遍历(根左右)、中序遍历(左根右)、后序遍历(左右根)。
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
前序遍历:0,1,3,7,8,4,9,10,2,5,11,12,6,13,14,
中序遍历:7,3,8,1,9,4,10,0,11,5,12,2,13,6,14,
后序遍历:7,8,3,9,10,4,1,11,12,5,13,14,6,2,0,
二叉树实例
数组二叉树。
下面的数字为数组下标。
节点的左孩子下标是该节点下标*2+1。
节点的左孩子下标是该节点下标*2+2。
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
代码:
【Tree.h】
#ifndef TREE_H
#define TREE_H
【Tree.cpp】
#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree(int size,int *pRoot)
{
m_iSize = size;
m_pTree = new int[m_iSize];
for (int i = 0;i < size;i++)
{
m_pTree[i] = 0;
}
m_pTree[0] = *pRoot;
}
Tree::~Tree()
{
delete []m_pTree;
m_pTree = nullptr;
}
int * Tree::SearchNode(int nodeIndex)
{
if (nodeIndex<0||nodeIndex>=m_iSize)
{
return nullptr;
}
if (m_pTree[nodeIndex]==0)
{
return nullptr;
}
return &m_pTree[nodeIndex];
}
bool Tree::AddNode(int nodeIndex, int direction, int * pNode)
{
if (nodeIndex < 0 || nodeIndex >= m_iSize)
{
return nullptr;
}
if (m_pTree[nodeIndex]==0)
{
return false;
}
if (direction == 0)//向左节点插入 nodeIndex *2 + 1
{
if (/*nodeIndex * 2 + 1 < 0 ||*/ 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)//向右节点插入 nodeIndex *2 + 2
{
if (/*nodeIndex * 2 + 2 < 0 ||*/ nodeIndex * 2 + 2 >= m_iSize)
{
return false;
}
if (m_pTree[nodeIndex * 2 + 2] != 0)
{
return false;
}
m_pTree[nodeIndex * 2 + 2] = *pNode;
}
}
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] << " ";
}
}
【main.cpp】
#include <iostream>
#include "Tree.h"
using namespace std;
int main(void)
{
int root = 1;
Tree *pTree = new Tree(10, &root);
int node1 = 1;
int node2 = 2;
pTree->AddNode(0, 0, &node1);
pTree->AddNode(0, 1, &node2);
int node3 = 3;
int node4 = 4;
pTree->AddNode(1, 0, &node3);
pTree->AddNode(1, 1, &node4);
int node5 = 5;
int node6 = 6;
pTree->AddNode(2, 0, &node5);
pTree->AddNode(2, 1, &node6);
int node = 0;
pTree->DeleteNode(6, &node);
pTree->TreeTraverse();
int *p = pTree->SearchNode(2);
cout << *p << endl;
delete pTree;
pTree = nullptr;
return 0;
}
线性表二叉树
代码:
【Node.h】
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
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"
Node::Node()
{
index = 0;
data = 0;
pLChild = nullptr;
pRChild = nullptr;
pParent = nullptr;
}
Node* Node::SearchNode(int nodeIndex)
{
if (this->index==nodeIndex)
{
return this;
}
Node *temp = nullptr;
if (this->pLChild!=nullptr)
{
if (this->pLChild->index==nodeIndex)
{
return this->pLChild;
}
else
{
temp = this->pLChild->SearchNode(nodeIndex);
if (temp != nullptr)
{
return temp;
}
}
}
if (this->pRChild != nullptr)
{
if (this->pRChild->index == nodeIndex)
{
return this->pRChild;
}
else
{
temp = this->pRChild->SearchNode(nodeIndex);
if (temp != nullptr)
{
return temp;
}
}
}
return nullptr;
}
void Node::DeleteNode()
{
if (this->pLChild != nullptr)
{
this->pLChild->DeleteNode();
}
if (this->pRChild != nullptr)
{
this->pRChild->DeleteNode();
}
if (this->pParent != nullptr)
{
if (this->pParent->pLChild == this)
{
this->pParent->pLChild = nullptr;
}
if (this->pParent->pRChild == this)
{
this->pParent->pRChild = nullptr;
}
}
delete this;
}
void Node::PreorderTraversal()
{
cout << this->index << " " << this->data << endl;
if (this->pLChild != nullptr)
{
this->pLChild->PreorderTraversal();
}
if (this->pRChild != nullptr)
{
this->pRChild->PreorderTraversal();
}
}
void Node::InorderTraversal()
{
if (this->pLChild != nullptr)
{
this->pLChild->InorderTraversal();
}
cout << this->index << " " << this->data << endl;
if (this->pRChild != nullptr)
{
this->pRChild->InorderTraversal();
}
}
void Node::PostorderTraversal()
{
if (this->pLChild != nullptr)
{
this->pLChild->PostorderTraversal();
}
if (this->pRChild != nullptr)
{
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"
Tree::Tree()
{
m_pRoot = new Node();
m_pRoot->pLChild = nullptr;
m_pRoot->pRChild = nullptr;
m_pRoot->pParent = nullptr;
}
Tree::~Tree()
{
m_pRoot->DeleteNode();//或者DeleteNode(0,nullptr);
}
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==nullptr)
{
return false;
}
Node *node = new Node();
if (node==nullptr)
{
return false;
}
node->index = pNode->index;
node->data = pNode->data;
node->pParent = temp;
if (direction == 0)
{
temp->pLChild = node;
}
if (direction == 1)
{
temp->pRChild = node;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex, Node * pNode)
{
Node *temp = SearchNode(nodeIndex);
if (temp == nullptr)
{
return false;
}
if (pNode!=nullptr)
{
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();
}
【main.cpp】
#include "Tree.h"
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;
Tree *tree = new Tree();
tree->AddNode(0, 0, node1);
tree->AddNode(0, 1, node2);
tree->AddNode(1, 0, node3);
tree->AddNode(1, 1, node4);
tree->AddNode(2, 0, node5);
tree->AddNode(2, 1, node6);
tree->DeleteNode(5, nullptr);
tree->PreorderTraversal();
cout << "------------" << endl;
tree->InorderTraversal();
cout << "------------" << endl;
tree->PostorderTraversal();
return 0;
}