wu :: forums
« wu :: forums - correcting a BST »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 12:43am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Grimbal, SMQ, towr, william wu, ThudnBlunder, Icarus, Eigenray)
   correcting a BST
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: correcting a BST  (Read 527 times)
inexorable
Full Member
***





   


Posts: 211
correcting a BST  
« on: Jan 27th, 2007, 11:20pm »
Quote Quote Modify Modify

One of the leaves of the binary search tree is connected to some other node of the binary search tree by mistake. So disconnect the wrong connection assuming the wrong connection is at a higher depth than the correct connection.
So write a function which corrects the given binary tree.
 
PS:- I heard that this was an interview question. As the question does not seem well defined, please make any necessary assumptions.
 
Thanks.
IP Logged
sk
Newbie
*





   


Posts: 45
Re: correcting a BST  
« Reply #1 on: Jan 28th, 2007, 4:38pm »
Quote Quote Modify Modify

it shud be straight forward. u can do it in 3 steps
 
1) find the node
 
use a BFT and at every node check if the BST property holds leftchild <= parent node <= rightchild. If not then  
faultyNode is the left or right kid not satisfying the eq.  
 
2) delete the node (since this is the leaf node there is no readjustment that is to be made)
 
make parent-><faulty kid> = null;
 
3. Insert it again
insert it in proper place.
 
however, this would get interesting if the faulty one was an internal node. Try it for that.
« Last Edit: Jan 28th, 2007, 4:39pm by sk » IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board