Trees 2:
Linked Representation, Tree Traversal, and Binary Search Trees
Linked representation of binary tree
• Again, as with linked list, entire tree can be represented with a single pointer -- in this case, a pointer to the root node
• Nodes are instances of class BTnode, described in the next several slides
• The class contains methods that operate on single nodes
• Since each node is potentially the root node of a tree (or subtree), we will also include operations on entire trees
Linked representation of binary tree
Methods of BTnode class
Methods of BTnode class
Static methods operate at tree level
• Accessors (entire tree):– treeSize() returns number of nodes in tree– treeCopy() returns a new tree copied from a
source tree argument
Output methods
Constructor
Simple accessor methods
Recursive accessors
Mutators
Mutators
Static methodspublic static <E> BTNode<E> treeCopy(BTNode<E> source) {
BTNode<E> leftCopy, rightCopy;if (source == null)
return null;else {
leftCopy = treeCopy(source.left);rightCopy = treeCopy(source.right);return new BTNode<E>(source.data, leftCopy,
rightCopy);}
}
Static methods
Tree traversal
Now, more ways to climb!
Example: printing all nodes
// pre-order traversalpublic void preorderPrint( ){
System.out.println(data);if (left != null)
left.preorderPrint( );if (right != null)
right.preorderPrint( );}
Example: printing all nodes
Example: printing all nodes (part 1)
Example: printing all nodes (part 2)
Example: printing all nodes (part 3)
A forest full of trees
• The generic BTNode class we have seen thus far can be used to create many types of data structures derived from binary trees
• Now we will look at a specific ADT based on the generic binary tree: binary search trees
BST characteristics
• Based on binary trees• Defining quality has to do with the order in
which data are stored• Commonly used in database applications
where rapid retrieval of data is desired
Binary Search Trees
• Entries in a BST must be objects to which total order semantics apply -- in other words, objects for which all the binary comparison operations are defined
• Storage rules -- for every node n:– every entry in n’s left subtree is less than or equal
to the entry in n– every entry in n’s right subtree is greater than n’s
entry
Binary Search TreeUnlike a heap, a BST has no specialrequirement for the tree to maintain a particular shape –but a balancedtree (in which there are approximately the same number of nodes in each subtree) facilitates data searching
The Dictionary Data Type
• A dictionary is a collection of items, similar to a bag.
• Unlike a bag, each item is ordered according to an intrinsic or extrinsic value called the item's key.
• We have seen how a hash table might be used to implement the Dictionary ADT; now we'll implement it as binary search tree
A Dictionary of States in BST Form
• Storage rules:– Every key to the
left of a node is alphabetically before the key of the node.
– Every key to the right of a node is alphabetically after the key of the node.
Retrieving Data
• Start at the root.– If the current node
has the key, then stop and retrieve the data.
– If the current node's key is too large, move left and repeat previous steps
– If the current node's key is too small, move right and repeat previous steps
Adding
• Pretend that you are trying to find the key, but stop when there is no node to move to.
• Add the new node at the spot where you would have moved to if there had been a node.
• Where would you add the 51st state?
Adding
Removing an Item with a Given Key
• Find the item.• If necessary, swap
the item with one that is easier to remove.
• Remove the item.
Removing an Item with a Given Key
• Find the item.• If the node is not a leaf:
– If the node has 1 child, replace the parent with the child
– If the item has 2 children, rearrange the tree:• Find smallest item in the right subtree• Copy that smallest item onto the one that you want to
remove• Remove the extra copy of the smallest item (making sure
that you keep the tree connected)
• Else just remove the item.
Summary
• Binary search trees are a good implementation of data types such as sets, bags, and dictionaries.
• Searching for an item is generally quick since you move from the root to the item, without looking at many other items.
• Adding and deleting items is also quick.• But … it is possible for the quickness to fail in
some cases -- can you see why?
treebigo 37
Worst-case times for tree operations
For a tree of depth d, all of the following are O(d) applications in the worst case: adding an entry to a binary search tree or heap deleting an entry from a binary search tree or
heap search for an entry in a binary search tree (we
don’t search heaps)
treebigo 38
Heap analysis
By definition, a heap is a complete binary tree
Maximum nodes at each level: root node (level 0) : 1 (20 ) nodes root’s children (level 1): 2 (21) nodes root’s grandchildren (level 2): 4 (22) nodes At level d, there are 2d nodes
treebigo 39
Heap analysis
So, for a heap to reach depth d, it must have
(1 + 2 + 4 + … + 2(d-1)) + 1 nodes Simplifying the formula:1 + 1 + 2 + 4 + … + 2(d-1)
2 + 2 + 4 + … + 2(d-1)
4 + 4 + … + 2(d-1)
2(d-1) + 2(d-1) = 2d
treebigo 40
Worst-case times for heap operations
Since d (depth) = log22d andn (number of nodes) >= 2d
log2n >= log22d so log2n >= d Adding or deleting an entry is O(d); since
d<=log2n, worst-case scenario for heap operations is O(log2n) or just O(log n)
treebigo 41
Significance of logarithms
Logarithmic algorithms are those (such as heap operations) with worst-case time of O(log n)
For a logarithmic algorithm, doubling the input size (N) will make the time required increase by a (small) fixed number of operations
treebigo 42
Significance of logarithms
Example: adding a new entry to a heap with n entries In worst case, the algorithm may examine as
many as log2n nodes Doubling the number of nodes to 2n would
require the algorithm to examine as many as log22n nodes -- but that is just 1 more than log2n (example: log21024 = 10, log22048 = 11)
treebigo 43
Depth is not the whole story for binary search trees
Time analysis on the basis of depth is not always the most useful measure -- these two binary search trees have the same depth:
treebigo 44
Analysis based on number of entries in a BST
The maximum depth of a binary search tree is n-1 (because there must be at least one node at each level)
So the worst-case time for a binary search tree (O(d)) converts to O(n-1), or just O(n)
The problem of balance
• In the best case, the average execution time for the following operations on a binary search tree:– Search– Insertion– Deletion… is a respectable O(log N). The average actual time (mathematically beyond the scope of this course) is about twice that – still very good (and better than any sorting algorithm we’ve seen, including HeapSort)
• But worst case is O(N)
The problem of balance
• Binary search trees are usually balanced only if insertion occurs in random order:
Perfectly balanced
Unbalanced (entries wereadded in reverse order)
More typical – fairly balanced
Red-black trees: a variation on the BST
• Goal: keep the tree balanced by:– Adding an extra data field to each node –
represented as a “color,” either red or black (but really just a boolean value)
– Imposing a set of rules related to node color (see next slide)
Graphic source:Wikipedia
Red-Black Tree Rules
• Every node is either red or black• Null nodes (links from leaves) are always black• Red nodes have 2 black children (note that
this means that leaf nodes can be either redor black)
• If a node is red, its parent is black• Every path from any node to its lowest leaf’s
null link(s) contains the same number of blacknodes (not counting the origin node)
Enforcing the rules: rotation
• Operation on a parent node and one of its children– Left rotation: parent node swaps positions with its
right child– Right rotation: parent node swaps positions with
its left child
• May involve color changes• May propogate up the tree
Rotation
Source: Wikipedia
Recoloring
Thank you Wikipedia!
Red-Black Tree Live!
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
Top Related