Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Implementation of Tables

Similar presentations


Presentation on theme: "Advanced Implementation of Tables"— Presentation transcript:

1 Advanced Implementation of Tables
Chapter 12

2 Chapter 12 -- Advanced Implementations of Trees
Although we described the advantages of using the binary search tree, the efficiency of this implementation suffers when the tree loses its balance. This chapter introduces various search trees, which remain balanced in all situations. CS 308 Chapter Advanced Implementations of Trees

3 Chapter 12 -- Advanced Implementations of Trees
Balanced Search Trees As we saw in a previous chapter, the efficiency of the binary search tree is related to the tree’s height. The operations Retrieve, Insert, and Delete follow the path from the root of the tree to the node that contains the desired item (or to the parent of the item in the case of insert) As we learned, the height of a BST is sensitive to the order in which you insert the items. CS 308 Chapter Advanced Implementations of Trees

4 Chapter 12 -- Advanced Implementations of Trees
Consider a tree with the following nodes: 10,20, 30, 40, 50, 60, and 70 Depending upon the insertion order you could end up with: CS 308 Chapter Advanced Implementations of Trees

5 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees

6 Chapter 12 -- Advanced Implementations of Trees
Nodes are similar to a BST node CS 308 Chapter Advanced Implementations of Trees

7 Chapter 12 -- Advanced Implementations of Trees
A 2-3 Tree could look like this: CS 308 Chapter Advanced Implementations of Trees

8 Chapter 12 -- Advanced Implementations of Trees
Traversal Similar to a BST CS 308 Chapter Advanced Implementations of Trees

9 Chapter 12 -- Advanced Implementations of Trees
Searching Similar to a BST CS 308 Chapter Advanced Implementations of Trees

10 Chapter 12 -- Advanced Implementations of Trees
Inserting Quite different Let’s start with the following trees To the left is a BST, to the right is a 2-3 tree with the same elements CS 308 Chapter Advanced Implementations of Trees

11 Chapter 12 -- Advanced Implementations of Trees
Insert 39, 38, 37, … , 32 (in that order) Here is what the trees will look like, CS 308 Chapter Advanced Implementations of Trees

12 Chapter 12 -- Advanced Implementations of Trees
So, let’s walk through the insertions and figure out how it works. Insert 39 – Fairly straightforward Now, what happens when you insert 38? CS 308 Chapter Advanced Implementations of Trees

13 Chapter 12 -- Advanced Implementations of Trees
This is a little more difficult You can’t insert a third value into a node, so you have to split it (and push up the middle value) So, split 38,39,40 and push up 39 CS 308 Chapter Advanced Implementations of Trees

14 Chapter 12 -- Advanced Implementations of Trees
Insert 37 – fairly straightforward CS 308 Chapter Advanced Implementations of Trees

15 Chapter 12 -- Advanced Implementations of Trees
Insert 36 This also creates a 3 value node, so you have to split it, and push up the middle, Which creates a 3 value node, so you split it… CS 308 Chapter Advanced Implementations of Trees

16 Chapter 12 -- Advanced Implementations of Trees
And push up the middle CS 308 Chapter Advanced Implementations of Trees

17 Chapter 12 -- Advanced Implementations of Trees
Insert 35, 34, and 33 Now Insert 32 CS 308 Chapter Advanced Implementations of Trees

18 Chapter 12 -- Advanced Implementations of Trees
Insert 32 CS 308 Chapter Advanced Implementations of Trees

19 Chapter 12 -- Advanced Implementations of Trees
The Algorithm Locate the leaf node to insert into If leaf node contains only one item before insert (two after) you are done. If it contains two before (three after) you need to split the node and push the middle up CS 308 Chapter Advanced Implementations of Trees

20 Chapter 12 -- Advanced Implementations of Trees
This process (pushing up) continues until a node is reached that has only one node in it (before push up) Here is the illustration of splitting an internal node: CS 308 Chapter Advanced Implementations of Trees

21 Chapter 12 -- Advanced Implementations of Trees
Sometimes you may have to split all the way up to the root node CS 308 Chapter Advanced Implementations of Trees

22 Chapter 12 -- Advanced Implementations of Trees
Deletion This is the inverse of the insertion Start with the following: Now delete 70 CS 308 Chapter Advanced Implementations of Trees

23 Chapter 12 -- Advanced Implementations of Trees
Step 1 Make the node a leaf – swap with the inorder successor CS 308 Chapter Advanced Implementations of Trees

