SlideShare a Scribd company logo
Lecture 5:
          Dictionaries
         Steven Skiena

Department of Computer Science
 State University of New York
 Stony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena
Dictionary / Dynamic Set Operations
Perhaps the most important class of data structures maintain
a set of items, indexed by keys.
 • Search(S,k) – A query that, given a set S and a key value
   k, returns a pointer x to an element in S such that key[x]
   = k, or nil if no such element belongs to S.
 • Insert(S,x) – A modifying operation that augments the set
   S with the element x.
 • Delete(S,x) – Given a pointer x to an element in the set S,
   remove x from S. Observe we are given a pointer to an
   element x, not a key value.
• Min(S), Max(S) – Returns the element of the totally
   ordered set S which has the smallest (largest) key.
 • Next(S,x), Previous(S,x) – Given an element x whose key
   is from a totally ordered set S, returns the next largest
   (smallest) element in S, or NIL if x is the maximum
   (minimum) element.
There are a variety of implementations of these dictionary
operations, each of which yield different time bounds for
various operations.
Problem of the Day
What is the asymptotic worst-case running times for each of
the seven fundamental dictionary operations when the data
structure is implemented as
 • A singly-linked unsorted list,
 • A doubly-linked unsorted list,
 • A singly-linked sorted list, and finally
 • A doubly-linked sorted list.
Solution Blank

                      singly   singly doubly doubly
                      unsorted sorted unsorted sorted
  Search(L, k)
  Insert(L, x)
  Delete(L, x)
  Successor(L, x)
  Predecessor(L, x)
  Minimum(L)
  Maximum(L)
Solution

                      singly     double     singly   doubly
 Dictionary operation unsorted   unsorted   sorted   sorted
 Search(L, k)         O(n)       O(n)       O(n)     O(n)
 Insert(L, x)         O(1)       O(1)       O(n)     O(n)
 Delete(L, x)         O(n)∗      O(1)       O(n)∗    O(1)
 Successor(L, x)      O(n)       O(n)       O(1)     O(1)
 Predecessor(L, x)    O(n)       O(n)       O(n)∗    O(1)
 Minimum(L)           O(n)       O(n)       O(1)     O(1)
 Maximum(L)           O(n)       O(n)       O(1)∗    O(1)
Binary Search Trees
Binary search trees provide a data structure which efficiently
supports all six dictionary operations.
A binary tree is a rooted tree where each node contains at
most two children.
Each child can be identified as either a left or right child.

                             parent



                      left            right
Binary Search Trees
A binary search tree labels each node x in a binary tree such
that all nodes in the left subtree of x have keys < x and all
nodes in the right subtree of x have key’s > x.
                          2


                              3


                                  7


                              6       8


                          5




The search tree labeling enables us to find where any key is.
Implementing Binary Search Trees

typedef struct tree {
      item type item;
      struct tree *parent;
      struct tree *left;
      struct tree *right;
} tree;


The parent link is optional, since we can store the pointer on
a stack when we encounter it.
Searching in a Binary Tree: Implementation

tree *search tree(tree *l, item type x)
{
      if (l == NULL) return(NULL);

      if (l->item == x) return(l);

      if (x < l->item)
            return( search tree(l->left, x) );
      else
            return( search tree(l->right, x) );
}
Searching in a Binary Tree: How Much
The algorithm works because both the left and right subtrees
of a binary search tree are binary search trees – recursive
structure, recursive algorithm.
This takes time proportional to the height of the tree, O(h).
Maximum and Minimum
Where are the maximum and minimum elements in a binary
search tree?
Finding the Minimum

tree *find minimum(tree *t)
{
      tree *min; (* pointer to minimum *)

      if (t == NULL) return(NULL);

      min = t;
      while (min->left != NULL)
            min = min->left;
      return(min);
}


Finding the max or min takes time proportional to the height
of the tree, O(h).
Where is the Predecessor: Internal Node

                                   X




                      PREDECESSOR(X)   SUCCESSOR(X)




If X has two children, its predecessor is the maximum value
in its left subtree and its successor the minimum value in its
right subtree.
Where is the Successor: Leaf Node

                             predecessor(x)




                             X




If it does not have a left child, a node’s predecessor is its first
left ancestor.
The proof of correctness comes from looking at the in-order
traversal of the tree.
In-Order Traversal

