Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures...

52
Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Transcript of Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures...

Page 1: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Trees 2:

Linked Representation, Tree Traversal, and Binary Search Trees

Page 2: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 3: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Linked representation of binary tree

Page 4: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Methods of BTnode class

Page 5: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Methods of BTnode class

Page 6: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 7: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Output methods

Page 8: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Constructor

Page 9: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Simple accessor methods

Page 10: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Recursive accessors

Page 11: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Mutators

Page 12: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Mutators

Page 13: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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);}

}

Page 14: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Static methods

Page 15: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Tree traversal

Page 16: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Now, more ways to climb!

Page 17: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Example: printing all nodes

// pre-order traversalpublic void preorderPrint( ){

System.out.println(data);if (left != null)

left.preorderPrint( );if (right != null)

right.preorderPrint( );}

Page 18: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST
Page 19: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Example: printing all nodes

Page 20: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST
Page 21: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Example: printing all nodes (part 1)

Page 22: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Example: printing all nodes (part 2)

Page 23: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Example: printing all nodes (part 3)

Page 24: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST
Page 25: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 26: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 27: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 28: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 29: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 30: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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.

Page 31: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 32: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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.

Page 33: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

• Where would you add the 51st state?

Adding

Page 34: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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.

Page 35: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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.

Page 36: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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?

Page 37: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 38: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 39: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 40: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 41: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 42: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 43: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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:

Page 44: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 45: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 46: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 47: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 48: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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)

Page 49: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

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

Page 50: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Rotation

Source: Wikipedia

Page 51: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Recoloring

Thank you Wikipedia!

Page 52: Trees 2 - kirkwood.edu 2linkedRepTravBST.pdffar can be used to create many types of data structures derived from binary trees ... the generic binary tree: binary search trees. BST

Red-Black Tree Live!

https://www.cs.usfca.edu/~galles/visualization/RedBlack.html