📚 Gaddis (Ch. 21), Drozdek (Ch. 6)
In the computer science sense, trees are simply non-linear linked structures where each node may link to two or more other nodes, in a hierarchical fashion.
We use terms borrowed from family trees to describe the hierarchical relationships between nodes in CS trees.
(parent, child, grandparent, sibling, etc.)
A binary tree is a tree in which each node has up to two children.
The binary tree “Node” must store a payload value and maintain pointers to the left and right subtree.
The “Tree” object must store an owning pointer to the root of the tree. The tree itself has ownership responsibility for all nodes in the data structure.
A Binary Search Tree is a binary tree that has the following additional properties:
As the name implies, Binary Search Trees are optimized for searching.
Binary search trees are shaped by the order in which values are added to the container.
Order of value addition affects efficiency.
Consider these different situations:
“Random” arrival
Non-random (partially ordered) arrival
Discuss best, average, worst case search time for each.
Add items
Search for items
Traversals
In-order
pre-order
post-order
Remove items
algorithm binary search tree search (target, root):
let current := root
let answer := not_found_indicator
if target == value at current:
let answer := current
otherwise, if target < value at current:
if current has a left child:
let answer := tree search for target at current's left child.
otherwise:
if current has a right child:
let answer := tree search for target at current's right child.
return answer.
algorithm binary search tree insert (item, root):
let current := root
if item < value at current:
if current has no left child:
add new node containing item as current's left child.
otherwise:
insert item in the subtree rooted at current's left child.
otherwise:
if current has no right child:
add new node containing item as current's right child.
otherwise:
insert item in the subtree rooted at current's right child.
Traversal: In-order
algorithm binary search tree in-order traversal (root):
let current := root
if current exists:
perform in-order traversal from current's left child.
visit the current node.
perform in-order traversal from current's right child.
Traversal: pre-order
algorithm binary search tree in-order traversal (root):
let current := root
if current exists:
visit the current node.
perform in-order traversal from current's left child.
perform in-order traversal from current's right child.
Traversal: post-order
algorithm binary search tree in-order traversal (root):
let current := root
if current exists:
perform in-order traversal from current's left child.
perform in-order traversal from current's right child.
visit the current node.
Deleting an item from a binary tree is a bit more complicated, since the target item may have left and/or right subtrees attached.
We will take a look at the simple cases, and then the more complicated ones.
Figures: Drozdek, Figure 6.26 (top), Figure 6.27 (bottom).
Figures: Drozdek, Figure 6.28 (top), Figure 6.30 (bottom)
When delete by merge is used, the height of the tree may change rather dramatically, either increasing or decreasing:
Figure: Drozdek, Figure 6.31
If the value in a node is mutable, you can perform a delete by copy.
Delete by copy first locates the largest value in the left subtree OR the smallest value in the right subtree (just like delete by merge)–let’s call that value tmp
. Then it simply copies the value from tmp
into the target, then deletes node tmp
.
When delete by copy is used, the height of the tree will always either remain the same, or be reduced.
Figure: Drozdek, Figure 6.33
https://www.cs.usfca.edu/~galles/visualization/BST.html
Trees - Binary Search Trees
CS 50x2 Accelerated Programming Series