Data structures-trees and two fork trees

Source: Internet
Author: User
Tags data structures
The main contents of the tree
   Tree structure: A non-linear structure, a hierarchical structure defined by branching relationships.
    main content:
         The concept of the tree and the two-fork tree, the nature of
         two fork tree storage
         Two cross-tree traverse
         clue two fork Tree
         and two fork tree Transformation
         Huffman tree (optimal tree)
the definition of a tree

A tree is a finite set of n (n≧0) nodes, and if n=0 is called an empty tree, otherwise:
⑴ has a unique root (root) node called a tree;
⑵ if n>1, the remaining nodes are divided into M (m>0) disjoint subsets T1, T2, T3 ... Tm, where each subset itself is a tree, called the root of the subtree (subtree).
This is the recursive definition of a tree, which is defined by a tree, and only one node of the tree consists only of roots. basic terms of a tree

⑴ node: A data element and several branches (pointers) that point to its subtree.
⑵ node degree (degree): The number of subtrees owned by the node.
(3) The degree of the tree: the maximum number of nodes in the tree.
(4) Leaf node: The node of the tree with a degree of 0, also known as the Terminal node.
(5) Non-leaf junction: A node with a degree of not 0, also known as a non-terminal node or branch node. Root nodes, branch nodes are also called internal nodes.
(6) Children's knot, parents ' knot, brothers ' knot.
The root of a sub-tree of a node is called the node's child node (children) or sub-node;
Accordingly, the node is the parent node (parent) of the child's node and the parents node.
All sub-nodes of the same parent node are called sibling nodes.
(7) Level: The level of the root node in the tree is 1, the remaining nodes are equal to the level of their parents ' nodes plus 1.
If a node is in the L (l≧1) layer, its sub-node is at Layer l+1.
(8) Cousin Knot: All nodes of the parent node on the same level.
In Figure 6-1 (b), the nodes E, F, G, H, I, J.
(9) The hierarchical path of a node: starting from the root node, all the nodes (with and only one) that have reached the node p.
(10) Ancestor: All nodes (except P) on the hierarchical path of the node p.
(11) Descendants (descent): Any node in a subtree that is rooted in a node.
(12) Depth of the tree (depth): The maximum hierarchy of nodes in the tree, and the height of the tree in Figure 6-1 (b) is 4.
(13) Ordered and unordered trees: if the subtree (if any) of each node in the tree is considered to be sequential from left to right (that is, it cannot be interchanged), then the tree is called an ordered tree, otherwise it is called an unordered tree.
In an ordered tree, the root of the leftmost subtree is called the first child, and the rightmost one is called the last child.
(14) Forest (forest): M (m≧0) a collection of disjoint trees.
If the root node of a tree is deleted, the remaining subtree forms the forest. abstract data type definitions for trees

ADT tree{
A data Object D:D is a collection of data elements that have the same data type.
Data Relationship R: If D is an empty set, then it is called a null tree;
......
Basic operation:
......
} the definition of the ADT tree two fork

12 definition of a fork tree
A binary tree is a finite set of n (n≥0) nodes. N=0 is called an empty tree, otherwise:
⑴ has a unique root (root) node called a tree;
⑵ if n>1, the rest of the nodes are divided into two disjoint subsets T1,t2, respectively, called the left and right subtree, and the left and right subtree are two fork trees.

The definition of a binary tree is recursive.
Description
1, the binary tree each node has a maximum of two subtrees trees, the degree of all nodes are not more than 2;
2, the Saozi right sub-tree has the order of the points, the order can not be reversed; even if a node has only one subtrees tree, it is necessary to distinguish between the left.
The binary tree plays a very important role in the tree structure. Because the binary tree structure is simple, the storage efficiency is high, the tree operation algorithm is relatively simple, and any tree can easily be transformed into a binary tree structure.
The above-mentioned terminology for trees also applies to binary trees. full two fork tree

Full two-fork tree features:
All branches of a two-fork tree have left and right subtrees;
All the leaf nodes are only present at the lowest level;
The degree of non-leaf junction must be 2;
In the same depth of the two-fork tree, the number of nodes with a maximum of two forks, the maximum number of leaf nodes (that is, the nodes on each layer to reach the maximum); Complete binary Tree

Full binary tree (complete binary trees):
A two-prong tree with n nodes is numbered in the sequence, and if the node numbered I (1<=i<=n) and the two-ary tree with the same depth is exactly the same position in the binary tree, the two-tree is called a complete binary tree.

Description
The top n nodes numbered from 1 to N in a full two-tree depth of k form a complete binary tree with a depth of K, where 2k-1≦n≦2k-1.
A complete binary tree is part of a two-fork tree, and a full two-tree is a special case of a complete binary tree.
Features of the complete binary tree:
Leaf nodes can only be present in the bottom two layers;
The lowest leaf node must focus on the left continuous position;
Second-to-last layer if there are leaf nodes, must be in the right continuous position;
If the node degree = 1, then the node is only the left child, that is, there is no only right subtree case;
The two-fork tree of the same node tree, the depth of the complete binary tree is minimal;
For any node, if the maximum level of its right subtree is L, the maximum level of its left subtree is L or l+1. The character and proof of two fork tree

Property 1: In a non-empty binary tree, there are at most 2i-1 nodes (i≧1) on the first layer.

proof: Using mathematical inductive method to prove.
    when I=1: only one root node, 21-1=20 = 1, the proposition is established.
    it is assumed that at i>1, there is a maximum of 2 (i-1)-1 nodes on the i-1 level.
    It is known from the inductive hypothesis that there are at most 2i-2 nodes on the i-1 layer. Since the degree of each node of the binary tree is maximum 2, the maximum node number on layer I is twice times the maximum node number on the first i-1 layer.
        that is,   2x2i-2=2i-1                 
certificate.
Property 2: A two-tree with a depth of K has a maximum of 2k-1 nodes (k≧1).

Proof: The maximum number of nodes for a two-fork tree with a depth of k is the sum of the maximum nodes on each layer in the two-tree.
    from the nature of 1 know, the binary tree of the 1th layer, the 2nd floor, the number of nodes on the K-level at most: 20, 21 ... 2k-1.
    ∴  Total knot points are: 20+21+ ... +2k-1=2k-1     

Property 3: For any of the binary tree, if its leaf knot point is n0, the degree of 2 of the node is N2, then n0=n2+1.
Proof: Set the two-fork tree to 1 of the node is N1, the sum of the binary tree is N, because all nodes in the binary tree are less than or equal to 2, there are: N=n0+n1+n2
   again look at the branch in the binary tree: root node, the rest of each node has a unique branch into the Set B as the total number of branches in a two-fork tree, then: n=b+1. 
   all of these branches are emitted from nodes with degrees 1 and 2, so: b=n1+2n2          
      ∴         n=b+1=n1+2n2+1 
     ∴           n0+n1+n2=n1+2n2+1 
      is          n0=n2 +1                                                 Certificate of Completion
The full binary tree depth of 4:N nodes is: ㏒2n+1.
 where symbol: x represents the largest integer not greater than X (the next integer).
            x represents the smallest integer not less than x (Upper integer).
Proof: Assuming that the depth of the complete binary tree is k, then the definition of 2 and a complete binary tree is as follows:
2k-1-1<n≦2k-1  or   2 k-1≦n<2k
    take the logarithm: k-1<=㏒2n <k  because K is an integer.
    ∴  k=㏒2n+1                   
Certificate of Completion

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.