当前位置:网站首页>2022-08-01 回顾基础二叉树以及操作
2022-08-01 回顾基础二叉树以及操作
2022-08-05 09:04:00 【ShaYQ】
二叉树常被用于实现二叉查找树和二叉堆。
在计算机科学中,二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”和“右子树”。
根据不同的用途可分为:
1、完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1~h-1)
的结点数都达到最大个数,第h层有叶子结点,并且叶子结点都是从左到右依次排布,这就是完全二叉树。2、满二叉树——除了叶结点外每一个结点都有左右子叶且叶子结点都处在最底层的二叉树。
3、平衡二叉树——平衡二叉树又被称为AVL树(区别于AVL算法),它是一棵二叉排序树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
/* File: binary_tree.cpp Function: 回顾二叉树的一些操作,创建、销毁、遍历 Writer: syq Time: 2022-08-01 */
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef int KEY_VALUE;
/* 定义二叉树的节点 */
struct BinaryTreeNode
{
struct BinaryTreeNode* m_left;
struct BinaryTreeNode* m_right;
KEY_VALUE m_nKey; //key
};
/* 定义二叉树,二叉树可以从它的根节点进行遍历 */
struct BinaryTree
{
struct BinaryTreeNode* pRoot;
};
/* 创建一个二叉树的节点 */
struct BinaryTreeNode* CreateTreeNode(KEY_VALUE nKey)
{
struct BinaryTreeNode* pNode = (struct BinaryTreeNode*)malloc(sizeof(struct BinaryTreeNode));
if(pNode)
{
std::cout<<"create address:"<<pNode<<std::endl;
pNode->m_left = pNode->m_right = nullptr;
pNode->m_nKey = nKey;
return pNode;
}
return nullptr;
}
/* 向一个二叉树中插入一个二叉树节点,如果key已经存在则不需要执行插入 */
int BinaryTreeInsert(struct BinaryTree* pTree,KEY_VALUE nKey)
{
if(pTree == nullptr)
{
return -1;
}
if(pTree->pRoot == nullptr)
{
//根节点为空的时候
pTree->pRoot = CreateTreeNode(nKey);
return 0;
}
//根节点
struct BinaryTreeNode* pRoot = pTree->pRoot;
struct BinaryTreeNode* pTmp = pTree->pRoot;
//进行遍历,同时确定确定插入位置
while(pRoot != nullptr)
{
pTmp = pRoot;
if(nKey < pTmp->m_nKey)
{
//左子树
pRoot = pTmp->m_left;
}
else if(nKey > pTmp->m_nKey)
{
pRoot = pTmp->m_right;
}
}
//pRoot已经为nullptr了,则上一个有效节点是pTmp;
if (nKey < pTmp->m_nKey)
{
pTmp->m_left = CreateTreeNode(nKey);
} else
{
pTmp->m_right = CreateTreeNode(nKey);
}
return 0;
}
/* 销毁m某个节点开始的,二叉子树,采用中序遍历 */
int BinaryTree_Destory(struct BinaryTreeNode* pNode)
{
if (pNode == nullptr) return 0;
BinaryTree_Destory(pNode->m_left);
free(pNode);
std::cout<<"free address:"<<pNode<<std::endl;
BinaryTree_Destory(pNode->m_right);
return 0;
}
/* 二叉树的遍历,前序,中序,后续遍历 前序遍历:先根节点,再左节点,最后右节点 中序遍历:先左节点,再根节点,最后右节点 后序遍历:先左节点,再右节点,最后根节点 */
int BinaryTree_Traversal(struct BinaryTreeNode* pNode)
{
if (pNode == nullptr) return 0;
BinaryTree_Traversal(pNode->m_left);
std::cout << pNode->m_nKey<<std::endl;
BinaryTree_Traversal(pNode->m_right);
return 0;
}
int main()
{
int keyArray[10] = {
1,3,5,6,11,2,4,88,66,55};
struct BinaryTree T = {
0};
int i = 0;
for (i = 0;i < 10;i ++) {
BinaryTreeInsert(&T, keyArray[i]);
}
BinaryTree_Traversal(T.pRoot);
getchar();
BinaryTree_Destory(T.pRoot);
getchar();
}
边栏推荐
- 浅谈自动采集程序及入库
- NC20164 :最大数MAXNUMBER [线段树]
- What is the connection and difference between software system testing and acceptance testing? Professional software testing solution recommendation
- Example of Noise Calculation for Amplifier OPA855
- Thinking and summary of the efficiency of IT R&D/development process specification
- 8.4 Summary of the mock competition
- Thinking after writing a code with a very high CPU usage
- 树状数组模版+例题
- DTcloud 装饰器
- Weekly Report 2022-8-4
猜你喜欢

Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上

【LeetCode】623. 在二叉树中增加一行

CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类

ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...

XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法

How to make pictures clear in ps, self-study ps software photoshop2022, simple and fast use ps to make photos clearer and more textured

工程制图知识点

汇编语言(8)x86内联汇编

express hot-reload

DPU — 功能特性 — 网络系统的硬件卸载
随机推荐
SQL语句查询字段内重复内容,并按重复次数加序号
并发之CAS
十一道家常小菜详细攻略[图文并茂]
sql server中 两表查询 平均数 分组
leetcode refers to Offer 10- II. Frog jumping steps
【 a daily topic 】 1403. The increasing order of the sequence, boy
pnpm 是凭什么对 npm 和 yarn 降维打击的
JS syntax usage
mySQL数据库初始化失败,有谁可以指导一下吗
express hot-reload
flink cdc支持从oracle dg库同步吗
DPU — 功能特性 — 网络系统的硬件卸载
JS语法使用
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
Three solutions to solve cross-domain in egg framework
Undefined symbols for architecture arm64解决方案
程序员的七种武器
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
动态内存开辟(C语言)
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood