【学习笔记】CH5 Binary Trees | Data Structure& Algorithm Analysis in C++(ed. 3)

《Data Structure And Algorithm Analysis In C++》读书笔记一 Chapter 1 Programming:A General Overview 1.1 What is This Book About? Problem. K-Selection Problem. Input a array and choose the k-th largest. User Bubble-sort #include #include void swap(i 阅读详情

Definitions and Properties

Nodes, subtrees.
Ancestors and descendants 后代和前面点的关系
Parent and child. 直接的上下关系
Depth of the node M: the length of the path from the root to M
Height: depth of the deepest node +1
The level of the node: node of depth d is of levels. The root is at level 0
Full binary tree: where a node is either an internal node with two non-empty children or a leaf.
Complete binary tree: start at the root and fill the tree from left to right; all the levels except the d-1 level are full.

  • Full binary tree theorem:
    In a full non-empty binary tree, the number of leaf nodes is one more than internal nodes.
    The number of empty subtrees in a non-empty binary tree is one more than the number of nodes in the tree.

The binary tree ADT: class BinNode. The functions will either set and return the element, pointers and also whether this node is a leaf.

// Binary tree ADT
template<typename E> class BinNode{
   
   
public:
	virtual ~BinNode(){
   
   }//base destructor
	virtual E& element() = 0;//get the node’s value
	virtual void setElement(const E& ) = 0;// set the node’s element value
	virtual BinNode* left() const = 0; //get the left child of the node
	virtual BinNode* right() const = 0; // get the right child of the node
	virtual void setLeft(BinNode*) = 0; // set the left child
	virtual void setRight(BinNode*) = 0; //set the right child
	virtual bool isLeaf() = 0; //return whether this node is a leaf	
}

Binary Tree Traversals

Visiting the nodes: traversal; enumeration: a traversal that lists every node in the tree once.

preorder traversal:

Visit any given nodes before visiting its children. Visit the left children and then the right children.
Therefore, the order of visit would be: ABDCEGFHI. Parent first, then the left children, then the right children.

- Implementation of preorder traversal:(the other implementation is not so efficient therefore we don’t show it.)
template <typename E>
void preorder(BinNode<E>* root){
   
   //we set the root of the tree as entrance
	if (root == NULL) return;
	visit(root); //do something
	preorder(root->left);
	preorder(root->right);//first left then right
}

postorder traversal:

Visit the child of any internal nodes first. We start with a leaf, we judge whether its parent has another child, if not, visit the parent, if yes, visit the other child.
Therefore the order of visit would be DBGEHIFCA. We judge every time whether this node has a parent.
This traversal is useful when deleting nodes, when we delete the children before the parents.

- Implementation of postorder, similar as preorder
template <typename E>
void postorder(BinNode<E>* root){
   
   
	if (root==NULL) return;
	postorder(root->left);
	postorder(root->right);//first the left children, then right children, then the parent
	visit(root);//do something
}

In order traversal:

Visit the left child, then the parent node, then the right part the tree.
Therefore the order of visit would be BDAGECHFI.

- Implementation of inorder traversal, similar to preorder traversal
template<typename E>
void inorder(BinNode<E>* root){
   
   
	if (root==NULL) return;
	inorder(root->left);
	visit(root);//do something
	inorder(root->right);//first the left children, then right children, then the parent
}

example tree

A recursive count of node implementation


        
《Data Structure And Algorithm Analysis In C++》读书笔记四 Chapter 4 TreesThis chapter discusses the data structure for which the average running time of most operation is O(logN).The data structure is the Binary search tree. In STL , it is implemented by std... 阅读详情

相关推荐

【信息科学与工程学】【安全领域】第三十五篇 网络安全算法表

编号算法名称算法的逐步骤思考推理的数学方程式和分解的数学方程式参数/变量列表复杂度(时间/空间)1​AES (高级加密标准)​1. 密钥扩展:从初始密钥生成轮密钥。 2. 初始轮: 3. 主轮循环(Nr-1轮):  - SubBytes:(非线性字节替换)  - ShiftRows:对状态矩阵的行进行循环移位。  - MixColumns:(在GF(2⁸)上的矩阵乘法)  - AddRoundKey: 4. 最终轮:执行SubBytes、ShiftRows、AddR

weixin_49199313的博客 1376

【信息科学与工程学】【通信工程】第一百九十一篇 路由器体系结构03

