Class notes: the logical structure of the tree, the storage structure of the tree

The logical structure of the
tree : a finite set of n (n≥0) nodes.
When n = 0, it is called an empty tree ;
any non-empty tree satisfies the following conditions: (
1) There is only one specific node called the root; (
2) When n> 1, the rest except the root node The node is divided into m (m> 0) disjoint finite sets T1, T2, ..., Tm, where each set is a tree, and is called a subtree of this root node.
The definition of a
tree is
the degree of a node that is a basic term of the tree using a recursive method : the number of subtrees owned by the node.
The degree of the tree: the maximum value of the degree of each node in the tree.
Leaf node : a node with a degree of 0, also known as a terminal node.
Branch node : a node whose degree is not 0, also known as a non-terminal node.
Children and parents : the root node of a subtree in the tree is called the child node of this node, and this node is called the parent node of its child node;
brother : the child nodes with the same parent are called each other For brothers.
Path : If the node sequence n1, n2,…, nk of the tree has the following relationship: the node ni is the parent of ni + 1 (1 <= i <k), then n1, n2,…, nk are called a The path from n1 to nk; the number of edges passing on the path is called the path length .
Ancestors and descendants : In the tree, if there is a path from node x to node y, then x is called the ancestor of y, and y is called the descendant of x.
Node layer: The number of layers of the root node is 1; for any other node, if a node is on the kth layer, the child node is on the k + 1th layer.
Tree Depth : The maximum number of all nodes in the tree, also known as height .
Sequence number : the nodes in the tree are sequentially numbered from 1 to the lower layer and the same layer from left to right in order of consecutive natural numbers starting from 1.
Ordered tree, disordered tree : If the subtrees of the nodes in a tree are ordered from left to right, this tree is called an ordered tree; otherwise, it is called a disordered tree.
Forest : m (m≥0) a collection of disjoint trees.
Tree traversal : Starting from the root node, all nodes in the tree are accessed in a certain order, so that each node is visited once and only once.
The essence of traversal : tree structure (non-linear structure) → linear structure.
Trees usually have three ways: pre-order (root) traversal , post-order (root) traversal and hierarchy (second) traversal .
Pre-order traversal
The pre-order traversal operation of the tree is defined as: if the tree is empty, no traversal is performed; otherwise ⑴ visit the root node; ⑵ traverse each sub-tree of the root node in order from left to right.
Post-order traversal
The post-order traversal operation of the tree is defined as: if the tree is empty, the traversal ends; otherwise ⑴ traverse each sub-tree of the root node in the order from left to right; ⑵ visit the root node.
The
order traversal operation of the order traversal tree is defined as: starting from the first layer of the tree (ie, the root node), traversing layer by layer from top to bottom, in the same layer, in order from left to right access.
Tree storage structure
The logical relationship between the nodes in the tree: a one-to-many relationship.
The key to the storage structure: how to represent the parents and children of the node.
The
basic idea of parent notation : use a one-dimensional array to store each node of the tree (generally stored in order), an element in the array corresponds to a node in the tree, each node records two types of information: The data information and the subscript of the parent of the node in the array.
Definition of node data types in parent notation

template <class T>
struct PNode{
     T data; 
     int parent; 
};

Child notation-multiple linked list notation (the pointer field in a node represents a child)
Each node in the linked list includes a data field and multiple pointer fields, and each pointer field points to a child node of the node.
Option 1: The number of pointer fields is equal to the degree of the tree.
Disadvantages: wasting space.
Option 2: The number of pointer fields is equal to the degree of the node.
Disadvantages: The node structure is inconsistent.
Child notation-child linked list notation (create one for each node) Single linked list)
Features: Put all the children of each node together to form a linear list.
Basic idea: Arrange the children of each node as a linear table, and store it as a single linked list, then n nodes have a total of n child linked lists.
The n singly linked lists have n head pointers, which form a linear table.
In order to facilitate the search, the head pointer of each linked list is sequentially stored and stored.
Finally, the array storing n head pointers and the array storing n nodes are combined to form the head array of the child linked list.
Child node

struct CTNode {
     int child;      
     CTNode *next; 
};

Header node

template <class T>
struct CBNode {
     T data;     
     CTNode *firstchild; 
};

Child brother notation
The first child of a node is unique and the right brother of a node is unique. Set two pointers to the first child and right brother of the node.
Node structure

template <class T>
struct TNode{
     T data;      
     TNode <T> *firstchild, *rightsib; 
};

Summary of the tree's storage structure
Sequential storage: essentially a static pointer
parent notation, parent, child notation
chain storage:
multiple linked list notation, child linked list notation, child brother notation

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/102990124