Search Trees
- Binary Search Trees
- Definition (HSM Ch.5.7.1, Fig.5.28)
- Searching a BST by key (HSM Ch.5.7.2, Prog.5.20-21)
- Searching a BST by rank (HSM Ch.5.7.2, Prog.5.22)
- Insertion into a BST (HSM Ch.5.7.3, Fig.5.29, Prog.5.23)
- Deletion from a BST (HSM Ch.5.7.4, Fig.5.29-30, Prog.5.23)
- AVL Trees
- The need for balancing BSTs (HSM Ch.10.2, Fig.10.8-10)
- Definitions: Height balanced, Balance factor (HSM p.554, Fig.10.8)
- AVL Tree: For all nodes, balance factor(N) = -1,0, or 1
- Maintaining AVL trees
- Balance whenever a node has BF +-2
- Balance wrt nearest parent of inserted node with BF = +-2
- Example (HSM Fig.10.11)
- Rotations
- New node in left of left of unbalanced node => do LL
- New node in right of left of unbalanced node => do LR
- New node in right of right of unbalanced node => do RR
- New node in left of right of unbalanced node => do RL
- Analysis (HSM Fig.10.13)
- 2-3 Trees
- Definitions (HSM p.567)
- 2-nodes and 3-nodes
- 2-3 trees
- Number of nodes between 2h-1 and 3h-1
- Hieght between ceil(log3(n+1)) and
ceil(log2(n+1))
- Example (HSM Fig.10.14)
- Searching a 2-3 tree = O(lnN)
- Insertion = O((2*)lnN)
- Into a 2-node (HSM Fig.10.15a)
- Into a 3-node with a 2-node parent (HSM Fig.10.15b)
- Into a 3-node with a 3-node parent (HSM Fig.10.16)
- Deletion = (O(lnN))
- Example (HSM Fig.10.17)
- If not deleting from a leaf, delete from a leaf (largest
in left subtree or smallest in right) to replace.
- From a leaf 3-node is direct
- From a leaf 2-node with a 3-node sibling => rotate
(HSM Fig.10.18)
- From a leaf 2-node with a 2-node sibling => combine
(HSM Fig.10.19)
- B Trees
- Motivation to reduce disk access (HSM p.602)
- Definition (HSM p.603-604)
- Full m-way search tree
- Root is at least a 2-node
- Non-root nodes are at least ceil(m/2)-nodes
- Examples (HSM Fig.10.38-39)
- Insertion = O(h)
- Search for leaf
- Insert if space
- Split on median and insert median in parent
- Deletion = O(h)
- Delete from leaf
- If enough left, done
- If too few left rotate if possible, else combine
- Choice of m
- Larger m => smaller h => lower complexity
- Larger m => larger nodes => more data to transfer and search
- Need to balance based on disk characteristics
Exercises
- Deletion of an element from a BST: HSM Ch.5.7, exercise 1.
Lab Tasks
Exam Style Questions
- Define a binary search tree.
- Give the {iterative,recursive} algorithm for search a binary search tree.
[Picture of some BST, make up your own :-]
- Derive, using using step counts or reasonable explanation, the
asymptotic time complexity of the {iterative,recursive} algorithm for
search a binary search tree.
- Describe what extra information has to be stored in each node of a BST
to enable the BST to be searched by rank.
Outline the algorithm for seaching such an extended BST by rank.
What is the asymptotic time complexity of the algorithm?
- What is the advantage of an AVL tree over a binary search tree?
- Copy the following AVL tree to your answer book, then annotate each
node with its balance factor.
-
[Picture of some AVL tree, make up your own :-]
- Given the attached generic rotations for maintaining AVL trees, draw the
sequence of AVL trees that result from adding the sequence of values
[some new values] to this AVL tree.
[Picture of some AVL tree, make up your own :-]
- Give the definition of a 2-3 tree.
- What is the asymptotic time complexity of {searching,inserting into,
deleting from} a 2-3 tree?
- Draw the sequence of 2-3 trees that result from adding the sequence
of values [some new values] to this 2-3 tree.
[Picture of some 2-3 tree, make up your own :-]
- Draw the sequence of 2-3 trees that result from deleting the sequence
of values [some new values] from this 2-3 tree.
[Picture of some 2-3 tree, make up your own :-]
- What is the motivation for the use of B-trees?
- Give rough (high level) algorithms for insertion and deletion from a
B-tree.
- Explain the factors that affect the choice of the degree of a B-tree.