Data Structure-tree and binary tree

Source: Internet
Author: User

Data Structure-tree and binary tree
Main contents of the tree

Tree Structure: a non-linear structure defined by the branch relationship. Main Content: Concepts of trees and Binary Trees, properties of binary tree storage of Binary Trees, traversal of Binary trees, and transformation of Binary Trees into the Huffman tree (Optimal Tree)
Tree Definition

A Tree is a finite set of n (n ≧ 0) nodes. If n = 0, it is called an empty Tree. otherwise:
(1) There is only one special Root node called the tree;
(2) If n> 1, the remaining nodes are divided into m (m> 0) subsets T1, T2, T3... Tm, where each subset is itself a tree, called the root Subtree ).
This is the recursive definition of the tree, that is, the tree is defined by the tree. The tree with only one node is composed of only the root.

Basic Terms of the tree

(1) node: a data element and its branches (pointers) pointing to its Subtrees ).
(2) degree of node (degree): number of shards of the subtree owned by the node
(3) tree degree: The maximum node degree in the tree.
(4) leaf (leaf) node: A node with a moderate value of 0 in the tree, also known as a terminal node.
(5) Non-leaf node: A node with a degree of less than 0, also known as a non-terminal node or branch node ). Besides the root node, the branch node is also called an internal node.
(6) child node, parent node, and sibling Node
The root of the subtree of a node is the child node or child node of the node;
Correspondingly, this node is the parent node or parent node of the child node.
All the child nodes of the same parent node are called sibling nodes.
(7) hierarchy: it is required that the hierarchy of the root node in the tree is 1, and that of the other nodes is equal to the hierarchy of the parent node plus 1.
If a node is in the l (l + 1) layer, its subnodes are in the l + 1 layer.
(8) cousin node: All nodes on the same layer of the parent node.
Node E, F, G, H, I, and J in Figure 6-1 (B.
(9) Hierarchical path of a node: Start from the root node and reach all the nodes (one and only one) that a node p passes through ).
(10) ancestor: All nodes on the hierarchical path of node p (except for node p ).
(11) descendant (descent): Any node in the subtree rooted in a certain node.
(12) tree depth (depth): The maximum hierarchy value of the node in the tree. In Figure 6-1 (B), the height of the tree is 4.
(13) Ordered Tree and disordered tree: If the subtree (if any) at each node in the tree is regarded as ordered from left to right (I .e., they cannot be exchanged ), the tree is called an Ordered Tree, otherwise it is called an unordered tree.
In the ordered tree, the root of the leftmost subtree is called the first child, and the rightmost is called the last child.
(14) forest (forest): a set of trees that do not meet each other in m (m 0.
If the root node of a tree is deleted, the remaining subtree forms a forest.

Tree definition of abstract Data Types

ADT Tree {
Data Object D: D is a collection of data elements of the same data type.
Data Relationship R: If D is an empty set, it is called an empty tree;
......
Basic operations:
......
} ADT Tree

Binary Tree Definition

1. Binary Tree Definition
A binary tree is a finite set of n (n ≥ 0) nodes. If n = 0, it is called an empty tree; otherwise:
(1) There is only one special Root node called the tree;
(2) If n> 1, the remaining nodes are divided into two subsets T1 and T2, which are called left and right subtree respectively, the left and right subtree are binary trees.

Binary Tree is defined as recursion.
Note:
1. Each node of a binary tree can have a maximum of two Subtrees. The degree of each node cannot exceed 2;
2. The left and right subtree have order, and the order cannot be reversed. Even if a node has only one subtree, it must be left and right.
Binary trees play an important role in the tree structure. Because the binary tree structure is simple and the storage efficiency is high, the tree operation algorithm is relatively simple, and any tree can be easily converted into a binary tree structure.
The related tree terms introduced above also apply to binary trees.

Full Binary Tree

Full Binary Tree features:
All branch nodes of a full binary tree have left and right Subtrees;
All leaf nodes can only appear at the bottom layer;
The degree of non-leaf nodes must be 2;
In a binary tree of the same depth, the full binary tree has the maximum number of knots and the maximum number of leaf nodes (that is, the maximum number of knots on each layer );

Full Binary Tree

Complete Binary Tree ):
ID a binary tree with n knots by sequence. If the number is I (1 <= I <= n) this binary tree is called a Complete Binary Tree when its position is exactly the same as that of the node numbered I in the same depth as that in the binary tree.

