数据结构第十三天(树)

目录

前言

概述

树的基本概念:

树的相关操作 :

 源码:

 主函数: 

 运行结果:

 往期精彩内容:


前言

2010年一部电影创造了奇迹,它是全球第一部票房到达 27 亿美 元,总票房历史 排名第一的影片,那就是詹姆斯·卡梅隆执导的电影 阿凡达)) (Avatar)。

                                                                    图6-1-1

电影里提到了一 棵高达 900 英尺(约 274 米)的参天巨树, 是那个潘多拉星球的纳威人的家园,让人印象非常深刻。可惜那只是导演的梦想,地球上不存在这样的物种。 无论多高多大的树,那也是从小到大、由根到叶、一点点成长起来的.俗话说十 年树木,百 年树人,可一棵大树又何止是十年这样容易一一哈哈,说到哪里去了,我 们现在不是在上生物谍,而是要讲 种新的数据结构 一树。

概述

树的基本概念:

  1. 树是一种非线性数据结构,由节点(node)和边(edge)组成。树的一个节点称为根节点,根节点可以有零个或多个子节点,每个子节点又可以有自己的子节点,以此类推。

  2. 树的过程算法:树的过程算法是指在构建一棵树的过程中所采取的算法。通常包括以下几个步骤:

    • 初始化:创建一个空的树或树的根节点。
    • 添加节点:按照特定规则向树中添加新节点,例如插入操作、遍历操作等。
    • 删除节点:根据需要删除树中的特定节点。
    • 查找节点:在树中查找特定节点。
    • 遍历树:按照不同的方式对树进行遍历,如前序遍历、中序遍历、后序遍历等。
  3. 常见的树结构:常见的树结构包括二叉树、二叉搜索树、平衡二叉树、红黑树等。不同的树结构适用于不同的应用场景,具有不同的特点和性能表现。

  4. 应用领域:树结构广泛应用于计算机科学领域,如算法设计、数据库索引、编译器设计、人工智能等领域。树的高效构建和操作对于提高算法的效率和性能至关重要。

树的相关操作 :

接口API

    void initTree();
	void createTree();
	bool bTrueIfTreeEmpty();
	struct NODESTRUCT* root();
	
	void addLChild(char* fatherData, char* childData);
	void addRChild(char* fatherData, char* childData);

	void clearTree();
	void ergodic();

 

 源码:

#include <malloc.h>
#include<string.h>
#include<iostream>
using namespace std;

class BINARYTREE
{
protected:
	struct NODESTRUCT
	{
		char data[15];
		struct NODESTRUCT* lChild;
		struct NODESTRUCT* rChild;
	};
	struct NODESTRUCT* treeRoot=nullptr;

protected:
	struct data
	{
		struct NODESTRUCT* nodePtr;
		struct data* pre, *bk;
	};
	struct data* top, *button;

private:
	struct NODESTRUCT* getPtrOfDataNode(char* data);
private:
	void push(struct NODESTRUCT* nodePtr);
	struct NODESTRUCT* pop();
public:
	BINARYTREE()
	{
		//队列初始化
		top = button = new struct data;
        button->pre = nullptr;
        button->bk = nullptr; 
	}
	void initTree();
	void createTree();
	bool bTrueIfTreeEmpty();
	struct NODESTRUCT* root();
	
	void addLChild(char* fatherData, char* childData);
	void addRChild(char* fatherData, char* childData);

	void clearTree();
	void ergodic();
};
void  BINARYTREE::push(struct NODESTRUCT* nodePtr){
	top->nodePtr = nodePtr;
	top->bk = new struct data;
	top->bk->pre = top;
	top = top->bk;
	top->bk = nullptr;
	return;
}
struct BINARYTREE::NODESTRUCT* BINARYTREE::pop()
{
	if (button != top)
	{
		top->nodePtr = button->nodePtr;
		button = button->bk;
		free(button->pre);
		return top->nodePtr;
	}
	else
	{
		return nullptr;
	}
}