24 Chapter 12 -- Advanced Implementations of Trees
Step 2 – delete and fix from there. Now delete 100 CS 308 Chapter Advanced Implementations of Trees

25 Chapter 12 -- Advanced Implementations of Trees
Delete 100 Now delete 80 CS 308 Chapter Advanced Implementations of Trees

26 Chapter 12 -- Advanced Implementations of Trees
Delete 80 CS 308 Chapter Advanced Implementations of Trees

27 Chapter 12 -- Advanced Implementations of Trees
The results of deleting 70, 100, and 80 from a BST and a 2-3 tree CS 308 Chapter Advanced Implementations of Trees

28 Chapter 12 -- Advanced Implementations of Trees
Deletion Overview CS 308 Chapter Advanced Implementations of Trees

29 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees

30 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees

31 Chapter 12 -- Advanced Implementations of Trees
Insertion Insert 60, 30, 10, and 20 into an empty tree Now insert 50, 40, 70, 80, 15, 90, 100 CS 308 Chapter Advanced Implementations of Trees

32 Chapter 12 -- Advanced Implementations of Trees
50 and 40 are easy Now insert 70 CS 308 Chapter Advanced Implementations of Trees

33 Chapter 12 -- Advanced Implementations of Trees
Insert 70 Split the node (push middle up) Then Insert 70 Insert 80 and 15 (easy) Now insert 90 CS 308 Chapter Advanced Implementations of Trees

34 Chapter 12 -- Advanced Implementations of Trees
Insert 90 Split node and push middle up Then insert 90 Now Insert 100 There is a trick here…. CS 308 Chapter Advanced Implementations of Trees

35 Chapter 12 -- Advanced Implementations of Trees
Insert 100 Split the 4 node at the root. Then do the insertion as before The thing to remember – You split every 4 node on your way down for an insertion. CS 308 Chapter Advanced Implementations of Trees

36 Chapter 12 -- Advanced Implementations of Trees
Splitting a 4 node At the Root CS 308 Chapter Advanced Implementations of Trees

37 Chapter 12 -- Advanced Implementations of Trees
Splitting a 4 node Whose parent is a 2-node CS 308 Chapter Advanced Implementations of Trees

38 Chapter 12 -- Advanced Implementations of Trees
Splitting a 4 node Whose parent is a 3-node CS 308 Chapter Advanced Implementations of Trees

39 Chapter 12 -- Advanced Implementations of Trees
Deleting from a tree The deletion algorithm has the same beginning as the deletion fro a 2-3 tree. Locate the node with item I Locate the inorder successor Swap If the leaf with I is a 3-node or a 4-node, just remove it. Otherwise restructure Combine nodes and then remove I CS 308 Chapter Advanced Implementations of Trees

40 Chapter 12 -- Advanced Implementations of Trees
Red-Black Trees Represent a tree as a BST with colored links 4 nodes are easy CS 308 Chapter Advanced Implementations of Trees

41 Chapter 12 -- Advanced Implementations of Trees
3 nodes have options CS 308 Chapter Advanced Implementations of Trees

42 Chapter 12 -- Advanced Implementations of Trees
Example 2-3-4 Tree Red-Black Tree CS 308 Chapter Advanced Implementations of Trees

43 Chapter 12 -- Advanced Implementations of Trees
Code for a Red-Black Tree Node enum Color {RED, BLACK}; class TreeNode{ private: TreeItemType Item; TreeNode *left, *right; Color leftColor, rightColor; friend class RedBlackTree; }; CS 308 Chapter Advanced Implementations of Trees

44 Chapter 12 -- Advanced Implementations of Trees
Searching and traversing a Red-Black Tree Since a red-black tree is a binary search tree you can traverse it by using the algorithms for a binary search tree (you simply ignore the color of the pointers) Inserting Since a red-black tree actually represents a tree, you simply need to adjust the insertion algorithms to accommodate the red-black representation. CS 308 Chapter Advanced Implementations of Trees

45 Chapter 12 -- Advanced Implementations of Trees
Inserting (cont) Recall that you split 4 nodes you encounter on insertion. CS 308 Chapter Advanced Implementations of Trees

46 Chapter 12 -- Advanced Implementations of Trees
If you split a 4 node whose parent is a 2 node you push up CS 308 Chapter Advanced Implementations of Trees

47 Chapter 12 -- Advanced Implementations of Trees
Splitting a 4-node whose parent is a 3-node is a little more complicated – there are a few cases Case 1 CS 308 Chapter Advanced Implementations of Trees

48 Chapter 12 -- Advanced Implementations of Trees
Case 2 CS 308 Chapter Advanced Implementations of Trees

