目录
前言
2010年一部电影创造了奇迹,它是全球第一部票房到达 27 亿美 元,总票房历史 排名第一的影片,那就是詹姆斯·卡梅隆执导的电影 阿凡达)) (Avatar)。
图6-1-1
电影里提到了一 棵高达 900 英尺(约 274 米)的参天巨树, 是那个潘多拉星球的纳威人的家园,让人印象非常深刻。可惜那只是导演的梦想,地球上不存在这样的物种。 无论多高多大的树,那也是从小到大、由根到叶、一点点成长起来的.俗话说十 年树木,百 年树人,可一棵大树又何止是十年这样容易一一哈哈,说到哪里去了,我 们现在不是在上生物谍,而是要讲 种新的数据结构 一树。
概述
树的基本概念:
-
树是一种非线性数据结构,由节点(node)和边(edge)组成。树的一个节点称为根节点,根节点可以有零个或多个子节点,每个子节点又可以有自己的子节点,以此类推。
-
树的过程算法:树的过程算法是指在构建一棵树的过程中所采取的算法。通常包括以下几个步骤:
- 初始化:创建一个空的树或树的根节点。
- 添加节点:按照特定规则向树中添加新节点,例如插入操作、遍历操作等。
- 删除节点:根据需要删除树中的特定节点。
- 查找节点:在树中查找特定节点。
- 遍历树:按照不同的方式对树进行遍历,如前序遍历、中序遍历、后序遍历等。
-
常见的树结构:常见的树结构包括二叉树、二叉搜索树、平衡二叉树、红黑树等。不同的树结构适用于不同的应用场景,具有不同的特点和性能表现。
-
应用领域:树结构广泛应用于计算机科学领域,如算法设计、数据库索引、编译器设计、人工智能等领域。树的高效构建和操作对于提高算法的效率和性能至关重要。
树的相关操作 :
接口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;
}
运行结果:
往期精彩内容:
数据结构第十二天(队列)
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/2203_75909025/article/details/136000691