TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: SOREN PETERSEN
from: JULIAN KWAN
date: 1997-05-17 10:30:00
subject: Re: Binary Search Tree

 SP> How to insert something in the tree and still be sure that the tree
 SP> very compact (to enable the fastest search capabilities)
       Well a binary search tree isn't designed to be compact. It's designed 
for speed (I think). To insert something :
- If you don't have anything there yet (I think it's called the root) then 
create place the current value as the root.
- If there is something there then start comparing values, if the value in 
the tree is < the node you want to add, go right in the tree. Continue this 
process until you've found an "empty" space to place the node (you must keep 
comparing values).
- If the value in the tree is >= to the value you want to insert then do the 
above except start by going left in the tree.
       Of course this can be changed however you like but that's the basic 
Idea. An example may go a long way to explaining this so I'll try.
Say you want to build a BST of the phrase "Computer Science". For our 
purposes I'll ignore the space, you can take it into account if you want to.
So because you have no tree to start the first letter you encounter in the 
string is used as the root (C in this case).
The second letter you encounter is O and that is greater than C so you check 
the right child of C to see whether a node already exists. It doesn't so now 
you have :   C
              \
               O
Next letter up to the plate is M which is greater than C but less than O so 
you check if O's left child is empty which it is, so you make M the left 
child of O or :    C
                    \
                     O
                    /
                   M
Well.. I hope you get the idea. If you need any more help (or if you already 
knew how to do this and I was confused) let me know =)
There is no way to make a "compact" tree so to speak. You can evenly balance 
two trees but I haven't found a purpose for that yet.
 SP> How to delete a node in the tree.
       It's easiest if you're using pointers or an array representation of 
pointers. You simply change a coupld of links. Say you wanted to (from the 
above) delete O. Now you don't want to lose everything connected to O so what 
you do is make C's right child (in this specific case) point to M instead of 
O at which point you can delete O from memory. If O has two children, I don't 
really know what to do. Maybe which ever one came first in the list or 
whichever one is less than the other. Anyone know?
... Epoch:  the sound made by a hen
--- Telegard v3.03.b05/mL
---------------
* Origin: fks Online! * Ontario, Canada * (905)820-7273 * (1:259/423)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.