当前位置:网站首页>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();
}
边栏推荐
- The toss of MM before going to the street (interesting)
- Detailed explanation of DNS query principle
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
- ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
- Does flink cdc support synchronization from oracle dg library?
- Linux导出数据库数据到硬盘
- 线程之Happens-before规则
- 【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
- 最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
- 使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)
猜你喜欢

全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了

Example of Noise Calculation for Amplifier OPA855

php fails to write data to mysql

链表中的数字相加----链表专题

DPU — 功能特性 — 网络系统的硬件卸载

How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect

express hot-reload

Three solutions to solve cross-domain in egg framework

代码审计—PHP

How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
随机推荐
Linux导出数据库数据到硬盘
spark集群部署(第三弹)
“充钱”也难治快手的“亏亏亏”?
六年团队Leader实战秘诀|程序员最重要的八种软技能 - 脸皮薄容易耽误事 - 自我营销
我的杂记链接
按钮上显示值的轮流切换
【LeetCode】623. Add a row to the binary tree
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
Is there a problem with writing this?How to synchronize data in sql-client
动态库之间回调函数使用
哪个是你爱情的颜色?
基于 Kubernetes 的微服务项目整体设计与实现
IT研发/开发流程规范效能的思考总结
动态内存开辟(C语言)
在colab里怎样读取google drive数据
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
Thinking after writing a code with a very high CPU usage
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
512色色谱图