Data structure--tree

Source: Internet
Author: User

I. Definition of a tree

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) a non-intersecting finite set T1, T2 、..... Tm, where each set is itself a tree, and is called the root subtree.

Second, the degree of the node, the degree of the owning subtree is called the nodal point


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

A node with a degree of 0 is called a leaf node, and nodes with a degree of not 0 are called branch nodes.

Iii. Relationship between nodes

For example, the node's subtree is called the root of the child, and the node is called the child's parents.

The same parent's child is called a brother.

Any node in a subtree that is a root is called a descendant of that node.

Iv. depth of the tree


The hierarchy of nodes is defined from the root, and the maximum level of tree nodes is called the depth or height of the trees.

If the tree tree nodes of the various sub-trees from left to right is ordered, not interchangeable, then called the tree is an ordered tree, otherwise known as unordered tree.

Five, the 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 node number}ptree;
Such a structure is easy to find parents, and if the child is to find the node, it needs to traverse the entire structure. We can add a domain to the left child to solve, the design of the storage structure is a very flexible process. Whether the design of a storage structure is reasonable depends on whether the operation based on the storage structure is suitable, whether it is convenient or not, and the time complexity is good.

VI. storage structure of trees--child notation

We know the number of children each node is different, so we have to store the child's address there are two ways, the first is the number of pointer fields equal to the degree of the tree, the second is the number of pointer fields equal to 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;
Seven, the storage structure of the tree--the representation of parents and children

Eight, the storage structure of the tree--child brother representation

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