【数据结构与算法004】基本数据结构——树

本文介绍二叉树的基本概念,并通过数组和链表两种方式实现二叉树,包括创建、搜索、添加、删除节点等操作,以及前序、中序、后序遍历。

一、基本概念


树:树是节点的有限集合。

孩子:对于B    E,F肯定是孩子,对于D    G和H是孩子。

双亲:最顶端根节点A肯定是双亲。

度:A节点度为3,B节点的度为2,C节点度为0。

叶子:对于一棵树终端节点就是叶子。如E,F,G和H.

根:对于一棵树非终端节点就是根。如A,B和D.

有序树:有序树和无序树是相对的概念,B节点有两个孩子E和F,如果E和F不可以换顺序则为有序树。

无序树:有序树和无序树是相对的概念,B节点有两个孩子E和F,如果E和F可以换顺序则为无序树。

祖先:指定当前节点向上的一直到总的根节点之前所路过的所有节点。

子孙:从该节点开始的向下伸出的节点以及伸出节点的子节点。举例:对于A节点下面所有的节点都为其子孙,对于D节点G,H为其子孙。

节点深度:节点深度与所在的层是统一的,如A深度为1,B,C,D深度为2,E,F,G,H深度为3

树的深度:上图树的深度为3

森林:多颗独立的树组成森林。

二叉树:所有节点的度都小于等于2

二叉树的遍历:前序遍历(根左右)、中序遍历(左根右)、后续遍历(左右根)。

用途:压缩软件 赫夫曼编码 人机对战(不断进行树的搜索找到最佳的对战方法让机器做出最佳的当前选择)

二、数组表示二叉树

我们使用数组表示二叉树,共包含Tree.h,Tree.cpp和demo.cpp

Tree.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 TreeTraverse(); //遍历节点

private:
	int *m_pTree;
	int m_iSize;
};

#endif
Tree.cpp

#include <iostream>
#include "Tree.h"
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(){

	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] << " ";
	}
	cout << endl;
}

demo.cpp

/************************************************************************/
/* 二叉树(数组表示)
	完成数的基本操作
	1.树的创建和销毁
	2.树中节点的搜索
	3.树中节点的添加与删除
	4.树中节点的遍历

	BOOL CreateTree(Tree **pTree,Node *pRoot); //创建树									
	void DestoryTree(Tree *pTree); //销毁树
	Node *SearchNode(Tree *pTree,int nodeIndex); //根据索引寻找节点
	BOOL AddNode(Tree *pTree,int nodeIndex,int direction,Node *pNode); //添加节点
	BOOL DeleteNode(Tree *pTree,int nodeIndex,Node *pNode); //删除节点
	void TreeTraverse(Tree *pTree); //遍历

	关于数组与树之间的算法转换

	int tree[n] 3    5 8    2 6 9 7  父亲节点下标*2+1 该节点左
									
									父亲节点下标*2+2 该节点右

				3(0)

		 5(1)	       8(2)

	2(3)	6(4)	 9(5)	 7(6)

*/
/************************************************************************/
#include <iostream>
#include <stdlib.h>
#include "Tree.h"

using namespace std;

int main(void){

	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->TreeTraverse();

	int *p = pTree->SearchNode(2);
	cout << "node=" << *p << endl;

	delete pTree;
	system("pause");
	return 0;
}

三、链表表示二叉树

Tree.h

/*
	二叉树:链表实现

	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(); //后序遍历


	节点要素:索引 数据 左孩子指针 右孩子指针 父节点指针

	前序遍历: 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)
*/
#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();

}

Tree::~Tree(){

	//DeleteNode(0, NULL);
	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 *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; 
	}

	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(); 
}


void Tree::PreorderTraversal(){

	m_pRoot->PreorderTraversal();
}

void Tree::InorderTraversal(){

	m_pRoot->InorderTraversal();
}

void Tree::PostorderTraversal(){

	m_pRoot->PostorderTraversal();

}
Node.h

#ifndef NODE_H
#define NODE_H

#include <stdio.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;

}
demo.cpp

#include <stdlib.h>
#include "Tree.h"

/*
	前序遍历: 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)
*/
int main(void){


	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->DeleteNode(5, NULL);

	tree->DeleteNode(2, NULL);
	//tree->PreorderTraversal();
	//tree->InorderTraversal();
	tree->PostorderTraversal();
	delete tree;
	system("pause");
	return 0;
}
一点总结:遇到编写二叉树各个函数功能时多考虑用递归的思路实现即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaveBobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值