data structures - Binary tree to Binary Search Tree (BST) -


How do you change the binary tree in the binary search tree with o (1) extra space?

Changing an orrod binary tree into the ordered binary search tree is trivial, but slightly faster It's hard to do.

Here is a simple implementation that should meet your criteria, I will not just describe the overall algorithm with actual steps.

  1. Take random leaf nodes from your existing tree
  2. Unlink the lead node from your existing tree
  3. Enter your new binary search tree
  4. Unlink that node from your existing tree
  5. Find the right place for you, Link the node to your new binary search tree
  6. Repeat steps 4-6 until the original tree is empty

Only some variables should be required, such as the parents of the leaf node, you are unlikely (unless there are parent links in the nodes), the root node of the new tree and some floating variables, your o (1 ) In the space criteria.

This will not produce an optimum binary tree for you before adding the nodes, and adding them in the correct order, or balancing like a red-black tree or an aspen tree of binary search tree. Have to use.


Comments