MindMap Gallery Data structure_5_1 binary tree
Refer to the "Data Structure" course of the Wangdao Computer Postgraduate Entrance Examination to independently summarize the knowledge points and share the necessary review materials, so that everyone can browse and review when preparing for the exam, and improve the review efficiency. I hope it will be helpful to everyone in preparing for the exam.
Edited at 2024-03-31 11:30:50This Valentine's Day brand marketing handbook provides businesses with five practical models, covering everything from creating offline experiences to driving online engagement. Whether you're a shopping mall, restaurant, or online brand, you'll find a suitable strategy: each model includes clear objectives and industry-specific guidelines, helping brands transform traffic into real sales and lasting emotional connections during this romantic season.
This Valentine's Day map illustrates love through 30 romantic possibilities, from the vintage charm of "handwritten love letters" to the urban landscape of "rooftop sunsets," from the tactile experience of a "pottery workshop" to the leisurely moments of "wine tasting at a vineyard"—offering a unique sense of occasion for every couple. Whether it's cozy, experiential, or luxurious, love always finds the most fitting expression. May you all find the perfect atmosphere for your love story.
The ice hockey schedule for the Milano Cortina 2026 Winter Olympics, featuring preliminary rounds, quarterfinals, and medal matches for both men's and women's tournaments from February 5–22. All game times are listed in Eastern Standard Time (EST).
This Valentine's Day brand marketing handbook provides businesses with five practical models, covering everything from creating offline experiences to driving online engagement. Whether you're a shopping mall, restaurant, or online brand, you'll find a suitable strategy: each model includes clear objectives and industry-specific guidelines, helping brands transform traffic into real sales and lasting emotional connections during this romantic season.
This Valentine's Day map illustrates love through 30 romantic possibilities, from the vintage charm of "handwritten love letters" to the urban landscape of "rooftop sunsets," from the tactile experience of a "pottery workshop" to the leisurely moments of "wine tasting at a vineyard"—offering a unique sense of occasion for every couple. Whether it's cozy, experiential, or luxurious, love always finds the most fitting expression. May you all find the perfect atmosphere for your love story.
The ice hockey schedule for the Milano Cortina 2026 Winter Olympics, featuring preliminary rounds, quarterfinals, and medal matches for both men's and women's tournaments from February 5–22. All game times are listed in Eastern Standard Time (EST).
Binary tree
storage structure
Basic concepts, characteristics, properties
basic concept
definition
Or an empty binary tree
Or it consists of a root node and two disjoint left subtrees and right subtrees called roots.
Features
The left subtree and right subtree cannot be reversed (ordered tree)
The degree of any node ≤ 2
Binary tree V.S ordered tree with degree 2
Ordered tree of degree 2
node order
At least one node has degree = 2
Binary tree
node order
The degree of the node ≤ 2 (the degree of the node can all be < 2)
special binary tree
full binary tree
A binary tree with height h and 2^h-1 nodes
Features
Only the last layer has leaf nodes
There are no nodes with degree 1 (only nodes with degree 0/2)
Numbering starts from 1 in hierarchical order, node i
The left child is 2i
The right child is 2i 1
The parent node is [i/2] (the largest integer not greater than i/2 - rounded down)
complete binary tree
On the basis of a full binary tree, several nodes with higher numbers can be removed
Features
Only the last two layers have leaf nodes
There is at most one node with degree 1
Numbering starts from 1 in hierarchical order, node i
The left child is 2i
The right child is 2i 1
The parent node is ⌊i/2⌋ (the largest integer not greater than i/2 - rounded down)
n≤⌊n/2⌋ is a branch node, i>⌊n/2⌋ (≥⌊n/2⌋ 1) is a leaf node
Binary sorting tree
Left subtree keyword<root node keyword<right subtree keyword
Binary sorting tree can be used for sorting and searching elements
balanced binary tree
The depth difference between the left and right subtrees does not exceed 1
characteristic
Can have higher search efficiency
Nature of regular exams
Binary tree
Test point 1
n0 = n2 1
Test point 2
The i-th level of the binary tree has at most 2^(i-1) nodes.
Test point 3
A binary tree with height h has at most 2^h-1 nodes (full binary tree)
complete binary tree
Test point 1
The height h of a complete binary tree with n (n>0) nodes <---->The level of the i-th node of the complete binary tree is
Determined by the range of the number of nodes of a full binary tree with a height of h--a height of h-1 (open on the left and closed on the right)
Determine the number of nodes of a complete binary tree with height h (left closed and right open)
Test point 2
Number of nodes n--->n0,n1,n2 (complete binary tree)
2k (even number) nodes
n1 = 1; n0 = k; n2 = k-1
2k-1 (odd number) nodes
n1 = 0; n0 = k; n2 = k-1
Binary tree traversal
First/middle/last order traversal
Order rules based on the recursive properties of trees
three methods
Space complexity O(h 1(call function to determine empty tree)) = O(h)
Preorder traversal (root traversal first)
root, left, right
In-order traversal (middle root traversal)
left, root, right
Postorder traversal (post-root traversal)
left, right, root
Traverse an arithmetic expression tree
source
Prefix expression obtained by preorder traversal
Infix expression of inorder traversal (no parentheses - delimiter required)
Postfix expression obtained by postorder traversal
Test point: Find the traversal sequence
Expand branch nodes layer by layer (recursively traverse subtrees in the same way)
traversal route method
Preorder - the node will be output when it is accessed for the first time.
In-order—The node will be output when it is accessed for the second time.
Postorder—the node will be output when it is accessed for the third time.
Leaf nodes should be understood as the left and right subtrees are empty trees
level-order traversal
Algorithmic thinking
Initialize an auxiliary queue
Root node joins the queue
Repeat cycle
I. If the queue is not empty, the head node is dequeued.
access this node
And insert its left and right children into the end of the queue (if any)
II. Repeat I until the queue is empty
Notice
Store pointers instead of nodes
Construct a binary tree from a traversal sequence
Traverse combinations of sequences (must have in-order sequences)
Preorder and inorder traversal of sequence
Post-order and in-order traversal sequence
Level order in-order traversal sequence
Pairwise combinations of preorder, postorder, and hierarchical sequences cannot uniquely determine a binary tree. (No inorder sequence cannot divide left and right subtrees)
Information learned from various traversal sequences
Preorder traversal of sequence
Root node Preorder traversal sequence of the left subtree Preorder traversal sequence of the right subtree
In-order traversal sequence
Preorder traversal sequence of the left subtree Root node Preorder traversal sequence of the right subtree
Postorder traversal of sequence
Preorder traversal sequence of the left subtree Preorder traversal sequence of the right subtree Root node
level order traversal sequence
Root node Root node of the left subtree Root node of the right subtree
Key steps (recursive use)
1. Find the root node of the tree and divide the left and right subtrees according to the inorder sequence
2. Then find the root nodes of the left and right subtrees, and divide the subtrees of the left and right children according to the in-order sequence of the left and right subtrees.
clue binary tree
Problem introduction
Can in-order traversal start from a specified node (cannot - only start from the root node)
How to find the predecessor of the specified node p in the inorder traversal sequence
concept
clue
Pointers to predecessors/successors become clues
Precursor in sequence/Successor in sequence
Specifies the predecessor/successor node of the specified node in the in-order traversal sequence
First-order-precursor/First-order-successor, Later-order-precursor/Later-order-successor
storage structure
Use n 1 empty link domains of the binary tree to connect the predecessor and successor of the node
On the basis of ordinary binary tree nodes, two flag bits ltag and rtag are added.
ltag
ltag==1
Indicates that lhlid points to the precursor
ltag==0
Indicates that lhlid points to the left child
rtag
rtag==1
Indicates that lhlid points to the successor
rtag==0
Indicates that lclid points to the right child
Three clues binary tree
In-order clue binary tree
"Clueing" based on in-order traversal sequence
preorder clue binary tree
"Clueing" based on pre-order traversal sequence
postorder clue binary tree
"Clueing" based on post-order traversal sequence
effect
It is convenient to start from a specified node and find its predecessor and successor.
Facilitates traversal of first/middle/last order traversal sequences
Threading of binary trees
Hand calculation
Determine the clue binary tree type - inorder, preorder or postorder?
According to the corresponding traversal rules, determine the access sequence of each node and write the number
Connect n 1 empty link domains to the predecessor and successor
Code
Algorithm ideas
Transformation of inorder/preorder/postorder traversal algorithm
core
Use a pointer pre to record the predecessor node of the currently accessed node.
When a node is accessed, the clue information connecting the node to the predecessor node
1. Is the right child pointer of the predecessor node null?
2. Is the left child pointer of the current node null?
Easy to make mistakes
1. Processing of rchild and rtag of the last node (fully utilizing the empty link domains of all nodes)
2. In pre-order threading, pay attention to solving the problem of falling into an infinite loop; When itag==0, the left subtree can be clued in order.
Find the predecessor and successor of p in the clue binary tree (cannot be implemented in X-binary linked list)
In-order clue binary tree (left root (p) right (left root right))
Successor
The lower left node in the right subtree of p
precursor
The lower right node in the left subtree of p
Preorder clue binary tree (around root (p))
Successor
If p has a left child, the left child is followed in order (root p left and right - root p (root left and right) right)
If p has no left child, the right child is followed in order (root p right - root p (root left and right))
Precursor (if the parent node of p can be found)
And p is the left child (root (left and right of root p) right), then the parent node of p is its predecessor node
And p is the right child and its left brother is empty, then the parent node of p is its predecessor node.
And p is the right child and its left brother is not empty, then the predecessor of p is the subtree of its left brother. The last node to be traversed in order (p parent left (root left) (root p left))
If p is the root node, then p has no predecessor
postorder clue binary tree
Successor (if the parent node of p can be found)
And p is the right child ((left)(left and right root p)p parent), then the parent node of p is its successor
And p is the left child ((left and right root p)p parent), and the right brother is empty, then the parent node of p is its successor.
And p is the left child and its right brother is not empty, then the successor of p is the subtree of its right brother. The first node traversed in postorder ((left and right roots p) (left and right roots) p parent)
If p is the root node, then p has no precedence.
precursor
If p has a right child, the right child is followed in order (left and right roots p - left and right (left and right roots (p right)) p)
If p has no right child, then the left child is followed in order (left root p-left (left and right roots (p left)) p)
Ideas
Expand branch nodes layer by layer (analysis method)
Special case
Node p is the left child of its parent node, and the right brother is empty
Node p is the left child of its parent node and has a right brother
Node p is the right child of its parent node, and the left brother is empty
Node p is the right child of its parent node and has a left brother