void BINARYTREE::initTree()
{
	treeRoot = new NODESTRUCT;
	treeRoot->lChild = nullptr;
	treeRoot->rChild = nullptr;
	//测试区间
		strcpy_s(treeRoot->data, "j");
		/*treeRoot->lChild = (NODESTRUCT*)malloc(sizeof(NODESTRUCT*));
		strcpy_s(treeRoot->lChild->data, "jjs");*/
		//测试区间
	return;
}
struct BINARYTREE::NODESTRUCT* BINARYTREE::root()
{
	return treeRoot;
}
bool BINARYTREE::bTrueIfTreeEmpty()
{
	if (treeRoot == nullptr)
	{
		return true;
	}
	else{
		return false;
	}
}
struct BINARYTREE::NODESTRUCT* BINARYTREE::getPtrOfDataNode(char* data)
{
	NODESTRUCT* nodePtr = nullptr;
	if (treeRoot != nullptr)
	{
		push(treeRoot);
		while (true)
		{
			nodePtr = pop();
			if (nodePtr == nullptr)
			{
				break;
			}
			if (strcmp(data, nodePtr->data) == 0)
			{
				return nodePtr;
			}
			cout << nodePtr->data << endl;
			if (nodePtr->lChild != nullptr)
			{
				push(nodePtr->lChild);
			}
			if (nodePtr->rChild != nullptr)
			{
				push(nodePtr->rChild);
			}
		}
	}
	return nullptr;
}
void BINARYTREE::addLChild(char* fatherData, char* childData)
{
	NODESTRUCT* nodePtr = getPtrOfDataNode(fatherData);
	if (nodePtr != nullptr)
	{
		nodePtr->lChild = new NODESTRUCT; 
		strcpy_s(nodePtr->lChild->data, childData);
		nodePtr->lChild->lChild = nullptr;
		nodePtr->lChild->rChild = nullptr;
	}
	return;
}
void BINARYTREE::addRChild(char* fatherData, char* childData)
{
	NODESTRUCT* nodePtr = getPtrOfDataNode(fatherData);
	if (nodePtr != nullptr)
	{
		nodePtr->rChild = new NODESTRUCT;
		strcpy_s(nodePtr->rChild->data, childData);
		nodePtr->rChild->lChild = nullptr;
		nodePtr->rChild->rChild = nullptr;
	}
	return;
}
void BINARYTREE::ergodic(){
	NODESTRUCT* nodePtr = nullptr;
	if (treeRoot != nullptr)
	{
		push(treeRoot);
		while (true)
		{
			nodePtr = pop();
			if (nodePtr == nullptr)
			{
				break;
			}
			cout << nodePtr->data << endl;
			if (nodePtr->lChild != nullptr)
			{
				push(nodePtr->lChild);
			}
			if (nodePtr->rChild != nullptr)
			{
				push(nodePtr->rChild);
			}
		}
	}
	return;
}
void BINARYTREE::clearTree()
{
	NODESTRUCT* nodePtr = nullptr;
	if (treeRoot != nullptr)
	{
		push(treeRoot);
		while (true)
		{
			nodePtr = pop();
			if (nodePtr == nullptr)
			{
				break;
			}
			if (nodePtr->lChild != nullptr)
			{
				push(nodePtr->lChild);
			}
			if (nodePtr->rChild != nullptr)
			{
				push(nodePtr->rChild);
			}
			delete nodePtr;
		}
	}
	return;

}

 测试函数: 

#include"BINARYTREE.h"//源码头文件
#include<windows.h>
int main()
{

BINARYTREE binaryTree;
binaryTree.initTree();
binaryTree.addLChild("j", "s");
binaryTree.addRChild("j", "U");
binaryTree.ergodic();
binaryTree.clearTree();
if (binaryTree.bTrueIfTreeEmpty()==true)
{
    cout << "Tree destroyed" << endl;
}


    system("pause");
    return 0;
}

 运行结果:

​​​​​​​

 往期精彩内容:

数据结构第一天(生成1000以内的随机数自动填充数组)

数据结构第二天(直接插入排序/内存申请/指针操作)

数据结构第三天(折半插入排序)

数据结构第四天(希尔排序)

数据结构第五天(冒泡排序)

数据结构第六天(快速排序)

数据结构第七天(简单选择排序)

数据结构第八天(归并排序)

数据结构第九天(堆排序)

数据结构第十天(排序算法总结)

数据结构第十一天(栈)

数据结构第十二天(队列)
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/2203_75909025/article/details/136000691

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值