Note:
The first n nodes numbered from 1 to n in the full binary tree with a depth of k constitute a complete binary tree with a depth of k. 2k-1 then n then 2k-1.
A full Binary Tree is a part of a full binary tree, and a full Binary Tree is a special case of a full binary tree.
Full Binary Tree features:
Leaf nodes can only appear at the bottom two layers;
The bottom-layer leaf node must be concentrated on the Left Continuous position;
If the penultimate layer has a leaf node, it must be in a continuous position on the right;
If the node degree is 1, the node has only the left child, that is, there is no right subtree;
Binary Trees of the same node tree have the minimum depth of the full binary tree;
For any node, if the maximum level of the right subtree is l, the maximum level of the Left subtree is l or l + 1.

Binary Tree nature and proof
Property 1: In a non-null Binary Tree, layer I has at most 2i-1 nodes (I segment 1 ). Proof: It is proved by mathematical induction. When I = 1: There is only one root node, 21-1 = 20 = 1, and the proposition is true. It is assumed that for I> 1, there are at most 2 (I-1)-1 nodes on the I-1 layer. According to the induction hypothesis, there are at most 2i-2 nodes on the I-1 layer. Because the degree of each node of the binary tree is 2, the maximum number of knots on the I layer is 2 times the maximum number of knots on the I-1 layer. 2 × 2i-2 = 2i-1
Property 2: A binary tree with a depth of k has at most 2k-1 nodes (k segment 1 ). Proof: the maximum number of knots of a binary tree with a depth of k is the sum of the maximum knots on each layer of the binary tree. According to the Nature 1, what are the 1st and 2nd layers of Binary Trees? The number of knots on the k layer can be at most 20, 21... 2k-1. The total number of worker nodes can be at most 20 + 21 +... + 2k-1 = completion of 2k-1 Certificate
Property 3: For any binary tree, if the number of knots in the leaf is n0 and the number of knots in the degree of 2 is n2, n0 = n2 + 1. Proof: Set the knots with a moderate value of 1 in a binary tree to n1, and the sum points in the binary tree to N. Because all the knots in the binary tree are less than or equal to 2, there are: N = n0 + n1 + n2 then look at the number of branches in the binary tree: apart from the root node, each of the other nodes has a unique branch to enter. If B is set to the total number of branches in the binary tree, there are: N = B + 1. All these branches are sent from nodes with degrees 1 and 2. Therefore, B = n1 + 2? N2 limit N = B + 1 = n1 + 2? N2 + 1 running n0 + n1 + n2 = n1 + 2? N2 + 1: n0 = n2 + 1 certificate
Nature 4: the depth of the Complete Binary Tree of n nodes is :? ? 2n? + 1. Symbol :? X? The maximum integer (lower integer) not greater than x ). ? X? Represents the smallest integer (upper integer) not less than x ). Proof: assume that the depth of the Complete Binary Tree is k, according to the definition of Nature 2 and the full Binary Tree: 2k-1-1 <n 1_2k-1 or 2 K-1 127n <2 k to obtain the logarithm: k-1 <= 2n <k because k is an integer. Required k =? ? 2n? + 1 certificate
Property 5: If a Complete Binary Tree with n knots (depth is? ? 2n? + 1) node by layer (from layer 1st to? ? 2n? + Layer 1) sequentially numbered from left to right. For nodes numbered I (1 when I bought n): (1) if I = 1: node I is the root of a binary tree without parent nodes. If I> 1, what is the ID of the parent node? I/2? . (2) If 2i> n: Then node I is a leaf node without a left child. Otherwise, its left child node number is 2i. (3) If 2i + 1> n: Then node I has no right child; otherwise, its right child node number is 2i + 1. Proof: It is proved by mathematical induction. First, we will prove (2) and (3), and then export (1) from (2) and (3. When I = 1, the full Binary Tree defines that the number of the left child of node I is 2, and the number of the right child is 3. If 2> n, there is no node 2 in the binary tree, indicating that the left child of node I does not exist. If 3> n, there is no node 3 in the binary tree, indicating that the right child of node I does not exist. Assume that (2) and (3) are valid for the node number j (1 then j when I. That is: ◆ when 2j limit n: The left child id of node j is 2j; When 2j> n, the left child node of node j does not exist. ◆ When 2j + 1 then n: The right child id of node j is 2j + 1; When 2j + 1> n, the right child node of node j does not exist.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.