编号类型领域问题详细的数学分析(逐步推理)参数列表(表达式 / 数值范围 / 边界 / 测量)关联知识839DCI演进 · 收敛DCI 2030 年三大技术收敛方向步骤1:方向一:IPoDWDM 成为绝对主流(Omdia 2027 59% → 2030 80%+),OTN 退化为 OLS 纯光层。步骤2:方向二:800ZR+ 取代 400ZR 成为主力速率,PCS 使 800G 达 600–1000km。步骤3:方向三:AI 集群 DCI 催生 1.6T/3.2T 超短距(<10km)相干模块(OSFP-X

weixin_49199313的博客 858

Data Structures and Algorithm Analysis in C++ 4th 原版pdf by Weiss

The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time of algorithms. As computers become faster and faster, the need for programs that can handle large amounts of input becomes more acute. Paradoxically, this requires more careful attention to efficiency, since inefficiencies in programs become most obvious when input sizes are large. By analyzing an algorithm before it is actually coded, students can decide if a particular solution will be feasible. For example, in this text students look at specific problems and see how careful implementations can reduce the time constraint for large amounts of data from centuries to less than a second. Therefore, no algorithm or data structure is presented without an explanation of its running time. In some cases, minute details that affect the running time of the implementation are explored. Once a solution method is determined, a program must still be written. As computers have become more powerful, the problems they must solve have become larger and more complex, requiring development of more intricate programs. The goal of this text is to teach students good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amount of efficiency. This book is suitable for either an advanced data structures course or a first-year graduate course in algorithm analysis. Students should have some knowledge of intermediate programming, including such topics as pointers, recursion, and object-based programming, as well as some background in discrete math.

Spark Graphx 项目实战(航班飞行网图分析)

航班飞行网图分析一.需要的技能点二.任务描述三.数据探索1.下载数据2.数据格式四.航班飞行网图问题分析1.构建航班飞行网图2.统计航班飞行网图中机场与航线的数量3.计算最长的飞行航线4.找出最繁忙的机场5.找出最重要的飞行航线6.找出最便宜的飞行航线五.代码实现 一.需要的技能点 Spark GraphX API Spark GraphX PageRank Spark GraphX Pregel 二.任务描述 需求概述: 探索航班飞行网图数据 构建航班飞行网图 使用Spark GraphX完成下列任

May_J_Oldhu的博客 1538

关于mos管的压降问题(项目遇到问题总结)

在研发过程中没有出现问题的板子,到了小批时竟出现雷达一直在触发???找了大几天,没有头绪。心中疑惑颇多,加上老板催促,自考等原因,真是压力山大。4月份自考,只有差不多一个月时间了吧,没有什么大的收获,反而在查找硬件问题的过程中,发现了一个自己忽视的一个问题,就是关于Mos管的压降问题。 备考的过程崎岖不平,工作又问题多多,孰轻孰重,如何取舍?从心理来说,不想取舍什么,只是有所得就可以,过不了就过不了呗,已经毕业十五年了,没这个本科文凭不也过来了吗?只是代表了自己在这一段时间的思考,自己兴趣吧,没什么大不了

guangod的博客 2万+

A-Level CS 计算机科学 考试知识点——考试介绍&第1章

前言:该系列文章是本人在给学生辅导本课程的过程中结合英文教材所总结的,该系列共分为30章,主要介绍了考试的核心知识点。若需要A-Level CS 计算机科学考试辅导也可以私信我。 目录 关于A-Level CS 计算机科学考试 考试简介 考试内容 考试时间 评分情况 第1章 信息表示 本章学习内容 1.01 数字系统 1.02 数字的内部编码 整数编码(Coding for integers) 二进制编码的十进制(Binary coded decimal——BCD) 1.03.

王俊的博客 6457

rk3568 RGMII KSZ8795 MAC TO MAC

参考《Rockchip_Developer_Guide_Linux_MAC_TO_MAC_CN.pdf》

明天乌儿会歌唱 6493

手把手教你学pcie--内存管理和保护机制(一):MPU与其他内存管理和保护机制详解

MPU(Memory Protection Unit)是一种专门用于 ​。,这些机制通常协同工作,形成多层防御体系,兼顾安全性、性能和灵活性。

MHD0815的博客 279

ArcGIS Pro 制作这种地图海报

ArcMap 版本与 ArcGIS Pro 版本的制图功能相差太多,无论从美观上还是制图效率都远远优于 ArcMap,用了一段时间的 ArcGIS Pro ,真心推荐有条件的大家赶紧试一试!一个软件也能搞定一张不错的海报哦!----吐槽:好的设计真的是门花时间的活,这个教程大概花了我7小时左右,我都忘了我自己教程中截图多少张了!(可能是我速度太慢了)测试数据:阿里云盘分享-----北极海冰数据.https://www.aliyundrive.com/s/SUhsnqfkmXh。

J_Giser的博客 1427

ThingsBoard教程(十六):RPC部件的使用

前言 各位读者好,截止目前,ThingsBoard系列文章已经做了十篇,分别是 ThingsBoard教程(一):ThingBoard介绍及安装 ThingsBoard教程(二):租户,租户配置,部件组模块的使用 ThingsBoard教程(三):系统设置模块的使用 ThingsBoard教程(四):规则链简介与操作 ThingsBoard教程(五):客户,资产管理 ThingsBoard教程(六):设备管理 ThingsBoard教程(七):模拟设备遥测 ThingsBoard教程(八):自定义UI T

知无涯 4580

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

上一篇: 【作业分享】Rail Station | 数据结构·stack
TemperanceH
博客等级 码龄7年 0粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值