Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
comparison)
specification for how the nodes should be arranged
values left of the root node are less than the root node, and vice versa for the
right side
-> if it didnt have the specification you would need to visit every single
node
To determine validity of a binary search tree, easiest to work from the biggest tree (at
root), to the
smallest subtree, and circle each left and right side and specify their required
domains.
Navigation through a BST - simply compare if your required value is <, =, or > the
node youre at.
If you hit NULL, you are at the right place but it doesnt exist.
// base 2
if (t == NULL) return 0;
return numNodes(t->left) + numNodes(t->right) + 1;
}
Insertion: find the position (if the branch you want to go to is NULL, you insert,
otherwise you keep
traversing down that subtree)
Deletion: a number of approaches
Delete a node, and replace it with its in-order successor (i.e. the next
highest node)
Easy to understand: consider a 1D sequence of numbers, you want an adjacent
number.
Another approach (lazy): just mark the node as deleted.
Search algorithm: if 6 is marked as deleted and the value you want to
search for is 6,
then you can just stop searching there, since the property of BSTs still
holds.
(Subtree values are < 6 and > 6).
Insertion: if 6 is marked as deleted, and you want to insert 6, then remark
that node as
not deleted.