On data Structure-tree

Source: Internet
Author: User

A tree is a data structure in which an element can have two or more data elements, with a one-to-many feature that uses a tree structure to store files.

the overview of the treeRead

    1. Node Degree: The number of child nodes. For example, there are 3 sub-nodes in Node 1, and the degree of Node 1 is 3.
    2. The degree of the tree: the degree of the tree is equal to the highest value of all node degrees. The node has the highest degree of 3, and the tree has a degree of 3.
    3. Leaf nodes: Nodes with a degree of 0, i.e. nodes without sub-nodes. For example: Medium 3,5,6,7,9,10.
    4. Branch node: A node that is not 0, except for a node that is not a leaf node. For example, the branch node of the tree above is 1,2,4,8.
    5. Internal nodes: The root node is removed except for the root node and the leaf node, or on the basis of the branch node. For example: The inner node of the tree above is 2,4,8.
    6. Parent nodes, sub-nodes, sibling nodes: parent, child, and sibling nodes are relative. For example: Node 1 is the parent node of the junction 2,3,4, the node 2,3,4 is also the node 1 of the child nodes, node 2,3,4 is also a sibling knot point.
    7. Level: We have the table in the figure, the root is the first layer, the root of the child is the second layer, and so on, if a node in the layer I, then its children node in the i+1 layer. 0 layer, 1 layer, 2 layer, 3 layer.

    8. Depth of tree: The largest layer of nodes in the tree, with a depth of 4 (4 layers).

Summary point = +1 of all degree nodes (should be parent node)

Storage structure of the tree 1 parent notation (sequential storage structure)

Pros: The parent (tree, X) action can be implemented in constant time

Cons: Need to traverse the entire structure when finding the child of the knot

A contiguous set of storage spaces is used to store the nodes of the tree, and an indicator (integer field) is appended to each node to indicate the position of the parent node (subscript value).

2 Child linked list notation

Each node in the tree has multiple pointer fields, and each pointer points to its root node of a subtrees tree. There are two kinds of node structures.

⑴ fixed-length node structure

The number of pointer fields is the degree of the tree.

Its characteristic is: the structure of the list is simple, but the waste of the pointer domain is obvious. The node structure is shown in (a). There must be N (k-1) +1 null pointer fields in a tree with n nodes and a degree of K.

⑵ Indefinite long node structure

Each node in the tree has a different number of pointer fields, which is the degree of the node, as shown in (b). There is no extra pointer field, but the operation is inconvenient.

⑶ Composite Linked list structure

For each node in the tree, its child nodes are represented by a single-linked list of lead nodes, and the structure of the table nodes and head nodes is shown.

n nodes of the tree have n (children) single-linked list (the leaf node of the child linked list is empty), and n the node is formed a linear table and the sequential storage structure representation.

3 Children Brother Representation (binary tree representation)

A two-fork list is used as the storage structure for the tree.

Two pointer fields: points to the first child node and the next sibling node, respectively.

The traversal of a tree

As defined by the tree structure, there are two ways to traverse a tree.

⑴ Sequence Traversal: Accesses the root node first, then sequentially iterates through each subtrees tree. Tree, the sequence of first order traversal is: ABCDEFGIJHK

⑵ post-order traversal: sequential traversal of each subtrees tree, and then access the root node. Tree, the order of sequential traversal is: Cdbfgijheka

Description

The first-order traversal of a tree is essentially the same as the first-order traversal of a two-fork tree after the tree is converted to a binary tree.

The post-order traversal of a tree is essentially the same as converting a tree into a binary tree after a two-fork tree.

2 Traversal of the forest

Set F={T1, T2,?, Tn} is a forest and there are two ways to traverse F.

⑴ Sequential traversal: Iterates through each tree in the first sequence, sequentially traversing the tree.

⑵ sequence traversal: Iterates through each tree in F in the same order as it traverses the tree sequentially.

A common tree

The tree structure includes: Two-fork search tree (binary sort tree), Balanced binary tree (AVL tree), red-black tree, B-tree, + + Tree, dictionary tree (trie tree), suffix tree, generalized suffix tree.

