[JS data structure and algorithm] knowledge tree

table of Contents

1. The characteristics of the tree in life

2. Simulation into a tree structure

Three, the definition of the tree

Four, the basic terminology of the tree

Five, the representation of the tree structure

Six, binary tree

1. Definition of Binary Tree

2. Attributes of Binary Tree

3. Five forms of binary trees

4. The special form of the binary tree

5. The storage structure of the binary tree

5.1 Sequential storage structure

5.2 Chain storage structure


1. The characteristics of the tree in life

The tree in life looks like this:
tree

  • It usually has only one root , and the tree trunk is connected to the root .
  • There are several branches on the trunk , which can be divided into several small branches .
  • There are several leaves at the end of the tree .

2. Simulation into a tree structure

Reverse the tree above, each branch connects a node, and each leaf is another node, which is the tree structure in our data structure.

Similar to the tree structure are family tree, company organizational structure, knowledge structure, and so on.

Abstract structure

Three, the definition of the tree

Tree is a finite set of n (n ≥ 0) nodes. It is either an empty tree (n = 0); or a non-empty tree, for a non-empty tree T;

(1) There is one and only one node called the root.

(2) The nodes other than the root node can be divided into m (m ≥ 0) disjoint finite sets T1, T2,..., Tm, each of which is a tree itself, and is called Rooted SubTree (SubTree)

The structure definition of the tree is a recursive definition, that is, the definition of the tree is useful in the definition of the tree.

 

Four, the basic terminology of the tree

The representation of nodes and nodes here is not disputed, both are acceptable.

(1) Node : an independent unit in the tree, including a data element and several branches pointing to child nodes. Nodes A~K in the picture;

(2) Degree of the node (Degree) : The number of subtrees owned by the node is called the degree of the node. For example, the degree of A is 4, the degree of E is 1, and the degree of D is 0;

(3) the tree degree : maximum point of the tree is the degree of each node within the tree. The degree of the tree in the figure is 4;

(4) Leaf : A node with a degree of 0 is called a leaf or a terminal node. For example, F, J, K, H, C, D, I are all leaves;

(5) Parent node or parent node (Parent) : If a node contains a child node, then this node is called the parent node of its child node, as shown in Figure B is the parent node of F, G, and H point;

(6) Child node or child node (Child) : the root node of the subtree contained in a node is called the child node of the node, H is the child node of B, and F is the child node of B ;

(7) Sibling : Nodes with the same parent node are called sibling nodes, and F and G are sibling nodes;

(8) The level of the node (Level) : starting from the definition of the root, the root is the first level, and the sub-nodes of the root are the second level, and so on; in the above figure, A is located in the first level, and K is located in the fourth Floor;

(9) The height or depth of the tree (Depth) : the maximum level of nodes in the tree; the depth of the tree in the above figure is 4;

(10) Non-terminal nodes or branch nodes : nodes whose degree is not 0;

(11) Cousin node : Nodes with parents on the same level are cousins ​​to each other;

(12) Ancestors of a node : from the root to all nodes on the branch of the node;

(13) Descendants : any node in the subtree rooted at a certain node is called the descendant of that node;

(14) Forest: A collection of m (m>=0) disjoint trees is called a forest.

Five, the representation of the tree structure

  • When the parent-child relationship node of the tree is simulated as a linked list connection node, it can have the following structure

But let's think about it, if we apply for vacancy pointers with this kind of mindlessness, it will become very complicated. Whether we want to apply for one or two or more, in the end, it may not really meet our needs.

So this way to construct our tree structure is not very good.

  • Son-brother notation

When defining a node, you only need to include three attributes

  • this.key : the stored data content. You can also store [key, value] in the form of key-value pairs
  • this.left : used to point to the left subtree.
  • this.sibling : used to point to a sibling node.

It is not difficult to find that when we rotate the structure represented by the son-brother representation by 45°, we reach a binary tree.

All trees can be simulated as binary trees in this form

Six, binary tree

1. Definition of Binary Tree

Binary Tree (Binary Tree) is a collection of n (n ≥ 0) nodes, it is either an empty tree (n = 0); or a non-empty tree, for a non-empty tree T;

(1) There is one and only one node called the root.

(2) The nodes other than the root node can be divided into two disjoint finite sets T1 and T2, which are called the left subtree and the right subtree of T respectively, and T1 and T2 are both binary trees.

2. Attributes of Binary Tree

(1) In a non-empty binary tree, the total number of nodes at the i-th level does not exceed   , i >= 1;

(2) A binary tree with depth h has at most   one node (h >= 1), and at least h nodes;

(3) For any binary tree, if the number of leaf nodes is N0, and the total number of nodes with degree 2 is N2, then N0 = N2 + 1;

  • Full binary tree : a binary tree with a depth of h and a node.

        The number of nodes on each layer is the maximum number of nodes, that is, the number of nodes in each layer i has the maximum value

 

  • Complete Binary Tree : A binary tree with n nodes with a depth of k, if and only if each node corresponds to a node numbered from 1 to n in a full binary tree with a depth of k. Call it a complete binary tree.

          (1) Leaf nodes are only allowed to appear on the two largest levels.

          (2) For any node, if the maximum level of descendants under its right branch is l, the maximum level of descendants under its left branch must be l or l + 1,

(4) The depth of a complete binary tree with n nodes is  (Note: [] means rounding down)

(5) If each node of a complete binary tree with N nodes is stored in a sequential manner, the relationship between the nodes is as follows:

  •            If I is the node number, if I> 1, then the number of its parent node is I / 2;
  •            If 2 * I <= N, the number of its left child (that is, the root node of the left subtree) is 2 * I; if 2 * I> N, there is no left child;
  •            If 2 * I + 1 <= N, then the node number of the right child is 2 * I + 1; if 2 * I + 1> N, there is no right child.

(6) Given N nodes, h (N) kinds of different binary trees can be formed. h(N) is the Nth term of Cattleya number. h( n )=C( 2 * n, n)/(n + 1).

(7) There are i branch points, I is the total road length of all branch points, J is the total road length of leaves J = I + 2i

3. Five forms of binary trees

4. The special form of the binary tree

5. The storage structure of the binary tree

5.1 Sequential storage structure

For such a complete binary tree, we store the elements in the form of an array, and get the following array

Node

A 

B

C

D

E

F

Serial number

1

2

3

4

5

6

But we found that not all binary trees can store the elements in order like a complete binary tree, such as the following binary tree

 

Node

A 

B

C

^

D

E

^

^

^

^

F

Serial number

1

2

3

4

5

6

7

8

9

10

11

In this case, it will cause a lot of waste of space, and it is more suitable to adopt the following chain storage structure

5.2 Chain storage structure

From the definition of the binary tree, the node of the binary tree is composed of a data element and two branches pointing to the left and right nodes respectively, so the nodes in the linked list representing the binary tree must contain at least three fields.

  • data: Data field.
  • lchild: Left pointer domain.
  • rchild: Right pointer field.

Sometimes, in order to find the parent of a node, we can add a pointer field to the parent node . The storage structure of the binary tree derived from these two node structures is called a binary linked list and a three-pronged linked list, respectively.

 

Guess you like

Origin blog.csdn.net/weixin_42339197/article/details/99671691