Basic knowledge of binary trees

The written test question encountered a binary tree problem, learn it. The content is transferred from: https://blog.csdn.net/xiaoquantouer/article/details/65631708

The definition of a tree

A tree is a data structure, which consists of n (n>=1) finite nodes to form a set with a hierarchical relationship.

 

 

 

Trees have 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 a subtree, 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.

 

 

 

2. Properties of binary tree

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

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

Property 3: The height of a binary tree with n nodes is at least (log 2 n)+1

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

 

3. Proof of property 4

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

Proof: Since the degree of all nodes in a binary tree is not greater than 2, let n 0 represent the number of nodes with degree 0, n 1 represent the number of nodes with degree 1, and n 2 represent the number of nodes with degree 2 number. The sum of the three types of nodes is the number of summary points, so we can get: n=n 0 +n 1 +n (1)

The second equation can be obtained from the relationship between degrees: n=n 0 *0+n 1 *1+n 2 *2+1 that is n=n 1 +2n 2 +1 (2)

Combining (1) (2) together gives n 0 =n 2 +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 2 h -1 nodes is called a full binary tree

 

 

 

2. Complete binary tree

Definition : 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.

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: n 0 =n 2 +1, bring it into 768=n 0 +n 1 +n 2 to get : 768=n 1 +2n 2 +1, because the complete binary tree is a node with degree 1 The number is either 0 or 1, then put n 1 =0 or 1 into the formula, and it is easy to find that n 1 =1 meets the conditions. So it is calculated that n 2 =383, so the number of leaf nodes n 0 =n 2 +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]

 

 

 

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=324771676&siteId=291194637