void traverse tree(tree *l)
{
      if (l != NULL) {
            traverse tree(l->left);
            process item(l->item);
            traverse tree(l->right);
      } }


                                       H

                                  A
                                           F

                                   B               G

                                       D


                                  C            E
Tree Insertion
Do a binary search to find where it should be, then replace the
termination NIL pointer with the new item.
                            1


                                    3


                                2       7


                                    6       8


                            5




Insertion takes time proportional to the height of the tree,
O(h).
insert tree(tree **l, item type x, tree *parent)
{
      tree *p; (* temporary pointer *)

      if (*l == NULL) {
            p = malloc(sizeof(tree)); (* allocate new node *)
            p->item = x;
            p->left = p->right = NULL;
            p->parent = parent;
            *l = p; (* link into parent’s record *)
            return;
      }

      if (x < (*l)->item)
            insert tree(&((*l)->left), x, *l);
      else
            insert tree(&((*l)->right), x, *l);
}
Tree Deletion
Deletion is trickier than insertion, because the node to die
may not be a leaf, and thus effect other nodes.
There are three cases:
Case (a), where the node is a leaf, is simple - just NIL out the
parents child pointer.
Case (b), where a node has one chld, the doomed node can
just be cut out.
Case (c), relabel the node as its successor (which has at most
one child when z has two children!) and delete the successor!
Cases of Deletion

            2                             2                                  2                               2


      1                  7          1                7                 1                 7             1                7



            4                8            4                 8                4                 8             5                 8


      3                  6                           6                 3                  5            3                6

             5                             5


          initial tree           delete node with zero children (3)   delete node with 1 child (6)   delete node with 2 children (4)
Binary Search Trees as Dictionaries
All six of our dictionary operations, when implemented with
binary search trees, take O(h), where h is the height of the
tree.
The best height we could hope to get is lg n, if the tree was
perfectly balanced, since
                          lg n
                                 2i ≈ n
                          i=0
But if we get unlucky with our order of insertion or deletion,
we could get linear height!
Worst Case and Average Height

insert(a)
insert(b)
insert(c)
insert(d)


                 A


                     B


                         C


                             D
Tree Insertion Analysis
In fact, binary search trees constructed with random insertion
orders on average have Θ(lg n) height.
The worst case is linear, however.
Our analysis of Quicksort will later explain why the expected
height is Θ(lg n).
Perfectly Balanced Trees
Perfectly balanced trees require a lot of work to maintain:
                                                   9


                                       5                    13


                               3               7       11             15



                           2       4       6       8   10        12        14

                       1




If we insert the key 1, we must move every single node in the
tree to rebalance it, taking Θ(n) time.
Balanced Search Trees
Therefore, when we talk about ”balanced” trees, we mean
trees whose height is O(lg n), so all dictionary operations
(insert, delete, search, min/max, successor/predecessor) take
O(lg n) time.
Extra care must be taken on insertion and deletion to
guarantee such performance, by rearranging things when they
get too lopsided.
Red-Black trees, AVL trees, 2-3 trees, splay trees, and B-trees
are examples of balanced search trees used in practice and
discussed in most data structure texts.

More Related Content

PPT
Data Structure and Algorithms Binary Search Tree
PPTX
Binary Search Tree
PPTX
Trees (data structure)
PDF
8 chapter4 trees_bst
PDF
Sienna 7 heaps
PPT
lecture 13
PPTX
Data Structure and Algorithms Binary Search Tree
Binary Search Tree
Trees (data structure)
8 chapter4 trees_bst
Sienna 7 heaps
lecture 13

What's hot (20)

