Big story data Structure reading Note series (vi) tree < prev >

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/u010194538/article/details/51212759

This chapter content is more, divided into the upper and the next 2 articles:

Chapter 6 tree definition of 6.2 tree

What we've been talking about is a one-off linear structure, but in reality there are a couple of situations that need to be dealt with, so we need to look at a one-to-many data structure-the "tree".

TreeTree) isN(N≥0) A finite set of nodes. n=0is called an empty tree. In any tree that is not empty: (1)There is only one specific node called root (root) .;(2) WhenN>1, the remaining nodes can be divided intom(m>0) ANon-intersectingThe finite setT1,T2、......、Tm, each of which is itself a tree, and is called the root of the subtree (subtree),6-2-1is shown.

6.2.1 node classification

The number of subtree owned by a node is called the degree of node (de-gree). A node with a degree of 0 is called a leaf node (leaf) or terminal node, and a node with a degree of 0 is called a non-terminal node or branch node. Root nodes, branch nodes are also called internal nodes. The degree of the tree is the maximum of the degree of each node within the tree. as shown in 6-2-4, because the maximum value of the node's degree is the degree of node D , which is 3, so the degree of the tree is also 3.

relationship between 6.2.2 nodes

The root of the node's subtree is called the child of that node, and accordingly, the node is called the child's parent (parent). The same parent's children are called Brothers (Sibling). The ancestor of a node is all nodes from the root to the branch of the node. So forHspeaking,D,B,Ais the ancestor of it. Conversely, any node in a subtree that is rooted in a node is called a descendant of that node. B's descendants haveD,G,H,I,6-2-5is shown.

Other related concepts of 6.2.3 tree

The level of the nodeisdefined from the root, and the root is the first layer, and the child of the root is the second layer. If a node is in the firstLlayer, then its subtree is in the firstl+1layer. Itsparents on the same level of the knot are cousins of each other。 Obviously the figure6-2-6in theD,E,Fare cousins, andG,H,Iwith theJis also a cousin. The maximum level of nodes in a tree is called the depth of the tree (Depth) or height ., the depth of the current tree is4.

If the subtree of the nodes in the tree is considered to be sequential from left to right and cannot be interchanged, the tree is called an ordered tree, otherwise it is called an unordered tree.

The Forest (Forest) is a Collection ofnon-intersecting trees of M (m≥0).

Compared to the structure of the linear table and the tree, they differ greatly.

storage structure of 6.4 tree

Simple sequential storage structures do not meet the requirements of tree implementation. A node in a tree can have more than one child, which means that regardless of the order in which all nodes in the tree are stored in the array, the node's storage location cannot directly reflect the logical relationship.

However, the full use of sequential storage and chain storage structure, can fully implement the storage structure of the tree representation. Here we introduce three different representations: parental representations, child representations, child brother representations.

6.4.1Parental representation

We people may because of a variety of reasons, no children, so is a person must have parents. The structure of the tree is no exception, except for the root node, each of the remaining nodes, it does not necessarily have children, but there must be and only one parent.

In each node, an indicator is attached indicating the position of its parent node in the array.

Data

Parent

where data is a field that stores data about the node. the parent is a pointer field that stores the subscripts of the parent of the node in the array. Since the root node has no parents, we agree that the root node's Location field is set to -1. The tree structure in 6-4-1 and the parent representation of the tree in table 6-4-2 are shown.

Advantage: with such a storage structure, we can easily find its parent node based on the parent pointer of the node.

Cons: But if we want to know what the child of the knot is, I'm sorry, just go through the structure.

This is really a problem, can you improve it? Of course. We add a node to the leftmost child's domain and may as well call it the firstborn domain , so that the child can easily get the knot. If there is no child node, the eldest son field is set to -1, as shown in table 6-4-3 .

Another problem scenario, we are concerned about the relationship between the brothers, the parents of the expression can not reflect such a relationship, then we do? Well, you can add a right sibling to reflect the Brotherhood, that is, every node if it exists right brother, then record the right brother's subscript. Similarly, if the right sibling does not exist, it is assigned a value of -1, as shown in table 6-4-4 .

6.4.2 Child notation

Each node has multiple pointer fields, where each pointer points to the root node of a subtrees tree, and we call this method a multi-link table notation.

