当前位置:网站首页>Basic knowledge of binary tree
Basic knowledge of binary tree
2022-07-25 13:16:00 【Cool pig】
Trees and binary trees
A binary tree is an ordered tree , If its left and right subtrees are reversed , It becomes a different binary tree . Even if the node in the tree has only one subtree , We also need to distinguish whether it is a left or right subtree .
Special binary tree :
- Full binary tree : The height of a tree is h, And it contains 2h-1 A binary tree of nodes becomes a full binary tree , That is, each layer in the tree contains the most nodes .

2. Perfect binary tree : If the last node removed from the binary tree is a full binary tree , And the nodes of the last layer are distributed from left to right , This binary tree is called a complete binary tree .
- if i < ⌊ n / 2 ⌋ \lfloor n/2 \rfloor ⌊n/2⌋ , The node I For branch nodes , Otherwise, it is a leaf node
- Leaf nodes can only appear on the two largest layers . For the leaf nodes in the largest layer, they are arranged in the leftmost position of the layer
- If there is a degree of 1 The node of , There can only be one , And the node has only left children and no right children
- After numbering by sequence , Once a node appears ( The number is i) For leaf nodes or only left children , The number is greater than i All nodes of are leaf nodes
- if n It's odd , Then each branch node has left child and right child ; if n For the even , Then the largest branch node becomes better ( The number is n/2) Only the left child , No right child , Other branch nodes have left and right children .
- Binary sort tree : The keywords of all nodes on the left subtree are smaller than those of the following nodes ; The keywords of all nodes on the right subtree are greater than those of the following nodes ; The left subtree and the right subtree are each a binary sort tree
- Balanced binary trees : The depth difference between the left subtree and the right subtree of any node in the tree does not exceed 1
Binary tree storage structure
The sequential storage of binary tree refers to using a group of storage units with continuous addresses from top to bottom , Store node elements on a complete binary tree from left to right , It will be completely numbered as... On the binary tree i The node elements of are stored in a one-dimensional array with the subscript i In the weight of
According to the properties of binary trees , Complete binary tree and full binary tree are more suitable for sequential storage .
Chain store , Use linked list to complete the storage of binary tree .
Traversal of binary tree
Traversal of binary tree refers to accessing each node in the tree according to a certain search path , So that each node is accessed once , And only once .
Common traversal order : Preface (NLR), Middle preface (LNR), In the following order (LRN)
// The first sequence traversal
void PreOrder(BiTree T){
if(T != NULL){
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
// In the sequence traversal
void InOrder(BiTree T){
if(T != NULL){
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
// After the sequence traversal
void PostOrder(BiTree T){
if(T != NULL){
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
A binary tree can be uniquely determined by the preorder sequence and middle sequence of the binary tree ;
A binary tree can be uniquely determined by the post order sequence and middle order sequence of the binary tree ;
Clue binary tree
effect : Speed up the traversal of binary tree
Regulations : If there is no left subtree , Make lchild Point to its precursor node ; If there is no right subtree , Make rchild Point to its successor node .
Add two more flag fields to identify that the pointer field points to the left ( Right ) Children are still pioneers ( The subsequent )
边栏推荐
- Convolutional neural network model -- alexnet network structure and code implementation
- Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
- How to understand metrics in keras
- 简单了解流
- [operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
- 全球都热炸了,谷歌服务器已经崩掉了
- JS sorts according to the attributes of the elements in the array
- Go: Gin custom log output format
- Error: cannot find or load main class XXXX
- 录制和剪辑视频,如何解决占用空间过大的问题?
猜你喜欢

Programmer growth chapter 27: how to evaluate requirements priorities?

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/20/bb43ab1bc447b519c3b1de0f809b31.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

ThreadLocal&Fork/Join

The larger the convolution kernel, the stronger the performance? An interpretation of replknet model

Excel录制宏

卷积神经网络模型之——AlexNet网络结构与代码实现

如何用因果推断和实验驱动用户增长? | 7月28日TF67
随机推荐
Shell common script: check whether a domain name and IP address are connected
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
int数组获取重复数据
如何用因果推断和实验驱动用户增长? | 7月28日TF67
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
卷积神经网络模型之——VGG-16网络结构与代码实现
Oran special series-21: major players (equipment manufacturers) and their respective attitudes and areas of expertise
Requirements specification template
从输入网址到网页显示
0713RHCSA
yum和vim须掌握的常用操作
程序的内存布局
Zero basic learning canoe panel (12) -- progress bar
[机器学习] 实验笔记 – 表情识别(emotion recognition)
Azure Devops(十四) 使用Azure的私有Nuget仓库
程序员成长第二十七篇:如何评估需求优先级?
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
Convolutional neural network model -- vgg-16 network structure and code implementation