Trees and binary trees (2)

Time:2021-5-31

Storage structure of binary tree

Sequential storage

Trees and binary trees (2)

  • Because binary tree is more flexible, sequential representation will waste a lot of space, so it is generally not used, unless it is full binary tree or full binary tree
Chain representation

Trees and binary trees (2)
Trees and binary trees (2)

Static linked list

Trees and binary trees (2)

  • It is stored in a linear structure, so the allocation of storage space is not very flexible. If a tree of known length is implemented, it is impossible to insert or delete data

Recursive algorithm for traversing binary tree

Trees and binary trees (2)

Application of binary tree traversal

Trees and binary trees (2)Trees and binary trees (2)
Trees and binary trees (2)Trees and binary trees (2)Trees and binary trees (2)Trees and binary trees (2)Trees and binary trees (2)

Non recursive algorithm for traversing binary tree

Storage structure of tree

Sequential representation

Parents express

Trees and binary trees (2)
Advantages: simple structure, a sequence table can store a tree
Disadvantages: static structure, not convenient to do node insertion and deletion; There is a waste of storage space; Because most of the application of tree is to find children from the node, and the parent representation is often used to find parents from the node, but the application in finding children from the node is more troublesome, so it needs to scan the whole tree.

Chain structure

Multilist (child representation)
  • Similar to binary list, it wastes more spaceTrees and binary trees (2)
Children‘s list

Trees and binary trees (2)
Considering the data and relationship, we first construct an array to store each node (data part); The relationship between the children and their parents is represented by the continuous structure. That is to say, all nodes are stored in array first, and then each node generates a single chain table to store all children of the node

Representation of left child, female and right brother of tree (binary linked list representation)

If the ordinary tree is transformed into a very familiar binary tree form, one-to-one correspondence can be formed, and the binary tree can be used to store the ordinary tree without losing the information of the ordinary tree
Trees and binary trees (2)