Java data Structure-tree and tree storage structure

Source: Internet
Author: User

The definition of a tree: a finite set of n (n>=0) nodes.

    1. N=0 is called an empty tree.
    2. N!=0 is a non-empty tree, there is only one specific node-root; When n>1, other nodes can be divided into m (m>0) disjoint finite set T1~TM, each of which is itself a tree, and is called the root subtree.

Some basic terms of the tree:

    1. node of a tree: consists of a data element and several branches pointing to its subtree.
    2. Node Degree: The number of subtrees owned by the node (that is, the number of branches) is called the degree of the node.
    3. Leaf nodes: Nodes with a degree of 0 are called leaf nodes, or terminal nodes.
    4. Branch nodes: Nodes with a degree of not 0 are called branch nodes, or are called non-terminal nodes. The nodes of a tree are composed of leaf nodes and branch nodes.
    5. Children, parents, brothers: the root node of a node in a tree is called the child of the node; the knot is called the parent of the child's node, and the child with the same parent is called a sibling.
    6. Path, path length: If a cluster of nodes in a tree n1,n2,..., NK has the following relationship: The node NI is Ni+1 's parent node (i is 1~k, not equal to the value of K), the N1,n2,..., nk is called a path from N1 to NK. The path length refers to the number of edges passing through the path, so the length of the path is k-1.
    7. Ancestors, descendants: In a tree, if there is a path from node m to node N, then M is called the ancestor of N, and N is the descendant of M.
    8. Number of nodes: the number of layers of the root node of the specified tree is 1, the number of layers of the remaining nodes equals the number of layers of its parent node plus 1.
    9. Depth (height) of the tree: the maximum number of layers in all nodes in the tree is called the depth of the tree.
    10. The degree of the tree: the maximum value of each node in the tree is called the degree of the tree.
    11. Ordered tree, unordered tree: If the subtree of a tree node is ordered from left to right, it cannot be exchanged, it is called an orderly tree, and the other is an unordered tree.
    12. Forest: M (m>=0) a collection of disjoint trees.
    13. Isomorphism: For two trees, if the nodes are properly renamed, the two trees can be completely equal (the node corresponds to the same, the nodal relationship is equal), called the Two trees isomorphic.
    14. Sequence Number: The book's nodes according to the order from the upper to the lower, the same level from left to right, the number of the natural number 1.

comparison of linear structures and tree structures:

the storage structure of the tree:
Parent notation:
(use a contiguous set of spaces to store the nodes of the tree)
In each node, an indicator is attached indicating its parent node to the position in the linked list.
The structure of the node is as follows:

The following shows the parent notation: the pointer field of the agreed root node is set to-1;

This method can access the parent node of this node as long as it spends O (1), but it needs to traverse the whole tree to find its child node. needs to be improved;
Improved version one: increase the left child (eldest son) node pointer field, as follows:

Improved version two: increase the right sibling node pointer field, as follows:

Can be improved according to requirements, flexible design of storage structure.

Multi-Link Table notation:
(implemented with a multi-linked list)
Each node has multiple pointers, where each pointer points to the root node of a subtrees tree, which is called a multilink notation.
Programme one:
The number of pointer fields equals the degree of the tree, and the structure is as follows:

For example, the degree of the tree is 3, so the number of pointer fields is 3, the method is implemented as follows:

Explanation: There is a waste of space when the degree of each node in the tree varies greatly, and the space is fully utilized if the degree of each node varies very little.
Based on the idea of space on demand, we have a second set of options.
Scenario Two:
The number of pointer fields per node is equal to the degree of the node, and the number of node pointer fields is stored by setting a position; The structure is as follows:

The diagram above can then draw the implementation of the scheme:

Explanation: This method is a waste of space, but due to the different structure of the linked list and the need to maintain the value of the degree of the node, the computation is in the time consumption.

Child notation:
Use an array to hold the nodes of an array, and use a single linked list to hold the node's child nodes.
The illustration above shows the following structure:

Explain:
The structure of the table header array; Data is a field that stores the data information of the node; FirstChild is the head pointer that stores the node's child linked list's head pointer.
The structure of a child's linked list: Children is a data field that stores the subscript of a node in an array of table headers; Next is a pointer field that stores a pointer to the next child node of a node.

Improved version: Child parental representation:
Adds a parent field to the table header array that holds pointers to parent nodes.

Child Brother representation:
The structure is as follows:

Explanation: Data is a domain, FirstChild is the pointer field, storing the node's eldest son's storage address, Rightsib is the pointer field, storing the node's right sibling node storage address.
The tree described in the above diagram is implemented as follows:

If you want to find parents, you can add a parent pointer.

Follow up on the binary tree.

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.

Java data Structure-tree and tree storage structure

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.