Data Structures-trees (tree)

Source: Internet
Author: User

This article first introduces some basic concepts about trees.

Common arrays, lists, stacks, and queues are linear structures that are slow to access when storing large amounts of data, while trees are a nonlinear structure that reduces access time complexity to O (Logn).

A structure similar to a tree in life is a genealogy. Each family had an ancestor, and the ancestors had several children, and each child continued to have children, constantly handing over to the youngest generation.

terminology of the tree:
(1) One of the most basic data in a tree is called a node, which corresponds to each member of the genealogy.
(2) Each tree has a root node (root), which corresponds to the oldest ancestor in the genealogy, and the whole tree has only one root. You can also continue to recursively define a node as a root node and form a subtrees tree (sub-tree) down the node.
(3) The youngest generation of the whole tree, called the leaf nodeof the tree in the tree. The nodes between the root node and the leaf node are called internal nodes (internal node).
(4) Each parent has several children, each with his father and his grandparents. Fathers are called parent nodes (parents), and children are called Child nodes (child). Each parent node can correspond to one or more child nodes, and a child node may have only one parent node. A node with the same parent node is called a sibling node (siblings).
(5) Degrees: The number of child nodes per node is the degree of this node. The maximum degree of all nodes in a tree is the degree of the tree.
(6) Starting from the root node of a tree is the first layer of a tree (sometimes starting from the No. 0 layer), its child nodes are the 2nd layer, and so on. The depth of the node is accumulated from the root node, and the height of the node is accumulated from the leaf node, Keith.
(7) Forest (Forest): A collection of trees that are disjoint by many.

Some properties of the tree:
(1) The number of nodes in the tree equals the degree of all nodes plus 1. (root node does not have parent node)
(2) the m^ (i-1) node (i >= 1) on the first layer of a tree with a degree of M
(3) The M fork tree with height H has a maximum of (m^h-1)/(M-1) nodes
(4) The minimum height of M-fork tree with N nodes is ceil (LOGM (n (m-1) +1))

The most common tree structure is a binary tree, that is, each node has a maximum of two child nodes, is divided into called Left Dial hand node and right child node.

typedefstruct node{    int val;    struct node *left;    struct node *right;}Node;

In addition to the binary tree, the more general structure of the tree is more than two child nodes, but more than one node. There are usually two types of implementations:
1. Array pointers
The simplest and most brutal approach is to store pointers to individual child nodes directly in each node using an array. If the tree is a M-fork tree, the node can be defined as follows:

typedefstruct node{    int val;    struct node* childs[M];}Node;

But this approach requires a lot of space to store pointers, and if the trees are sparse, much of the pointer space is wasted. So this storage structure is suitable for dense trees, and is known to be a few fork trees in the case of storage.

2. Left child-right sibling notation (first child/next sibling representation)
This method represents a multi-fork tree in the form of a binary tree, each node storing two pointers to its first child node and its neighboring sibling nodes.

typedefstruct node{    int val;    struct node *child;    struct node *sibling;}Node;

This kind of storage does not waste a lot of space as the array is stored, but it is cumbersome to traverse the tree or get the related node, and it is not easy to store the array.

Data Structures-trees (tree)

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.