[Data structure]--------Summary of basic knowledge points of binary tree

The definition of a tree

A tree is a data structure, which is a collection of hierarchical relationships consisting of n (n>=1) finite nodes.

write picture description here
The tree has the following characteristics:
(1) Each node has zero or more child nodes

(2) A node without a parent node is called a root node

(3) Each non-root node has one and only one parent node

(4) Except for the root node, each child node can be divided into multiple disjoint subtrees.

The basic terms for trees are:
if a node has subtrees, then the node is called the "parent" of the root of the subtree, and the root of the subtree is called the "child" of the node. Nodes with the same parents are "brothers" to each other. Any node on all subtrees of a node is a descendant of that node. All nodes on the path from the root node to a node are ancestors of that node.

Degree of a node: the number of subtrees a node has

Leaf nodes: nodes with degree 0

Branch node: a node whose degree is not 0

The degree of the tree: the maximum degree of the nodes in the tree

Level: The level of the root node is 1, and the level of the remaining nodes is equal to the level of the parent node of this node plus 1

The height of the tree: the maximum level of nodes in the tree

Forest: consists of 0 or more disjoint trees. Add a root to the forest and the forest becomes a tree; remove the root and the tree becomes a forest.

2. Binary tree

1. Definition of
binary tree A binary tree is a tree structure with at most two subtrees per node. It has five basic forms: a binary tree can be an empty set; the root can have an empty left subtree or a right subtree; or both left and right subtrees can be empty.
write picture description here

2. Definition of binary tree
Property 1: The number of nodes on the i-th layer of a binary tree is at most 2i-1 (i>=1)

Property 2: A binary tree with depth k has at most 2k-1 nodes (k>=1)

Property 3: The height of a binary tree containing n nodes is at least (log2n)+1

Property 4: In any binary tree, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, then n0=n2+1

3. Proof of property 4

Property 4: In any binary tree, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, then n0=n2+1

Proof: Since the degree of all nodes in the binary tree is not greater than 2, let n0 represent the number of nodes with degree 0, n1 represent the number of nodes with degree 1, and n2 represent the number of nodes with degree 2. The sum of the three types of nodes is the number of summary points, so we can get: n=n0+n1+n2 (1)
The second equation can be obtained from the relationship between degrees: n=n0*0+n1*1 +n2*2+1 means n=n1+2n2+1 (2)
Combining (1) (2) together can get n0=n2+1

Three, full binary tree, complete binary tree and binary search tree

1. Full binary tree

Definition: A binary tree with height h and consisting of 2h-1 nodes is called a full binary tree.

write picture description here

2. Definition of complete binary tree : In a binary tree, only the degree of the nodes in the bottom two layers can be less than 2, and the leaf nodes of the bottom layer are concentrated in several positions to the left. Such a binary tree is called a complete binary tree.
write picture description here
Features: Leaf nodes can only appear in the lowermost and sub-lower layers, and the lowermost leaf nodes are concentrated in the left part of the tree. Obviously, a full binary tree must be a complete binary tree, and a complete binary tree is not necessarily a full binary tree.

Interview question : If the total number of nodes in a complete binary tree is 768, find the number of leaf nodes.
From the properties of the binary tree: n0=n2+1, bring it into 768=n0+n1+n2 to get: 768=n1+2n2+1, because the number of nodes with a complete binary tree degree of 1 is either 0 or If it is 1, then substitute n1=0 or 1 into the formula, and it is easy to find that n1=1 meets the conditions. So it is calculated that n2=383, so the number of leaf nodes is n0=n2+1=384.

Summary rule: If the total number of nodes in a complete binary tree is n, then the leaf nodes are equal to n/2 (when n is even) or (n+1)/2 (when n is odd)

3. Binary search tree

Definition: A binary search tree is also known as a binary search tree. Let x be a node in the binary search tree, node x contains the keyword key, and the key value of node x is calculated as key[x]. If y is a node in the left subtree of x, then key[y]<=key[x]; if y is a node in the right subtree of x, then key[y]>=key[x]
write picture description here

Find tree species in binary:

(1) If the left subtree of any node is not empty, the value of all nodes on the left subtree is less than the value of its root node.

(2) If the right subtree of any node is not empty, then the value of all nodes on the right subtree is greater than the value of its root node.

(3) The left and right subtrees of any node are also binary search trees, respectively.

(4) There is no node with the same key value.

Guess you like

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