PPTX
Splay tree
PPT
1.1 binary tree
PPT
lecture 12
PDF
Algorithms notes tutorials duniya
PDF
Algorithm chapter 6
PPT
Introduction to data structure by anil dutt
PPT
binary search tree
PDF
Tree and binary tree
PPT
Review session2
PPTX
Set Operations - Union Find and Bloom Filters
PPT
Data Structure In C#
PPTX
Binary tree and Binary search tree
PDF
Absolute and Relative Clustering
PPT
Chapter 9 ds
PPTX
Binary Tree in Data Structure
PPTX
AVL Tree Data Structure
PDF
NTCIR11-Math2-PattaniyilN_poster
PPT
17. Trees and Graphs
PPTX
Splay Trees and Self Organizing Data Structures
Splay tree
1.1 binary tree
lecture 12
Algorithms notes tutorials duniya
Algorithm chapter 6
Introduction to data structure by anil dutt
binary search tree
Tree and binary tree
Review session2
Set Operations - Union Find and Bloom Filters
Data Structure In C#
Binary tree and Binary search tree
Absolute and Relative Clustering
Chapter 9 ds
Binary Tree in Data Structure
AVL Tree Data Structure
NTCIR11-Math2-PattaniyilN_poster
17. Trees and Graphs
Splay Trees and Self Organizing Data Structures
Ad

Viewers also liked (11)

PPTX
CSPro Workshop P-3
DOC
Data dictionary template
PPTX
Data Dictionary
PPTX
Data dictionary
PPTX
What is a DATA DICTIONARY?
PPTX
Systems Analyst and Design - Data Dictionary
PPS
Project report on mobile shop management
PPT
Data dictionary
PPT
Data flow diagrams (2)
PPTX
Dfd examples
PPSX
Data Flow Diagram Example
CSPro Workshop P-3
Data dictionary template
Data Dictionary
Data dictionary
What is a DATA DICTIONARY?
Systems Analyst and Design - Data Dictionary
Project report on mobile shop management
Data dictionary
Data flow diagrams (2)
Dfd examples
Data Flow Diagram Example
Ad

Similar to Skiena algorithm 2007 lecture05 dictionary data structure trees (20)

PDF
Red Black Trees
PPT
Binary Search Tree and AVL
PPT
BinarySearchTrees.ppt
PPT
BinarySearchTrees (1).ppt
PPT
data structure very BinarySearchTrees.ppt
PPT
BinarySearchTrees.ppt
PPT
BinarySearchTrees.ppt
PPT
Binary searchtrees
PDF
LEC 5-DS ALGO(updated).pdf
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
PPT
Trees
PPT
6_1 (1).ppt
PPTX
Binary trees1
PDF
CS-102 BST_27_3_14v2.pdf
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Week 8 (trees)
PPT
4a searching-more
PPT
Cinterviews Binarysearch Tree
Red Black Trees
Binary Search Tree and AVL
BinarySearchTrees.ppt
BinarySearchTrees (1).ppt
data structure very BinarySearchTrees.ppt
BinarySearchTrees.ppt
BinarySearchTrees.ppt
Binary searchtrees
LEC 5-DS ALGO(updated).pdf
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
Trees
6_1 (1).ppt
Binary trees1
CS-102 BST_27_3_14v2.pdf
Data structures and Algorithm analysis_Lecture4.pptx
Trees, Binary Search Tree, AVL Tree in Data Structures
Week 8 (trees)
4a searching-more
Cinterviews Binarysearch Tree

More from zukun (20)

PDF
My lyn tutorial 2009
PDF
ETHZ CV2012: Tutorial openCV
PDF
ETHZ CV2012: Information
PDF
Siwei lyu: natural image statistics
PDF
Lecture9 camera calibration
PDF
Brunelli 2008: template matching techniques in computer vision
PDF
Modern features-part-4-evaluation
PDF
Modern features-part-3-software
PDF
Modern features-part-2-descriptors
PDF
Modern features-part-1-detectors
PDF
Modern features-part-0-intro
PDF
Lecture 02 internet video search
PDF
Lecture 01 internet video search
PDF
Lecture 03 internet video search
PDF
Icml2012 tutorial representation_learning
PPT
Advances in discrete energy minimisation for computer vision
PDF
Gephi tutorial: quick start
PDF
EM algorithm and its application in probabilistic latent semantic analysis
PDF
Object recognition with pictorial structures
PDF
Iccv2011 learning spatiotemporal graphs of human activities
My lyn tutorial 2009
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Information
Siwei lyu: natural image statistics
Lecture9 camera calibration
Brunelli 2008: template matching techniques in computer vision
Modern features-part-4-evaluation
Modern features-part-3-software
Modern features-part-2-descriptors
Modern features-part-1-detectors
Modern features-part-0-intro
Lecture 02 internet video search
Lecture 01 internet video search
Lecture 03 internet video search
Icml2012 tutorial representation_learning
Advances in discrete energy minimisation for computer vision
Gephi tutorial: quick start
EM algorithm and its application in probabilistic latent semantic analysis
Object recognition with pictorial structures
Iccv2011 learning spatiotemporal graphs of human activities

