MindMap Gallery Mind map of data structure tree
Summarizes the knowledge points of data structure trees. The main contents include definitions and basic terms, properties of trees, definitions and basic terms of binary trees, properties of binary trees, and storage structures of binary trees.
Edited at 2023-01-06 10:54:13Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Tree
first part
Definitions and basic terms
Root node, leaf node, edge, branch node, empty tree: a number with 0 nodes, subtree, path between nodes: from top to bottom. Path length: the number of edges passed.
Features: There is one and only one node called the root. Each node except the root node has only one predecessor. All nodes can have many successors.
An ordered tree is unordered. From left to right, is it ordered or unordered?
trees and forest
Node, tree attribute description
Degree of tree: the number of children that a tree has the largest number of child nodes
Node height: counting from bottom to top
Node level, depth: counting from top to bottom
Degree of node: How many children does a node have?
tree nature
Number of nodes = total degree 1
Trees of degree m and m-ary trees
A tree with degree m: the degree of each node is <=m, and at least one node has degree m. It must be a non-empty tree with at least m 1 nodes.
m-ary tree: the degree of any node <=m, the degree of all nodes is allowed <m, it can be an empty tree
The i-th level of a tree with degree m has at most m^i-1 nodes, and the m-ary tree is the same
An m-ary tree with height h has at most m^h-1/m-1 nodes.
An m-ary tree with height h has at least h nodes, and a tree with height h and degree m has at least h m-1 nodes.
The minimum height of an m-ary tree with n nodes is (log)[m](n(m-1) 1 rounded up
the second part
Binary tree definition and basic terminology
basic concept
Features: Each node has at most two subtrees, and the left and right subtrees cannot be reversed.
Several special binary trees
full binary tree
The height is h and the number of nodes is 2^h-1
Numbering starts from 1 in hierarchical order, and the parent node of node i is rounded down to i/2
complete binary tree
It has a one-to-one correspondence with the full binary tree and can only be deleted from back to front. The queue is similar.
i<=n/2 is rounded to the next branch node, and if it is greater than or equal to n/2, it is rounded to the leaf node.
Binary sorting tree
The keys of all nodes in the right subtree are greater than the keys of the root node, and the keys of all the nodes in the left subtree are less than
Used for sorting and searching elements
balanced binary tree
The depth difference between the left subtree and the right subtree of any node in the tree does not exceed 1
Fatter, with as few layers as possible
Higher search efficiency
Properties of binary trees
Properties of binary trees
n0=n2 1
The rest is the same as above
Properties of complete binary trees
The height of a complete binary tree with n nodes is log2(n 1) rounded up and (log2n) 1 rounded down.
According to the number of nodes being n, we can deduce the number of nodes with degrees 0, 1, and 2.
Binary tree storage structure
sequential storage
Create a static array to store the tree in order from top to bottom, from left to right. The tree structure contains two variables, one is the value of the data, and the other is the bool value to determine whether the node is empty. During initialization, a loop is required to set each node to be empty (bool value is true)
operate
The left child of i node 2i
The right child of i node 2i 1
The parent node of i is rounded down to i/2
Round up log2(n 1)
complete binary tree
Determine whether i has a left child 2i<=n
Determine whether i has a right child 2i 1<=n
Determine whether node i is a leaf or branch i> round down to n/2
In the sequential storage of binary trees, the node numbers of the binary tree must be matched with the complete binary tree, so that the logical relationships between the nodes can be reflected by the sequentially stored numbers. It only makes sense for complete binary trees to be stored sequentially.
In the worst case, a single tree with only the right child requires at least 2^h-1 sequential storage units
chain storage
The structure has three variables, one is the value, and the other two are the left and right child pointer variables.
A binary tree linked list with n nodes has a total of n 1 empty link fields.
The advantage is that it is very convenient to find the left and right children of a node, but the disadvantage is that finding the parent node requires traversing from the beginning. The method is to set one more parent node pointer in the structure.
the third part
First/middle/post-order traversal of binary tree
preorder traversal
Around the root
prefix expression
step
If empty, do nothing
access root node
Preorder traversal of left subtree
Preorder traverse the right subtree
inorder traversal
left root right
Inorder expression, requires home delimiter
Ruokong does nothing
In-order traversal of left subtree
access root node
In-order traversal of right subtree
Postorder traversal
left and right roots
postfix expression
If empty, do nothing
Postorder traversal of left subtree
Postorder traversal of right subtree
access root node
Each node will be passed three times in each traversal, and the three traversals visit the node during the first, second, and third passes in sequence.
Level-order traversal of binary tree
Algorithmic thinking
Initialize an auxiliary queue
Let the root node join the queue
If the queue is not empty, let the root node dequeue and let the left and right children of the root node join the queue (the left and right children exist)
Repeat step 3 until the queue is empty
Construct a binary tree from a traversal sequence
If only one of the front, middle and back order traversal sequences is given, a binary tree cannot be uniquely determined.
Two must be combined, and one of them must be an intermediate sequence.
The principle is that the root node can be determined based on the front and rear levels, and then the left and right subtrees are divided according to the middle order.
Clue Binary Tree Concept
cueing
Idea: There are n 1 empty link domains for n nodes. We use the empty link domain of this node to store the predecessor and successor of this node in each sequence traversal. Set rtag, ltag indicates whether it points to child rag=1 or clue rag=0.
Precursor clue: served by the pointer of the left child
Successor clue: served by the pointer of the right child
In-order predecessor: points to the predecessor of the node in the in-order traversal
The reason is that if we do not use threaded methods, we need to traverse the nodes all over again, which is inefficient. The simple method is to use two pointers to traverse in sequence and record, and then use the two pointers to find the node for equivalence judgment. Two types of before and after judgments correspond to predecessor and successor.
Threading of binary trees
Three types of traversal code implementations
Modification of the three traversal algorithms. When visiting a node, the clue information connecting the node and the predecessor node is
Use a pointer pre to record the predecessor node of the currently accessed node.
Easy to make mistakes
Processing of rchild and rtag of the last node
Preorder clues to pay attention to the magic circle problem of love. When ltag=0, the left subtree can be clued in preorder.
Find the predecessor and successor in the clue binary tree
Find the predecessor and successor of p node in in-order clue binary tree
Looking for a successor
Determine the value of rtag
if 0
Loop to find the left node of the right subtree until it is a leaf node, then this node is the successor
if 1
Then the right child of p is the successor
Find a pioneer
Determine the value of ltag
if 0
Loop to find the right child of the left subtree until it is a leaf node, then this node is the predecessor.
if 1
Then the left child of p is the predecessor
precedence
Find a pioneer
Determine ltag=1
is the left pointer
ltag=0
In preorder traversal, the left and right children can only be successors and not predecessors.
Alternative method is to traverse from scratch
Use a three-pronged linked list instead and add a pointer to the parent node
If there is a parent node and p is the left child
Then the parent node of p is the predecessor
If there is a parent node and p is the right child
Brother Zuo is not empty
The predecessor of p is the last node of the left sibling subtree in preorder traversal.
The left brother is empty
The parent node is the predecessor
If p is the root node
no precursor
Looking for a successor
Determine ltag=1
is the left pointer
ltag=0
Suppose there is a left child
Then the left child is the successor
no left child
is the right child
Afterword
Find a pioneer
Determine ltag=1
is the left pointer
ltag=0
There must be a left child
Assume there is no right child
Then the left child is the precursor
Have a right child
Then the right child is the precursor
Looking for a successor
Determine rtag=1
is the right pointer
rtag=0
p must have a right child
In post-order traversal, the left and right subtrees can only be the predecessor of the root, not the successor.
Traverse from scratch using local methods
Use a three-pronged linked list to add a parent pointer instead
Can find the parent node
p is the right child
The parent node of p is the successor
p is the left child
The right brother is empty
The parent node of p is the successor
Right brother is not empty
The successor of p is the first node in the right sibling subtree that is traversed in postorder
P is the root node
p has no successor
fourth part
tree storage structure
Parental notation (sequential storage)
How to define
Define two structures, one for each node and one for the tree structure
Store trees into an array in order from top to bottom, left to right
The parent node pointer of the root node is -1, and the value of the parent node pointer of other nodes is the array subscript of the parent node.
operate
New elements do not need to be physically stored in order
Delete element
Option 1 changes the parent node pointer value of the deleted element to -1
Option 2: Cover the last node with the node to be deleted
If it is a leaf node, it is very convenient to delete it. What if it is not a leaf node?
It is necessary to traverse from the beginning and delete all its subtrees.
Advantages and Disadvantages: The advantage is that it is easy to find the parent node, but finding the child node requires traversing from the beginning.
Child representation (sequential chain storage)
How to define
Define three structures
linked list structure
a tree structure
The structure of a node
Store each node sequentially, and store the head pointer of the child's list in each node.
Child brother representation (chained storage)
There are two pointers in the structure, one pointing to the left child and the other pointing to the right brother of the left child.
Advantages: You can use binary tree operations to directly process trees
Present the structure of a binary tree in physical logic
Implement tree and binary tree conversion
Realize the conversion of binary trees and forests
The root nodes of each tree are regarded as brothers.
Tree and forest traversal
tree traversal
tree root traversal
If the tree is not empty, visit the root node first, and then perform root-first traversal on each subtree in turn.
depth first traversal
The tree's pre-root traversal sequence is the same as the pre-order sequence of the corresponding binary tree of this tree.
Back root traversal of tree
If the tree is not empty, first perform a post-root traversal on each subtree in turn, and finally visit the root node.
depth first traversal
The subsequent traversal of the tree is the same as the in-order sequence of the binary tree corresponding to this tree.
Tree level traversal
Implemented using queues
If the tree is not empty, the root node is added to the queue.
If the queue is not empty, the head element is dequeued and accessed, and the children of the element are added to the queue.
Repeat the previous step until the queue is empty
breadth-first traversal
Traveling through the forest
Preorder traversal of the forest
The effect is equivalent to root-first traversal of each tree in sequence.
Equivalent to pre-order traversal of a binary tree in sequence
inorder traversal
Equivalent to performing a post-root traversal on each tree in turn
Equivalent to inorder traversal of a binary tree
First first first, Shusen Er. After the middle, Shusen two.
the fifth part
Huffman tree
Right path and length
The weight of the node: a value with some practical meaning (such as the importance of the node)
The full path length of a node: the product of the path length (number of edges passed) from the root of the tree to the node and the weight of the node
The full path length of the tree
The sum of the weighted path lengths of all leaf nodes of the tree species
Huffman tree definition
In a binary tree containing n weighted leaf nodes, the binary tree with the smallest total path length is called a Huffman tree, also known as the optimal binary tree.
The structure of Huffman tree
Each time the two nodes with the smallest weights are combined into a tree, the weight of the new node generated is the sum of the two nodes. Continue the above steps in sequence until all nodes are combined.
nature
Each initial node eventually becomes a leaf node, and the node with the smallest weight has the largest path length to the root node.
The total number of nodes of the Huffman tree is 2n-1
There is no node with degree 1 in the Huffman tree
Huffman tree is not unique, but wpl must be the same and optimal
Huffman coding
Fixed length encoding: each character is represented by equal length of binary bits
Variable length encoding: allows different characters to be represented by unequal lengths of binary bits
Prefix encoding: If no encoding is the prefix of another encoding, then such encoding is called prefix encoding.
Prefix encoding and decoding are unambiguous
Huffman coding is obtained by using a Huffman tree. Each character in the character set is used as a leaf node, and the frequency of each character is used as the weight of the node to construct a Huffman tree.
nature
Huffman trees are not unique, and Huffman codes are not unique.
Application: Telegraph and compressed data
And search the collection
Union-find set is a logical structure, a specific implementation of a set. It only performs two operations: union and search. A forest is used to represent a set, and each tree is a disjoint subset.
operate
Search: Start from the specified element, go all the way north, and find the root node.
Determine whether two elements are in the same set. Find the roots of two elements separately and determine whether the root nodes are the same
And: Just make another lesson tree a subtree of another tree
Storage structure: Parental representation of a tree.
Code
Initialization: Let the parent pointer of each node point to -1
Find
and
Optimization of merging: Use the absolute value of the root node to represent the total number of nodes in the tree, so that small trees can be merged into large trees.
The worst time complexity of optimization and search operations changes from on to olog2n
The height of the tree constructed by this method does not exceed (rounded down log2n) 1
Further optimization of union lookup
Optimization of find operation: compress the path, first find the root node, and then hang all nodes on the search path under the root node