数据结构探险——树篇(代码实现)

imooc相关学习视频.

1、什么是树?

树是节点的有限集合
树中相关的概念
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、什么是二叉树?

定义:所有结点的度都小于等于2

二叉树的遍历分为三种,这三种遍历方式是相对于根节点来说的
1)前序遍历—>遍历顺序是:根–左--右
2)中序遍历—>遍历顺序是:左–根--右
3)后序遍历—>遍历顺序是:左–右--根
在这里插入图片描述

3、树的用途

1)压缩软件 --赫夫曼树
2)搜索 --人机对战

4、二叉树数组实现编码

4.1 相关说明

关于数组与树之间的算法转换
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)

二叉树(数组表示)
完成树的基本操作;
1)树的创建和销毁
2)树中节点的搜索
3)树中节点的添加与删除
4)树中节点的遍历
C语音表示:
BOOL CreateTree(Tree **pTree, Node *pRoot); //创建树
void DestroyTree(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); // 遍历

4.2 编码实现

第一个测试代码
Tree.h–>定义二叉树相关功能接口和元素的头文件(存入的数据都是int类型)

#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_H_ */

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 (0 == m_pTree[nodeIndex]) {
		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 (0 == 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 (1 == 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 (0 == m_pTree[nodeIndex]) {
		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;
}

TreeDemo.cpp–>测试入口代码文件

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


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

	int node = 0;
	pTree->DeleteNode(5, &node);
	cout << "node=" << node << endl;
	pTree->TreeTraverse();

	delete pTree;
	pTree = NULL;
	return 0;
}

测试结果为:

3 5 8 2 6 9 7 0 0 0 
temp=8
node=9
3 5 8 2 6 0 7 0 0 0 

5、二叉树链表实现编码

5.1 相关说明

二叉树:链表实现
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)
 5(1)      8(2)
2(3) 6(4) 9(5) 7(6)

上面树进行遍历:
前序遍历:0 1 3 4 2 5 6
中序遍历:3 1 4 0 5 2 6
后序遍历:3 4 1 5 6 2 0

5.2 编码实现

第二个测试代码
Node.h–>定义结点相关功能接口和元素的头文件(存入的数据都是Node类型,包含一个整型的index索引,一个int的data数据)

#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_H_ */

Node.cpp–>实现树中节点相关功能代码

#include <iostream>
#include "Node.h"
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) { //查找index为该结点的左孩子
			return this->pLChild;
		} else { //没查找到index继续在找到该结点的左孩子下面继续查找
			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–>修改后的定义树中相关功能接口和元素的头文件(存入的数据都是int类型)

#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_H_ */

Tree.cpp–>修改后的实现树的相关功能代码

#include <iostream>
#include "Tree.h"
using namespace std;


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 (NULL == temp) {
		return false;
	}
	Node *node = new Node();
	if (NULL == node) {
		return false;
	}
	node->index = pNode->index;
	node->data = pNode->data;
	node->pParent = temp;
	if (0 == direction) {
		temp->pLChild = node;
	}
	if (1 == direction) {
		temp->pRChild = node;
	}
	return true;
}

bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
	Node *temp = SearchNode(nodeIndex);
	if (NULL == temp) {
		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();
}

TreeDemo.cpp–>修改后的测试入口代码文件

#include <iostream>
#include "Tree.h"
using namespace std;
/*
          (0)
    5(1)         8(2)
2(3)   6(4)   9(5)   7(6)
前序遍历:0 1 3 4 2 5 6
中序遍历:3 1 4 0 5 2 6
后序遍历:3 4 1 5 6 2 0
 */
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 *pTree = new Tree();
	pTree->AddNode(0, 0, node1);
	pTree->AddNode(0, 1, node2);

	pTree->AddNode(1, 0, node3);
	pTree->AddNode(1, 1, node4);

	pTree->AddNode(2, 0, node5);
	pTree->AddNode(2, 1, node6);
	// 测试1
	pTree->PreorderTraversal();
	// 测试2
	//pTree->InorderTraversal();
	// 测试3
	//pTree->PostorderTraversal();
	// 测试4
	//pTree->DeleteNode(2, NULL);
	//pTree->PreorderTraversal();
	
	delete pTree;
	pTree = NULL;
	return 0;
}

测试1的结果为:

0   0
1   5
3   2
4   6
2   8
5   9
6   7

测试2的结果为:

3   2
1   5
4   6
0   0
5   9
2   8
6   7

测试3的结果为:

3   2
4   6
1   5
5   9
6   7
2   8
0   0

测试4的结果为:

0   0
1   5
3   2
4   6
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值