Recently uploaded (20)

DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
PDF
COST SHEET- Tender and Quotation unit 2.pdf
PDF
MSPs in 10 Words - Created by US MSP Network
PDF
NISM Series V-A MFD Workbook v December 2024.khhhjtgvwevoypdnew one must use ...
PDF
How to Get Funding for Your Trucking Business
PPTX
New Microsoft PowerPoint Presentation - Copy.pptx
PDF
Reconciliation AND MEMORANDUM RECONCILATION
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PPTX
Amazon (Business Studies) management studies
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PDF
Power and position in leadershipDOC-20250808-WA0011..pdf
PDF
Tata consultancy services case study shri Sharda college, basrur
DOCX
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
PDF
IFRS Notes in your pocket for study all the time
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
PDF
Chapter 5_Foreign Exchange Market in .pdf
PPTX
HR Introduction Slide (1).pptx on hr intro
DOCX
Euro SEO Services 1st 3 General Updates.docx
unit 1 COST ACCOUNTING AND COST SHEET
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
COST SHEET- Tender and Quotation unit 2.pdf
MSPs in 10 Words - Created by US MSP Network
NISM Series V-A MFD Workbook v December 2024.khhhjtgvwevoypdnew one must use ...
How to Get Funding for Your Trucking Business
New Microsoft PowerPoint Presentation - Copy.pptx
Reconciliation AND MEMORANDUM RECONCILATION
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Amazon (Business Studies) management studies
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Power and position in leadershipDOC-20250808-WA0011..pdf
Tata consultancy services case study shri Sharda college, basrur
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
IFRS Notes in your pocket for study all the time
340036916-American-Literature-Literary-Period-Overview.ppt
Chapter 5_Foreign Exchange Market in .pdf
HR Introduction Slide (1).pptx on hr intro
Euro SEO Services 1st 3 General Updates.docx

