Data Structure - Tree (1)

The content comes from: "Dahua Data Structure" reading notes

Definition of a tree

The definition of tree should pay attention to two points

  • The first is that if a tree has a root node, then the root node must be unique.
  • The second is that there is no limit to the number of subtrees, but they must be disjoint.

Node related concepts

Degree of nodes, leaf and terminal nodes, non-terminal nodes or branch nodes, internal nodes, degrees of trees

Other related concepts of tree

The depth or height of a tree is the maximum level. The concept of ordered tree and Wu-ordered tree, the concept of forest, the main difference between linear structure and tree structure is that one is one-to-one and the other is one-to-many.

tree storage structure

The first parental notation.

The most basic parental notation is when allocating storage space for a node. There is a data field and a pointer field. It can also be divided into pointers to child nodes as needed. This storage method can easily find her parent node, and the time complexity is O(1). Very convenient. Because if you need to find the corresponding child node or sibling node, cousin node, you only need to increase the corresponding pointer.

second child notation

Scheme 1 assigns the number of pointers to all nodes equal to the degree of the tree

This allocation method is relatively simple, because the degrees of nodes are different, but the space waste is relatively large. It is more suitable if the degree of each node is the same or similar.

Scheme 2 The number of pointer fields of each node is equal to the degree of the node.

But take a special location to store the number of node pointer fields. This method overcomes the disadvantage of wasting space, but it will bring loss in time.

Scenario three child representation.

Connect the child nodes of each node. Using a singly linked list as a storage structure, with n nodes, there are n linked lists. If it is a leaf node, the singly linked list is empty. Then n head pointers point to these linked lists. Finally, the data fields and head pointers of these nodes are stored in a sequential storage structure and put into a one-dimensional array. There are two node storage structures in this way. One is a sequential storage structure composed of data fields and head pointers, and the second is a chained storage structure composed of child nodes.

You can combine parent notation with child notation. Add a block to the original data field and head pointer node to store the parent node, this method is called parent-child representation .

The third is the child brother notation.

In this method, each tree node is divided into three parts, one is the data field, one stores a pointer to the first child node, and the last stores a pointer to the right sibling of the node. The biggest advantage of this method is to convert any tree into a binary tree. This tree can then be processed using the properties and algorithms of a binary tree. Of course, if you're looking for parent nodes. It is also possible to add a pointer to the parent node above the storage structure of this node.

binary tree

Features of binary tree

  • First, each node has at most two subtrees, so there is no node with degree greater than two in a binary tree.
  • Second, the left and right subtrees are ordered.
  • Third, even if there is only one subtree, it is necessary to distinguish whether it is a left subtree or a right subtree.

There are five basic forms of binary trees.

  • First, empty binary tree
  • Second, there is only one root node.
  • Third, the root node has only the left subtree
  • Fourth, the root node has only the right subtree
  • Fifth, the root node has both a left subtree and a right subtree.

special binary tree

First, the oblique tree.

It is divided into left-slope tree and right-slope tree. If all nodes are binary trees with only left subtree, it is called left-slope tree. The opposite is called a right-slope tree.

Second, a full binary tree.

All branch nodes have left and right subtrees, and all leaves are on the same level.

Features of a full binary tree
  • First, leaves can only appear on the bottom layer.
  • Second, the degree of non-leaf nodes must be two.
  • Third, in a binary tree of the same depth, the number of nodes with full binary number is the largest and the number of leaves is the largest.
Third, a complete binary tree.

It is a full binary tree, and some nodes on the right side of the last layer are missing. A binary tree with the same number of nodes has the smallest depth of a complete binary tree.

Properties of Binary Trees

  • The first property is that there are at most 2 (k-1) nodes on the i-th level of the binary tree.
  • The second property is that a binary tree of depth k has at most 2 k times - 1 nodes.
  • Property 3: For any binary tree, the number of nodes with degree zero is equal to the number of nodes with degree two + 1
  • Property four, the depth of a complete binary tree with n nodes is [log2n]+1
  • Property 5. For a complete binary tree with n nodes, for any node i,
    1, if i=1, then i is the root node of the binary tree, without parents. If i>1, its parent node is [i/2].
    2. If 2i>n, node i has no left child, otherwise its left child node is 2i
    3, if 2i+1>n, then node i has no right child, otherwise its right child is node 2i+1.

storage structure of binary tree

sequential storage structure

The sequential storage structure is generally only used to store the complete binary tree. The specific storage method is to store the complete binary tree in a one-dimensional array sequentially from top to bottom and from left to right according to the program number.

chain storage method

Each node contains one data and two pointer fields. The two pointer fields point to its left and right nodes respectively.

Reference: Dahua Data Structure

---------------Dividing line--------------

Will write more later

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561772&siteId=291194637