Example Trace of Kruse's Delete (remove) Routine for B_Trees
Note: This e.g. follows the picture of an order 5 B-tree drawn and passed out in class.
| Global variable | delval
40 |
In the following: param = parameter, lv = local variable, out=output, in = input, ret addr= return address, fn ret= value the function returns, ptr=pointer, ref= call by reference.
Also note - values in parentheses ( ) are values that eventually are placed in the memory cell in question, by a function which follows later in the stack.
The following is what the environment stack that is built when insert(newint) is called in a main program.......
| Environment of Function: | Status - when popped off stack - | Local variables, parameters, return addresses, return values...... | ||
| remove | last of all that follow |
target(param) ptr back to delval |
old_root
(ptr to node with 100 orig.) |
root node with 100. later node with (60,100, 120, 140) |
|
result(lv) (success) |
fn remove ret (success) |
remove ret addr addr where called in main or ? |
||
| recursive_remove, 1st call | right before remove above is popped or 2nd to last |
current(in param) address of root with 100 |
target (in param) ptr to delval |
position(lv)
(0 from searchnode, below) |
|
result(lv) (eventually success) |
fn recursive_remove ret (eventually, success) |
recursive_remove ret addr the line where (or just after where) recursive_remove is called in remove |
||
| search_node | popped before the next (2nd) call to recursive_remove, below | current(in param)
root node w/100 |
target(in param) ptr to delval |
position(out param) 0 and stays 0 |
|
fn search_node ret not_present |
search_node ret addr line with test if == success, not so go to else clause in recursive_return |
|||
| recursive_remove, 2nd call | right after 3rd call to recursive_remove is popped, below |
current(in param) address of node with 30,60 |
target (in param) ptr to delval |
position(lv)
(1, after searchnode returns 1) |
|
result(lv) (eventually, success) |
fn recursive_remove ret (eventually, success) |
recursive_remove ret addr the line where (or just after where) recursive_remove is called in recursive_remove |
||
| search_node | popped before the next (3rd) call to recursive_remove, below | current(in param)
address of 30, 60 |
target(in param) ptr to delval |
position(out param) 0 then 1 |
|
fn search_node ret not_present |
search_node ret addr line with test if == success, not so go to else clause in recursive_remove |
|||
| recursive_remove, 3rd call | right after is popped, below |
current(in param) address of node with 40, 50 |
target (in param) ptr to delval |
position(lv)
(0, after searchnode returns 0) |
|
result(lv) (eventually, success) |
fn recursive_remove ret (eventually, success) |
recursive_remove ret addr the line where (or just after where) recursive_remove is called in recursive_remove |
||
| search_node | popped below | current(in param)
address of node with 40, 50 |
target(in param) ptr to delval=40 |
position(out param) 0 and stays 0 |
|
fn search_node ret success |
search_node ret addr line with test if == success, yes so go result = success line |
|||
| remove_data | called in 3rd environment of recursive_remove, above, popped, and returns to 3rd env of recursive_remove | current(param)
ptr to 40,50 |
position(in param) 0
|
|
|
fn remove_data ret. void |
remove_data ret. addr. to call in 3rd recursive_remove environment |
|||
| restore | now, the 3rd environment of recursive_remove has been popped, we are in the 2nd environment of recursive remove and test for count < (order-1)/2 is TRUE | current
address of node with 30, 60
|
position
1
|
|
| fn restore returns
void |
fn restore return addr
to last line of 2nd call to recursive_return |
|||
| combine | since we are in the middle branch and neither the left or right sibling has room for us to borrow, we combine | current (param)
address of node with 30, 60
|
position(param)
1
|
i(local variable)
loop control variable
|
| NOTE: when done, current is pointing to a node with just 60, and branch 0 out of 60 has 10, 20, 30, 50, branch 1 out of 60 has 70,80 | left_branch(lv)
address of 10, 20 |
right_branch(lv)
address of 50 (40 has been removed) |
||
| fn combine returns
void |
return address for function
combine
to end of restore, so pop restore off stack |
|||
| Note: | now combine(above is popped! | restore(above) is popped! | 2nd call to recursive_remove is popped! | We are back in the 1st call to recursive_remove right after the 2nd recursive_remove call returns |
| restore | now, the 2nd environment of recursive_remove has been popped, we are in the 1st environment of recursive remove and test for count < (order-1)/2 is TRUE | current
address of node with 100
|
position
0
|
|
| fn restore returns
void |
fn restore return addr
to last line of 1st call to recursive_return |
|||
| combine | since we are in the left branch and the right sibling has no room for us to borrow, we combine | current (param)
address of node with 100
|
position(param)
0
|
i(local variable)
loop control variable
|
| NOTE: when done, current is pointing to a node with just 60, 100, 120, 140 | left_branch(lv)
address of 60 (alone) |
right_branch(lv)
address of 120, 140 |
||
| fn combine returns
void |
return address for function
combine
to end of restore, so pop restore off stack |
|||
| Note: when this last call to combine completes... | The 1st call to recursive_return completes... | Then remove completes by discarding the current empty root node and making the root now be 60, 100, 120, 140 |