Data structure-tree
The basic concept of the tree:
- Node, root node, parent node, child node, sibling node
- A tree can have no nodes and is called an empty tree
- A tree can have only one node, which is the root node
- Subtree, left subtree, right subtree
Degree of tree:
- Node degree: the number of subtrees
- Tree degree: the maximum value among all node degrees
- Leaf node: a leaf node with degree 0
- Non-leaf nodes: nodes whose degree is not 0
The depth and height of the tree:
- Number of levels: the root node is on the first level, the child nodes of the root node are on the second level, and so on
- Node depth: the total number of nodes on the unique path from the root node to the current node (root node----->current node)
- Node height: the total number of nodes on the path from the current node to the farthest leaf node (current node----->farthest leaf node)
- The depth of the tree: the maximum value of all node depths
- Tree height: the maximum value among all node heights
- The depth of the tree is equal to the height of the tree
Ordered tree: there is an order relationship between the child nodes of any node in the tree
Unordered tree: there is no order relationship between the child nodes of any node in the tree
Forest: a collection of m (m is greater than or equal to 0) disjoint trees
Binary Tree
The characteristics of the binary tree:
- The maximum degree of each node is 2 (up to 2 subtrees)
- The left subtree and the right subtree are in order
- Even if a node has only one subtree, distinguish between left and right subtrees
- Binary tree is an ordered tree
The nature of the binary tree:
- The i-th layer of a non-empty binary tree has at most 2^i-1 nodes (i is greater than or equal to 1)
- On the binary tree with height h, there are at most 2^h -1 nodes (h >=1)
- For any non-empty binary tree, if the number of leaf nodes is n0 and the number of nodes with degree 2 is n2, then n0=n2 + 1
- Assuming that the number of nodes with degree 1 is n1, then the total number of nodes in the binary tree n = n0 + n1 + n2
- The number of edges of the binary tree T = n1 + 2 ^n2 = n -1 = n0 + n1 + n2 -1
Proper Binary Tree
The degree of all nodes is either 0 or 2
Full Binary Tree
The degree of all nodes is either 0 or 2. And all the leaf nodes are in the last layer
- In a binary tree of the same height, a full binary tree has the largest number of leaf nodes and the largest number of total nodes
- A full binary tree must be a true binary tree, and a true binary tree is not necessarily a full binary tree
Complete Binary Tree
Only the last 2 layers of leaf nodes appear, and the leaf nodes of the last 1 layer are aligned to the left
- The complete binary tree is a full binary tree from the root node to the bottom 2nd level
- A full binary tree must be a full binary tree, a full binary tree is not necessarily a full binary tree
The nature of the complete binary tree:
- The node with degree 1 has only the left subtree
- There are either 1 or 0 nodes with degree 1
- For a binary tree with the same number of nodes, the height of a complete binary tree is the smallest
Practice questions:
If a complete binary tree has nodes 768, the number of leaf nodes seeking ???
assuming the number of leaf nodes n0, 1 degree is the number of node n1, the node number of degree 2 is n2
number of points summarized n = n0 + n1 + n2, and n0 = n2 + 1
n = 2n0 + n1 – 1
The n1 of a complete binary tree is either 0 or 1 When
n1 is 1, n = 2n0, n must be the number of even-numbered
leaf nodes n0 = n / 2, the number of non-leaf nodes n1 + n2 = n / 2 When
n1 is 0, n = 2n0 – 1, n must be an odd number of
leaf nodes n0 = (n + 1) / 2, non-leaf nodes Number n1 + n2 = (n – 1) / 2 Number of
leaf nodes n0 = floor( (n + 1) / 2) = ceiling( n / 2) Number of
non-leaf nodes n1 + n2 = floor( n / 2) = ceiling( (n – 1) / 2)
so the number of leaf nodes is 384