Skiena algorithm 2007 lecture05 dictionary data structure trees

  • 1. Lecture 5: Dictionaries Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. Dictionary / Dynamic Set Operations Perhaps the most important class of data structures maintain a set of items, indexed by keys. • Search(S,k) – A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nil if no such element belongs to S. • Insert(S,x) – A modifying operation that augments the set S with the element x. • Delete(S,x) – Given a pointer x to an element in the set S, remove x from S. Observe we are given a pointer to an element x, not a key value.
  • 3. • Min(S), Max(S) – Returns the element of the totally ordered set S which has the smallest (largest) key. • Next(S,x), Previous(S,x) – Given an element x whose key is from a totally ordered set S, returns the next largest (smallest) element in S, or NIL if x is the maximum (minimum) element. There are a variety of implementations of these dictionary operations, each of which yield different time bounds for various operations.
  • 4. Problem of the Day What is the asymptotic worst-case running times for each of the seven fundamental dictionary operations when the data structure is implemented as • A singly-linked unsorted list, • A doubly-linked unsorted list, • A singly-linked sorted list, and finally • A doubly-linked sorted list.
  • 5. Solution Blank singly singly doubly doubly unsorted sorted unsorted sorted Search(L, k) Insert(L, x) Delete(L, x) Successor(L, x) Predecessor(L, x) Minimum(L) Maximum(L)
  • 6. Solution singly double singly doubly Dictionary operation unsorted unsorted sorted sorted Search(L, k) O(n) O(n) O(n) O(n) Insert(L, x) O(1) O(1) O(n) O(n) Delete(L, x) O(n)∗ O(1) O(n)∗ O(1) Successor(L, x) O(n) O(n) O(1) O(1) Predecessor(L, x) O(n) O(n) O(n)∗ O(1) Minimum(L) O(n) O(n) O(1) O(1) Maximum(L) O(n) O(n) O(1)∗ O(1)
  • 7. Binary Search Trees Binary search trees provide a data structure which efficiently supports all six dictionary operations. A binary tree is a rooted tree where each node contains at most two children. Each child can be identified as either a left or right child. parent left right
  • 8. Binary Search Trees A binary search tree labels each node x in a binary tree such that all nodes in the left subtree of x have keys < x and all nodes in the right subtree of x have key’s > x. 2 3 7 6 8 5 The search tree labeling enables us to find where any key is.
  • 9. Implementing Binary Search Trees typedef struct tree { item type item; struct tree *parent; struct tree *left; struct tree *right; } tree; The parent link is optional, since we can store the pointer on a stack when we encounter it.
  • 10. Searching in a Binary Tree: Implementation tree *search tree(tree *l, item type x) { if (l == NULL) return(NULL); if (l->item == x) return(l); if (x < l->item) return( search tree(l->left, x) ); else return( search tree(l->right, x) ); }
  • 11. Searching in a Binary Tree: How Much The algorithm works because both the left and right subtrees of a binary search tree are binary search trees – recursive structure, recursive algorithm. This takes time proportional to the height of the tree, O(h).
  • 12. Maximum and Minimum Where are the maximum and minimum elements in a binary search tree?
  • 13. Finding the Minimum tree *find minimum(tree *t) { tree *min; (* pointer to minimum *) if (t == NULL) return(NULL); min = t; while (min->left != NULL) min = min->left; return(min); } Finding the max or min takes time proportional to the height of the tree, O(h).
  • 14. Where is the Predecessor: Internal Node X PREDECESSOR(X) SUCCESSOR(X) If X has two children, its predecessor is the maximum value in its left subtree and its successor the minimum value in its right subtree.
  • 15. Where is the Successor: Leaf Node predecessor(x) X If it does not have a left child, a node’s predecessor is its first left ancestor. The proof of correctness comes from looking at the in-order traversal of the tree.
  • 16. In-Order Traversal void traverse tree(tree *l) { if (l != NULL) { traverse tree(l->left); process item(l->item); traverse tree(l->right); } } H A F B G D C E
  • 17. Tree Insertion Do a binary search to find where it should be, then replace the termination NIL pointer with the new item. 1 3 2 7 6 8 5 Insertion takes time proportional to the height of the tree, O(h).
  • 18. insert tree(tree **l, item type x, tree *parent) { tree *p; (* temporary pointer *) if (*l == NULL) { p = malloc(sizeof(tree)); (* allocate new node *) p->item = x; p->left = p->right = NULL; p->parent = parent; *l = p; (* link into parent’s record *) return; } if (x < (*l)->item) insert tree(&((*l)->left), x, *l); else insert tree(&((*l)->right), x, *l); }
  • 19. Tree Deletion Deletion is trickier than insertion, because the node to die may not be a leaf, and thus effect other nodes. There are three cases: Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer. Case (b), where a node has one chld, the doomed node can just be cut out. Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor!
  • 20. Cases of Deletion 2 2 2 2 1 7 1 7 1 7 1 7 4 8 4 8 4 8 5 8 3 6 6 3 5 3 6 5 5 initial tree delete node with zero children (3) delete node with 1 child (6) delete node with 2 children (4)
  • 21. Binary Search Trees as Dictionaries All six of our dictionary operations, when implemented with binary search trees, take O(h), where h is the height of the tree. The best height we could hope to get is lg n, if the tree was perfectly balanced, since lg n 2i ≈ n i=0 But if we get unlucky with our order of insertion or deletion, we could get linear height!
  • 22. Worst Case and Average Height insert(a) insert(b) insert(c) insert(d) A B C D
  • 23. Tree Insertion Analysis In fact, binary search trees constructed with random insertion orders on average have Θ(lg n) height. The worst case is linear, however. Our analysis of Quicksort will later explain why the expected height is Θ(lg n).
  • 24. Perfectly Balanced Trees Perfectly balanced trees require a lot of work to maintain: 9 5 13 3 7 11 15 2 4 6 8 10 12 14 1 If we insert the key 1, we must move every single node in the tree to rebalance it, taking Θ(n) time.
  • 25. Balanced Search Trees Therefore, when we talk about ”balanced” trees, we mean trees whose height is O(lg n), so all dictionary operations (insert, delete, search, min/max, successor/predecessor) take O(lg n) time. Extra care must be taken on insertion and deletion to guarantee such performance, by rearranging things when they get too lopsided. Red-Black trees, AVL trees, 2-3 trees, splay trees, and B-trees are examples of balanced search trees used in practice and discussed in most data structure texts.