当前位置:网站首页>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 )
边栏推荐
- Azure Devops(十四) 使用Azure的私有Nuget仓库
- How to use causal inference and experiments to drive user growth| July 28 tf67
- 0716RHCSA
- 0710RHCSA
- [机器学习] 实验笔记 – 表情识别(emotion recognition)
- Excel录制宏
- Vim技巧:永远显示行号
- [figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
- 备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
- 【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
猜你喜欢

Zero basic learning canoe panel (13) -- trackbar

yum和vim须掌握的常用操作

Excel添加按键运行宏

基于百问网IMX6ULL_PRO开发板驱动AP3216实验

Zero basic learning canoe panel (14) -- led control and LCD control

Error: cannot find or load main class XXXX

【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结

AtCoder Beginner Contest 261E // 按位思考 + dp

Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem

Django 2 ----- 数据库与Admin
随机推荐
ORAN专题系列-21:主要的玩家(设备商)以及他们各自的态度、擅长领域
卷积神经网络模型之——AlexNet网络结构与代码实现
全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
Word style and multi-level list setting skills (II)
Handwriting a blog platform ~ first day
为提高效率使用ParallelStream竟出现各种问题
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
错误: 找不到或无法加载主类 xxxx
Azure Devops(十四) 使用Azure的私有Nuget仓库
迁移PaloAlto HA高可用防火墙到Panorama
工业互联网的内涵及其应用
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
VIM tip: always show line numbers
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
央行数研所穆长春:数字人民币可控匿名是维护公众利益和金融安全的客观需要
arm架构移植alsa-lib和alsa-utils一路畅通
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
Use vsftpd service to transfer files (anonymous user authentication, local user authentication, virtual user authentication)
AtCoder Beginner Contest 261E // 按位思考 + dp