当前位置:网站首页>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 )
边栏推荐
- 如何用因果推断和实验驱动用户增长? | 7月28日TF67
- Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
- 【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
- Connotation and application of industrial Internet
- web安全入门-UDP测试与防御
- Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
- Date and time function of MySQL function summary
- Mlx90640 infrared thermal imager temperature sensor module development notes (V)
- 错误: 找不到或无法加载主类 xxxx
- yum和vim须掌握的常用操作
猜你喜欢

Connotation and application of industrial Internet

Zero basic learning canoe panel (13) -- trackbar

如何用因果推断和实验驱动用户增长? | 7月28日TF67

Excel录制宏

Jupyter Notebook介绍

B tree and b+ tree

Any time, any place, super detective, seriously handle the case!

【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】

程序的内存布局
![[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
随机推荐
JS sorts according to the attributes of the elements in the array
手写一个博客平台~第一天
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
Zero basic learning canoe panel (13) -- trackbar
ThreadLocal&Fork/Join
Zero basic learning canoe panel (15) -- CAPL output view
Handwriting a blog platform ~ first day
Deployment of Apache website services and implementation of access control
0710RHCSA
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
Redis可视化工具RDM安装包分享
Docker学习 - Redis集群-3主3从-扩容-缩容搭建
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结
AtCoder Beginner Contest 261 F // 树状数组
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
Cyberspace Security penetration attack and defense 9 (PKI)
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
Convolutional neural network model -- alexnet network structure and code implementation