当前位置:网站首页>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 )
边栏推荐
- 0713RHCSA
- Design and principle of thread pool
- 备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
- mysql函数汇总之日期和时间函数
- Substance Designer 2021软件安装包下载及安装教程
- Excel添加按键运行宏
- Excel import and export source code analysis
- [review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]
- Zero basic learning canoe panel (15) -- CAPL output view
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
猜你喜欢

Shell常用脚本:获取网卡IP地址

Shell常用脚本:判断远程主机的文件是否存在

Docekr learning - MySQL 8 master-slave replication setup deployment

工业互联网的内涵及其应用

Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)

程序员奶爸自制AI喂奶检测仪,预判宝宝饿点,不让哭声影响老婆睡眠

Date and time function of MySQL function summary

【历史上的今天】7 月 25 日:IBM 获得了第一项专利;Verizon 收购雅虎;亚马逊发布 Fire Phone

Mid 2022 review | latest progress of large model technology Lanzhou Technology

0713RHCSA
随机推荐
JS sorts according to the attributes of the elements in the array
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】
我的创作纪念日
[300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
Shell common script: judge whether the file of the remote host exists
Docekr learning - MySQL 8 master-slave replication setup deployment
Mid 2022 review | latest progress of large model technology Lanzhou Technology
B树和B+树
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
卷积神经网络模型之——GoogLeNet网络结构与代码实现
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
Numpy简介和特点(一)
pytorch创建自己的Dataset加载数据集
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
Cyberspace Security penetration attack and defense 9 (PKI)
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
vim基础操作汇总
基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)