1. Binary search tree (binary sort tree)

(Figure A)

A binary lookup tree is a dynamic lookup table (figure A) with these properties:
(1) If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
(2) if its right subtree is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
(3) The other left and right sub-trees were also two-fork search trees;
(4) Binary search tree is a dynamic lookup table, in the process of finding to add and remove the corresponding elements, in these operations need to maintain the binary search tree above the nature.

2. Balanced binary tree (AVL tree)

(Figure B)

A two-fork search tree with the same nodes can have different patterns, and the average lookup length of a binary lookup tree is related to the depth of the tree, so a tree with the smallest average length is needed to find a balanced binary tree (Figure B) with the following properties:
(1) Either an empty tree, or the absolute value of the difference between the left and right sub-trees of the root node is not more than 1;
(2) The left and right sub-trees are also balanced binary trees;
(3) The balance factor of a binary tree node is defined as the depth of the left subtree of the node minus the depth of the right subtree. The equilibrium factor for all nodes of the balanced binary tree may be only -1,0,1.

3. Red and black Trees

(Figure C)

The red-black tree is a self-balancing binary tree that, on the basis of a balanced binary tree, adds a color attribute to each node, and the node's color can only be red or black. Has the following properties:
(1) The root node can only be black;
(2) All the leaf nodes in the red and black tree are then connected to the left and right two empty nodes, so that the algorithm consistency can be maintained, and all the empty nodes are black;
(3) The other nodes are either red or black, the parent node of the red node and the left and right child nodes are black, and the black phase;
(4) In any subtrees tree, the number of black nodes that go down the path from the root node to the empty node is the same, thus guaranteeing a balanced binary tree.

4, B-Tree

(Figure D)

B-Tree is a balanced multi-path lookup tree, which is useful in file systems. An M-Order B-tree (Figure D is a 4-order B-Tree) having the following properties:
(1) Each node in the tree has a maximum of M subtrees tree;
(2) The Joghen node is not a leaf node, then there are at least 2 subtrees trees;
(3) All non-terminal nodes outside the root node have at least subtrees trees;
(4) The information structure in each node is (A0,K1,A1,K2 ... Kn,an), where n denotes the number of keywords, ki is the keyword, and AI is the pointer;
(5) All leaf nodes appear at the same level, without any information, and also in order to maintain the consistency of the algorithm.

5. B + Tree

(Figure E)

The B + number is a variant of S-tree, and the difference between it and the tree is that (Figure E is a 3-order B + Tree):
(1) The nodes with n subtrees tree contain n keywords;
(2) All the leaf nodes contain the information of all the keywords, and pointers to these key records, and the leaf node itself by the keyword size from small to large order link;
(3) All non-terminal nodes can be regarded as the index part, the node contains only the largest (or smallest) keywords in its subtree (root node), and all B + trees are more like an index order table;
(4) To find operations on B + trees, one is to search from the minimum keyword, the second is to start from the root node, random search.

6. Dictionary tree (trie tree)

(Figure F)

A dictionary tree is a tree-shaped structure that holds a large number of strings. In order to facilitate the statistics and search of strings, often by the search engine system for text frequency statistics. Its advantages are: the use of the common prefix of the string to save storage space, minimize unnecessary string comparisons, query efficiency is higher than the hash table. Has the following characteristics (Figure F):
(1) The root node is empty;
(2) root node, each node contains one character;
(3) from the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node.
(4) Each string in the process of building a dictionary with a distinguished terminator, to avoid a short string is exactly a long string prefix and drowned.

7. Suffix Tree

The suffix tree is a dictionary tree that consists of all suffixes of a string. The specific content has been mentioned in the previous chapters.

8. Generalized suffix tree

A generalized suffix tree is a dictionary tree of all suffixes of several strings, and the same terminator for all suffixes of each string, different terminator of different strings.

Reference links

JavaScript implementation data structures: trees and forests

Data structure and algorithm (2) tree and two-fork tree

On the various trees in data structure

On data Structure-tree

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.