49 Chapter 12 -- Advanced Implementations of Trees
Case 3 CS 308 Chapter Advanced Implementations of Trees

50 Chapter 12 -- Advanced Implementations of Trees
This chapter also introduces AVL Trees Named for its inventors: Adel’son-Vel’skii and Landis CS 308 Chapter Advanced Implementations of Trees

51 Chapter 12 -- Advanced Implementations of Trees
Hashing Hashing Enables access to table items in time that is relatively constant and independent of the items Hash function Maps the search key of a table item into a location that will contain the item Hash table An array that contains the table items, as assigned by a hash function CS 308 Chapter Advanced Implementations of Trees

52 Chapter 12 -- Advanced Implementations of Trees
A perfect hash function Maps each search key into a unique location Possible if all the search keys are known Collisions Occur when the hash function maps more than one item into the same array location Collision-resolution schemes Assign locations in the hash table to items with different search keys when the items are involved in a collision CS 308 Chapter Advanced Implementations of Trees

53 Chapter 12 -- Advanced Implementations of Trees
Requirements for a hash function Be easy and fast to compute Place items evenly throughout the hash table The calculation of the hash function should involve the entire search key If a hash function uses module arithmetic, the base should be prime CS 308 Chapter Advanced Implementations of Trees

54 Chapter 12 -- Advanced Implementations of Trees
Simple hash functions Selecting digits Does not distribute items evenly Folding Modulo arithmetic The table size should be prime Converting a character string to an integer If the search key is a character string, it can be converted into an integer before the hash function is applied CS 308 Chapter Advanced Implementations of Trees

55 Chapter 12 -- Advanced Implementations of Trees
Resolving Collisions Approach 1: Open addressing A category of collision resolution schemes that probe for an empty, or open, location in the hash table The size of the has table has to be increased when the table becomes full CS 308 Chapter Advanced Implementations of Trees

56 Chapter 12 -- Advanced Implementations of Trees
Approach 1: Open addressing (cont) Linear probing Searches the hash table sequentially, starting from the original location specified by the hash function Quadratic probing Searches the hash table beginning with the original location that the hash function specifies and continues at increments of 12, 22, 32, and so on Double hashing Uses two hash functions, called rehashing CS 308 Chapter Advanced Implementations of Trees

57 Chapter 12 -- Advanced Implementations of Trees
Resolving Collisions Approach 2: Restructuring the hash table The hash table can accommodate more than one item in the same location Buckets Each location in the hash table is itself an array Separate chaining Each hash table location is a linked list Successfully resolves collisions The size of the ADT table is dynamic CS 308 Chapter Advanced Implementations of Trees

58 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees

59 The Efficiency of Hashing
An analysis of the average-case efficiency Load factor  Ratio of the current number of items in the table to the maximum size of the array table Measures how full a hash table is Should not exceed 2/3 Hashing efficiency for a particular search also depends on whether the search is successful Unsuccessful searches generally require more time than successful searches CS 308 Chapter Advanced Implementations of Trees

60 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees

61 Table Traversal: An Inefficient Operation Under Hashing
For many applications, hashing provides the most efficient implementation Hashing is not efficient for Traversal in sorted order Finding the item with the smallest or largest value in its search key Range query CS 308 Chapter Advanced Implementations of Trees

62 Chapter 12 -- Advanced Implementations of Trees
Summary A 2-3 tree and a tree are variants of a binary search tree in which the balanced is easily maintained The insertion and deletion algorithms for a tree are more efficient than the corresponding algorithms for a 2-3 tree A red-black tree is a binary tree representation of a tree that requires less storage than a tree CS 308 Chapter Advanced Implementations of Trees

63 Chapter 12 -- Advanced Implementations of Trees
Summary (cont) An AVL tree is a binary search tree that is guaranteed to remain balanced Hashing as a table implementation calculates where the data item should be rather than search for it A hash function should be extremely easy to compute and should scatter the search keys evenly throughout the hash table CS 308 Chapter Advanced Implementations of Trees

64 Chapter 12 -- Advanced Implementations of Trees
Summary (cont) A collision occurs when two different search keys hash into the same array location Hashing as a table implementation does not efficiently support operations that require the table items to be ordered is simpler and faster than balanced search tree implementations when traversals are not important CS 308 Chapter Advanced Implementations of Trees

65 Chapter 12 -- Advanced Implementations of Trees
CS 308 Chapter Advanced Implementations of Trees


Download ppt "Advanced Implementation of Tables"

Similar presentations


Ads by Google