Data Structure-tree, Data Structure

Source: Internet
Author: User

Data Structure-tree, Data Structure

1. Tree Definition

A tree is a finite set of n (n> = 0) nodes. N = 0 is called an empty tree. In any non-empty tree: 1. There is only one specific root node. 2. When n> 1, the remaining nodes can be divided into m (m> 0) finite sets T1, T2 ,..... tm, where each set itself is a tree, also known as the root subtree.

2. The degree of the node. Its subtree is called the degree of the node.

For example, if the degree of node A is 2, the degree of Node B is 1, the degree of node C is 2, the degree of node E is 3, and the degree of node G is 0

A node whose degree is 0 is called a leaf node, and a node whose degree is not 0 is called a branch node.

3. Relationship between nodes

For example, the Child tree of a node is called the root child, And the node is called the child's parent.

Children of the same parent are called brothers.

Any node in the subtree with a node as the root is called the child of the node.

4. Tree depth

The hierarchy of a node is defined from the root. The maximum hierarchy of a node is called the depth or height of a tree.

If the sub-trees at the node of the tree are regarded as ordered from left to right and cannot be exchanged, the sub-trees are called ordered trees; otherwise, they are called unordered trees.

5. Storage Structure of the tree-parental Representation

Typedef int TElemType; typedef struct PTNode {// node Structure TElemType data; int parent;} PTNode; typedef struct {// node array PTNode nodes [MAX_TREE_SIZE]; int r, n; // root position and number of points} PTree;
This structure is easy to locate the parent. If you want to find the child of the node, You need to traverse the entire structure. We can add another domain for storing the children on the left to solve this problem. The storage structure design is a very flexible process. Whether a storage structure is designed properly depends on whether the calculation based on the storage structure is appropriate, whether it is convenient, or whether the time complexity is good.

6. Tree Storage Structure-Child notation

We know that the number of children at each node is different, so there are two ways to store the address of the child. The first is that the number of pointer fields equals the degree of the tree, the second is that the number of pointer fields equals the degree of the node.

#define MAX_TREE_SIZE 100typedef struct CTNode{int child;struct CTNode *next;} *ChildPtr;typedef struct{TElemType data;ChildPtr firstchild;}CTbox;typedef struct{CTbox nodes[MAX_TREE_SIZE];int r, n;}CTree;
VII. Tree Storage Structure-Expression of parents and children

8. Tree Storage Structure-child brother notation

If the first child of any tree is unique at its node, its right brother is also unique. Therefore, we set two pointers pointing to the first child of the node and the right brother of the node respectively.

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: 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.