Tree balancing

Now, when we know what a binary search tree looks like on the Assembly programming level, it would not be correct not to return to the question of binary search tree balancing. There are several approaches to this problem, however, we would only consider one-the Day-Stout-Warren algorithm (included in the accompanying code). The algorithm is simple:

  1. Allocate a tree node and make it a "pseudo root" for the tree, making the original root the pseudo root's right child.
  2. Convert the tree into a sorted linked list by means of an in-order traversal (this step also calculates the number of nodes in the original tree). No additional allocations are required, as the step reuses existing pointers in tree nodes.
  3. Convert the list back into a complete binary tree (one in which the bottom layer is populated strictly from left to right).
  4. Make pseudo root's right child the tree's root.
  5. Dispose of the pseudo root node.

Applying this algorithm to our opcode tree will result in the following structure:

The structure remains almost the same-four levels, including the root node, and four nodes at the bottom-most layer. The order of opcodes has changed a bit, but this is not that important in the case of this particular example. However, should we design a more complex system that expects much more load, we could design the encoding of the operation code in such a way that the most frequently used opcodes would be encoded with values from the upper layers and the least frequently used opcodes, with values from the bottom layers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset