树:是有限结点组成的一个具有层次关系的集合。每个结点有0个或多个子结点,没有父结点的结点称为根结点。除了根结点之外的其余数据元素被分为M个互不相交的集合,其中每个集合本身也是一棵树,被称作原树的子树。单个结点也是一棵树。
结点的度:一个结点含有的子树的个数称为该结点的度;
叶结点或终端结点:度为0的结点称为叶结点;
非终端结点或分支结点:度不为0的结点;
双亲结点或父结点:若一个结点含有子结点,那么这个结点称为该子结点的父结点;
孩子结点或子结点:一个结点含有子树的根结点成为该结点的子结点;
兄弟结点:具有相同父结点的结点互称为兄弟结点;
树的度:一棵树中,最大的结点的度称为树的度;
结点的层次:从根开始定义起,根为第一层,根的子结点为第二层,依次类推;
树的高度或深度:树中结点的最大层次;
树的种类:
无序树:树中任意结点的子结点之间没有顺序关系,这种树称为无序树,也称为自由树;
有序树:树中任意结点的子结点之间有顺序关系,这种树称为有序树;
二叉树:每个结点最多含有两个子树的树称为二叉树;
.........................................................................................................
接下来介绍数组存储情况下和链表存储情况下的树的实现,包括树的创建和销毁,寻找树中的结点,添加结点,删除结点,以及树的三种遍历,前序遍历,中序遍历,后序遍历。
定义一个树的类文件,其中头文件为Tree.h
#pragma once
#include "Node.h"
#include <stdio.h>
#ifndef TREE_H
#define TREE_H
/*
二叉树的数组实现
*/
class Tree
{
public:
Tree(int size, int *pRoot);//创建树
~Tree();//销毁树
int *SearchNode(int nodeIndex);//根据索引寻找结点
bool AddNode(int nodeIndex, int direction, int *pNode);//添加结点
bool DeleteNode(int nodeIndex, int *pNode);//删除结点
void TreeTaverse();//遍历结点
private:
int *m_pTree;
int m_iSize;
};
#endif // !TREE_H
/******************************************************************************/
//二叉树的链表实现
/**
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;
结点要素: 索引 数据 左孩子指针 右孩子指针
前序遍历:0 1 3 4 2 5 6
中序遍历:3 1 4 0 5 2 6
后序遍历:3 4 1 5 6 2 0
(0)
5(1) 8(2)
2(3) 6(4) 9(5) 7(6)
*/
/******************************************************************************/
cpp文件为Tree.cpp
//
#include "stdafx.h"
#include "Tree.h"
#include <stdio.h>
#include <iostream>
using namespace std;
Tree::Tree(int size, int *pRoot)
{
m_iSize = size;
m_pTree = new int[size];
for (int i = 0; i < size; i++)
{
m_pTree[i] = 0;
}
m_pTree[0] = *pRoot;
}
/*************************************************************************/
//二叉树中的链表实现的构造函数
//Tree::Tree()
//{
// m_pRoot = new Node();
//}
/*************************************************************************/
Tree::~Tree()
{
delete[]m_pTree;
m_pTree = NULL;
}
/*************************************************************************/
Tree::~Tree()
{
DeleteNode(0, 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];
}
/*************************************************************************/
//链表实现二叉树寻找结点的方法
//Node *Tree::SearchNode(int nodeIndex)
//{
// return m_pRoot->SearchNode(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 < 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)
{
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;
}
return true;
}
/**************************************************************************/
//链表情况下在二叉树中添加结点
//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;
// node->pParent = temp;
// if (direction == 0)
// {
// temp->pLChild = node;
// }
// if (direction == 1)
// {
// temp->pRChild = node;
// }
//}
/**************************************************************************/
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;
}
/****************************************************************************/
//链表实现二叉树删除一个结点的操作
//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::TreeTaverse()
{
for (int i = 0; i < m_iSize; i++)
{
cout << m_pTree[i] << " ";
}
}
/****************************************************************************/
//链表实现二叉树的前序遍历
//void Tree::PreorderTreaversal()
//{
// m_pRoot->PreorderTreaversal();
//}
/***************************************************************************/
/***************************************************************************/
//链表实现二叉树的中序遍历
//void Tree::InorderTraversal()
//{
// m_pRoot->InorderTraversal();
//}
/***************************************************************************/
/***************************************************************************/
//链表实现二叉树的后序遍历
//void Tree::PostorderTraversal()
//{
// m_pRoot->PostorderTraversal();
//}
/***************************************************************************/
然后再定义一个结点类,它的头文件是Node.h
#pragma once
class Node
{
public:
Node();
~Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
public:
int index;
int data;
Node *pLChild;
Node *pRChild;
Node *pParent;
};
cpp文件是Node.cpp
#include "stdafx.h"
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
pParent = NULL;
}
Node::~Node()
{
}
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;
}
最后定义一个主程序的入口:demo.cpp
// Tree.cpp : 定义控制台应用程序的入口点。
//二叉树的数组实现
#include "stdafx.h"
#include "Tree.h"
#include <iostream>
using namespace std;
int main()
{
int root = 3;
Tree *pTree = new Tree(10, &root);
int node1 = 5;
int node2 = 8;
pTree->AddNode(0, 0, &node1);
pTree->AddNode(0, 1, &node2);
int node3 = 2;
int node4 = 6;
pTree->AddNode(1, 0, &node3);
pTree->AddNode(1, 1, &node4);
int node5 = 9;
int node6 = 7;
pTree->AddNode(2, 0, &node5);
pTree->AddNode(2, 1, &node6);
int node = 0;
pTree->DeleteNode(6, &node);
cout << "node=" << node << endl;
pTree->TreeTaverse();
int *p = pTree->SearchNode(2);
cout << endl << "node=" << *p << endl;
delete pTree;
return 0;
/************************************************************************/
//链表情况下二叉树的实现检测
/**
前序遍历:0 1 3 4 2 5 6
中序遍历:3 1 4 0 5 2 6
后序遍历:3 4 1 5 6 2 0
(0)
5(1) 8(2)
2(3) 6(4) 9(5) 7(6)
*/
/*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(6,NULL);
tree->PreorderTraversal();
tree->InorderTraversal();
tree->PostorderTraversal();
delete tree;*/
/************************************************************************/
}
备注:注释的部分是链表存储下的树的实现,测试时一定要分开写。