Child notation : each node of the child node line up, with a single-linked list as the storage structure, n nodes have n children linked list, if it is a leaf node, this single linked list is empty. The N-head pointer then forms a linear table, which is stored in a one-dimensional array with sequential storage structure, as shown in6-4-4 .

6.4.3 children Brother representation (binary tree)

Any tree, the first child of its node if existence is the only one, its right brother if existence is also unique. Therefore, we set two pointers to the first child of the node and the right sibling of the node, respectively.

Data

FirstChild

Rightsib

Where data is the domain, FirstChild is the pointer field, storing the node's first child node storage address, Right-sib is the pointer field, storing the node's right sibling node storage address.

6.5 Two definition of a fork tree

it's classic. binary Lookup algorithm : I have already written a 100 positive integer number in the paper, please try to figure out which one I wrote? Note that you cannot guess more than 7, and my answer will only tell you that it is "big" or "small".

But for a situation where there are two outcomes at a certain stage, such as Open and Close,0 and 1, True and false, are all suitable for modeling with a tree structure, which is a very special tree structure called Two fork Tree .

A binary tree is a finite set of n(n>=0) nodes, which is either an empty set or a two-fork tree consisting of a root node and two Saozi right subtrees, called the root node, which are disjoint from each other.

 6.5.1 two fork tree features

two characteristics of the fork tree are:

1. every node. most There are two subtrees trees . .

2. Saozi Right Sub-tree is there Order of , the order cannot be reversed arbitrarily.

3. even if a node in a tree has only one subtrees tree, it is Zuozi or right subtree to distinguish it.

6.5.2 Special Two-fork tree

1) all the nodes are only Zuozi two forks tree called Left oblique tree. All nodes are only right subtree of two fork tree called Right oblique tree. The two are collectively referred to as oblique trees . Oblique tree has a very obvious feature, that is, each layer has only one node, the number of nodes and the depth of the two-fork tree is the same.

2) in a binary tree, if all branch nodes have Saozi right subtree, and all leaves are on the same layer, such a two-fork tree is called full two fork tree .

only each node has left and right sub-tree, not a full two fork tree, but also must be all the leaves are on the same layer, which will achieve the whole tree balance. As a result, the two-fork tree is characterized by: (1) leaves can only be present on the next level. There is no way to strike a balance on the other floor. (2) The degree of non-leaf junction must be 2. Otherwise it is "lack of arms and legs". (3) In the same depth of the two-fork tree, the number of nodes full of two forks , the largest number of leaves.

3) a two-prong tree with n nodes is numbered by sequence, if the node numbered I (1≤i≤n) is in the same depth as the full two-tree The nodes numbered I are in exactly the same position in the binary tree, and the binary tree is called completely binary tree .

difference: first of all literally to distinguish between "full" and "full" difference, full two fork tree must be a complete binary tree, but the complete binary tree is not necessarily full.

Judgment: determine whether a binary tree is a complete binary tree approach, that is to look at the tree, the heart silently to each node in accordance with the structure of the two-tree-level sequence number, if the number appears empty file, it is not a complete binary tree, otherwise is.

6.6 Two The nature of the fork tree

Property 1: There is a maximum of 2 on the first layer of the binary tree i-1 nodes (i≥1).

Property 2: a two-tree depth of K has a maximum of 2 k -1 nodes (k≥1).

Property 3: Yes any binary tree T, if its terminal node number is n 0 , the number of nodes with a degree of 2 is n 2 , then n 0 =n 2 +1.

Compared to the 6-6-1 example, the total number of nodes is 10, it is composed of a, B, C, D and other degrees of 2 nodes, F, G, H, I, J and so on 0 of the leaf node and E, the degree of 1 nodes. The sum is 4+1+5=10.

Property 4: The depth of a full binary tree with n nodes is |log2n+1| (|x| represents the largest integer not greater than X).

Property 5: If the node of a complete binary tree with n nodes (its depth is) is numbered in sequence (from layer 1th to layer, each layer from left to right), any node I (1≤i≤n) is:

1. If i=1, then node i is the root of the two fork tree, no parent, and if i>1, the parent is the node.

2. If 2i>n, then node I has no left child (node I is leaf node); otherwise its left child is node 2i.

3. If 2i+1>n, then node I has no right child, otherwise the right child is a node 2i+1.

Big story data Structure reading Note series (vi) tree < prev >

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.