Trees and Binary Trees

Basic terms for trees

Degree of a node: The number of subtrees a node has is called the degree of a node. A node whose degree is 0 is called a leaf node or terminal node ; a node whose degree is not 0 is called a non-terminal node or branch node.
Degree of a tree: The degree of a tree is the maximum degree of each node in the tree. As shown in the figure below, because the maximum degree of the tree node is the degree of node D, which is 3, the degree of the tree is also 3.
write picture description here
The root of the subtree of a node is called the child of the node, and correspondingly, the node is called the parent of the child. Children of the same parents are called siblings. The ancestors of a node are all nodes on the branch from the root to the node, and any node in the subtree with a node as the root is called the descendant of the node.
The level of the node ( Level I ) is defined from the root, the root is the first level, and the children of the root are the second level. The maximum level of nodes in a tree is called the depth or height of the tree.
If the subtrees of the nodes in the tree are regarded as ordered from left to right and cannot be interchanged, the tree is called an ordered tree, otherwise it is called an unordered tree.
A forest is a set of m (m >= 0) disjoint trees. For each node in the tree, the set of its subtrees is the forest.

tree storage structure

Three representations: parent representation, child representation, child sibling representation
Parent representation : In each node, an indicator is attached to indicate the position of its parent node in the linked list, and its structure is as follows:
write picture description here
where data is the data field, which stores the data information of the node. The parent is a pointer field, which stores the index of the parent of the node in the array
TreeParent notation, with the addition of the long-child field

Child notation : Arrange the child nodes of each node and use a singly linked list as the storage structure, then n nodes have n child linked lists. If it is a leaf node, the singly linked list is empty. Then the n head pointers form a linear table, which is stored in a one-dimensional array using a sequential storage structure, as shown below
parent child notation

Child sibling notation : For any tree, the first child of its node is unique if it exists, and its right sibling is also unique if it exists. Therefore, we set two pointers to the first child of this node and the right sibling of this node. The node structure is as follows:

write picture description hereImplementation

This notation brings convenience to finding a child of a node. You only need to find the eldest child of this node through firstchild, and then find its second brother through the rightsib of the eldest child node, and then keep going until you find specific child. If you want to find parents, you can add a parent pointer field to solve the problem of quickly finding parents. The biggest benefit of this notation is that it turns a complex tree into a binary tree.

Definition and characteristics of binary tree

A binary tree is a finite set of n (n>=0) nodes, which is either an empty set (called an empty binary tree), or consists of a root node and two disjoint trees, called respectively The left subtree of the root node and the binary tree of the right subtree.

Features of binary tree

  • Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree. Note that not only two subtrees, but at most, no subtrees or one subtree is possible.
  • The left subtree and right subtree are in order, and the order cannot be arbitrarily reversed.
  • Even if a node in the tree has only one subtree, it is necessary to distinguish whether it is a left subtree or a right subtree.

A binary tree has five basic forms :

  1. empty binary tree
  2. only one root node
  3. The root node has only left subtree
  4. The root node has only the right subtree
  5. The root node has both left and right subtrees

Question: If it is a tree with three nodes, how many forms are there? If it is a binary tree with three nodes, how many forms are there? (2, 5)

Special binary tree
1. Oblique tree: A binary tree in which all nodes have only left subtrees is called a left oblique tree. A binary tree whose nodes are only right subtrees is called a right-slope tree. The two are collectively called oblique trees. Its characteristic is that each layer has only one node, and the number of nodes is the same as the depth of the binary tree.

2. Full binary tree: In a binary tree, if all branch nodes have left subtree and right subtree, and all leaves are on the same level, such a binary tree is called a full binary tree. The characteristics of a binary tree are:

  • Leaves can only appear on the bottom layer, and it is impossible to achieve balance when they appear on other layers.
  • The degree of non-leaf nodes must be 2, otherwise it is "missing arms and legs".
  • In a binary tree of the same depth, a full binary tree has the largest number of nodes and the largest number of leaves.

3. Complete binary tree: Number a binary tree with n nodes in order of layers . If the node numbered i (1=<i <=n) is in the same depth as the node numbered i in the full binary tree If the positions in the binary tree are exactly the same, the binary tree is called a complete binary tree. As shown in the following figure:
complete binary tree
The characteristics of a complete binary tree:

  • Leaf nodes can only appear on the bottom two layers; the leaves on the bottom layer must be concentrated in the left continuous position.
  • In the penultimate layer, if there is a leaf node, it must be in a continuous position on the right.
  • If the node degree is 1, then the node has only left child, that is, there is no situation where there is only right subtree.
  • A binary tree with the same number of nodes has the smallest depth of a complete binary tree.

Note the difference between "complete" and "full", a full binary tree must be a complete binary tree, but a complete binary tree is not necessarily a full binary tree.
The way to judge whether a binary tree is a complete binary tree is to look at the schematic diagram of the tree and silently number each node layer by layer according to the structure of the full binary tree. If there is a gap in the number, it means that it is not a complete binary tree, otherwise it is.

Properties of Binary Trees

  1. There are at most 2^(i -1) nodes (i >= 1 ) at level i of a binary tree. (argument by mathematical induction)
  2. A binary tree of depth k has at most 2^k - 1 nodes (k >= 1).
  3. For any binary tree T, if the number of terminal nodes is n0 and the number of nodes of degree 2 is n2, then n0 = n2 + 1. (The number of terminal nodes is actually the number of leaf nodes, and in a binary tree, except for the leaf nodes, the rest is the number of nodes with degree 1 or 2, we set n1 to be the number of nodes with degree 1. Then the tree T node The total number of points n=n0 +n1 +n2 .)
  4. The depth of a complete binary tree with n nodes is [ log2(n) ]+ 1 ( [ x ] represents the largest integer not greater than x )
  5. If, for a complete binary tree with n nodes (its depth is ( [ log2(n) ]+ 1 ), the nodes are numbered in level order (from level 1 to level [ log2(n) ]+ 1 , Each layer from left to right), for any node i (1=< i<=n), there are:
    (1) If i = 1, then node i is the root of the binary tree, without parents; if i>1 , then its parent is node [i /2]
    (2) If 2i > n, node i has no left child (node ​​i is a leaf node); otherwise, its left child is node 2i.
    (3) If If 2i+1>n, node i has no right child; otherwise, its right child is node 2i+1.

storage structure of binary tree

Two ways:

  1. Binary tree sequential storage structure: use a one-dimensional array to store the nodes in the binary tree, and the storage location of the node, that is, the subscript of the array, should reflect the logical relationship between the nodes, such as the relationship between parent and child, left and right Brotherhood, etc.
    complete binary treesequential storage
    Consider an extreme case, a right-slope tree with a depth of k, which has only k nodes, but needs to allocate 2^k - 1 storage unit space, which is obviously a waste of storage space. Therefore, sequential storage structures are generally only used for complete binary trees .

2. Binary linked list: Each node of a binary tree has at most two children. A linked list designed with one data field and two pointer fields is called a binary linked list.
write picture description herewrite picture description here
Where data is the data field, and lchild and rchild are both pointer fields, storing pointers to the left child and the right child respectively. As discussed in the tree storage structure, if necessary, you can add a pointer field pointing to its parents, which is called a tri-linked list.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324615052&siteId=291194637