Data Structure (Binary Tree)

Related terms:

The node of the tree: contains a data element and several branches pointing to the subtree;
Child node: left child, right child
Parent node: node B is the child of node A, then node A is the parent of node B;
Brother node: the child node of the same parent; Cousin node: the node on the same layer;
Ancestor node: parent node, parent node of parent node, parent node of parent node, parent node of parent node, etc.
Node layer: The layer of the root node is defined as 1; the child of the root is the second layer node, and so on;
The depth of the tree: the largest node level in the tree
Degree of a node: the number of subtrees of a node
Degree of the tree: The maximum degree of a node in the tree.
Leaf node: also called terminal node, is a node with degree 0;
Branch node: a node whose degree is not 0;
Ordered tree: a tree with ordered subtrees, such as a family tree;
Unordered tree: does not consider the order of subtrees;

1. Definition

  A binary tree is a tree structure with at most two subtrees per node. Subtrees are divided into left and right, and the order cannot be reversed. They are generally called "left subtree" and "right subtree". Binary trees are often used to implement binary search and binary heap.

Properties: The i-th level of a binary tree has at most 2^{i-1} nodes; a binary tree with a depth of k has at most 2^k-1 nodes; for any binary tree T, the number of nodes with degree 0 is close to The number of nodes with a degree of 2 is 1 more. (A degree of 2 means that there are two subtrees)

The proof is as follows:

       Because the number of all nodes in the binary tree is not greater than 2, the total number of nodes is n=n0+n1+n2 (1) and because the nodes with degree 1 and degree 2 have 1 subtree and 2 subtrees respectively, so, In the binary tree, there are n (child) nodes in the subtrees = n1+2n2 In the binary tree, only the root node is not a subtree node, so the total number of nodes in the binary tree is n=n(child)+1, that is, n=n1+2n2+1 (2 ) combined with (1) and (2) to get n0=n2+1

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)

The depth of a complete binary tree with n nodes is (Note: [ ] means round down)

2. Type

(1) Complete binary tree - If the height of the binary tree is set to be h, the number of nodes in other layers (1 ~ h-1) has reached the maximum number except for the h-th layer, and the h-th layer has leaf nodes and leaves Nodes are arranged in order from left to right , which is a 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.
 
(2) Full binary tree —a binary tree in which every node except the leaf node has left and right cotyledons and the leaf nodes are at the bottom. (I understand that the nodes are all filled)
 
(3) Balanced binary tree - Balanced binary tree is also known as AVL tree (different from AVL algorithm), it is a binary sorting tree, and has the following properties: it is an empty tree or its left and right two subtrees The absolute value of the height difference does not exceed 1, and the left and right subtrees are a balanced binary tree .
(4) Binary search tree: Binary search tree is also known as 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]

In a binary search tree:

(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

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Analysis:
A binary tree is not a special case of a tree, and although it shares many similarities with a tree, there are two main differences between a tree and a binary tree:
1. There is no limit to the maximum degree of nodes in the tree, while the maximum degree of a binary tree node is 2;
2. The nodes of a tree are not divided into left and right, while the nodes of a binary tree are divided into left and right.
 

3. Convenience order

       (the order of visiting the root node)

  1. preorder traversal
  2. Inorder traversal
  3. post-order traversal
  4. Hierarchical convenience: that is, access according to the hierarchy, usually done with a queue. Visit the root, visit the children, and then visit the children of the children (the further down the hierarchy is) (the level of the two children is the same)

Guess you like

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