Binary tree to Binary Search Tree (BST)
NickName:dsiap Ask DateTime:2010-05-17T17:56:49

Binary tree to Binary Search Tree (BST)

How can you convert Binary Tree to Binary Search Tree with O(1) extra space ?

Copyright Notice:Content Author:「dsiap」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2848067/binary-tree-to-binary-search-tree-bst

Answers
Lasse V. Karlsen 2010-05-17T10:24:22

Converting an unordered binary tree into an ordered binary search tree is trivial, but a bit more difficult to do fast.\n\nHere's a naive implementation that should satisfy your criteria, I will not describe the actual steps to take, just the overall algorithm.\n\n\nGrab a random leaf node from your existing tree\nUnlink the leaf node from your existing tree\nMake the node the root of your new binary search tree\nGrab another random leaf node from your existing tree\nUnlink that node from your existing tree\nFind the right spot for, and link the node, into your new binary search tree\nRepeat step 4-6 until the original tree is empty\n\n\nYou should require only a few variables, like the parent of the leaf node you're unlinking (unless the nodes has parent-links), the root node of the new tree, and a couple of temporary variables, all within your O(1) space criteria.\n\nThis will not produce an optimal binary search tree. For that you need to either sort the nodes before adding them, and adding them in the right order, or use a balancing binary search tree, like a red-black tree or a splay tree.",


More about “Binary tree to Binary Search Tree (BST)” related questions

Inorder successor of a binary tree (NOT BST)

Can someone help me to figure out how to find inorder successor of a binary tree for a given node(not a binary search tree)? I know how to find it in a binary search tree: It will be the left-most ...

Show Detail

Maximum Sum BST in a Binary Tree

Given a binary tree root, the task is to return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST). Assume a BST is defined as follows: - The left sub-tree of a ...

Show Detail

Insertion into Binary Search Tree

So i have to insert a node into a binary search tree. In my introductory class, the binary search trees are represented as linked lists such as , [4, [5, [0, [],[]], [2, [], []]], [1, [],[]]] for t...

Show Detail

What is the difference between a balanced binary search tree and a binary search tree?

Sorry if this is an extremely basic question, but I am fairly new to trees and hence, this doubt bothers me these days. What is the difference between a binary search tree and a balanced binary se...

Show Detail

Finding maximum depth of a Binary Serach Tree (BST)

Given a binary search tree (BST). Find the maximum depth of the binary search tree iteratively. I know the method using queue [level order traversal] but time complexity is O(N) as we need to visi...

Show Detail

Recreate binary search tree BST

I have a Binary Search Tree and each of its node has two values. int value; String name; So its node is like this. class Node { int value; String name; Node left, right; ...

Show Detail

binary search tree bst

So, I need to build an unbalanced binary search tree, create a method to balance it, which i did by building a sorted array based off a level order traversal ( or at least attempted to ), then prin...

Show Detail

Binary tree to Binary Search Tree (BST)

How can you convert Binary Tree to Binary Search Tree with O(1) extra space ?

Show Detail

Insertion in Binary Search Tree vs Insertion in Binary Tree

What is the difference between insertion in Binary Search Tree(BST) and in Binary Tree(BT)? I know in BST, you compare the value of the new node with root, if smaller, you added to its left, if big...

Show Detail

Differences in implementation of a tree, binary tree and, binary search tree in python

I'm having a bit of difficulty grasping the differences in the methods and implementation of a simple tree, a binary tree, and a binary search tree. It seems that I keep getting confused between the

Show Detail