Memories of the conversion between trees, forests and binary trees

The conversion of trees, forests and binary trees is not difficult, but the conversion around it is very troublesome, especially when facing exams, it is easy to dazzle yourself, here is a summary of the following ways to strengthen various conversions using formulas memory.

1. Trees and Forests -> Binary Trees

Formula

  1. Add lines at the same level : connect the nodes at the same level
  2. Leapfrogging only the left line : nodes of different levels only keepthe line of the rightmost child
  3. The eldest child is on the left, and the child is on the right (other children become the right child of the eldest son in turn) : After the addition of the line is completed, adjust the structure of the generated binary tree. The eldest son serves as the left node, and the brother nodes at the same level as the right node in turn
  4. The steps of turning the forest into a binary tree are the same as above, only need to connect the independent trees at the end according to the requirements of the brothers on the right .

Insert picture description here

Insert picture description here

2. Binary Tree -> Tree and Forest

Formula

  1. Left child: the left child of all binary tree nodes does not move
  2. Right brother: All the right children of the binary tree nodes are added to become the brothers of the left child (the one that does not move) of the binary tree
  3. The right subtree is completely detachable: if it is a forest, then the right subtree will be disconnected one by one and then complete steps 1, 2

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_34902437/article/details/101535970