Data Structures and Algorithms. Roberto Sebastiani

Data Structures and Algorithms Roberto Sebastiani [email protected] http://www.disi.unitn.it/~rseba - Week 06 B.S. In Applied Computer...
Author: Darlene Lawson
2 downloads 2 Views 510KB Size
Data Structures and Algorithms

Roberto Sebastiani [email protected] http://www.disi.unitn.it/~rseba - Week 06 B.S. In Applied Computer Science Free University of Bozen/Bolzano academic year 2010-2011 05/24/11

M. Böhlen and R. Sebastiani

1

Acknowledgements  & Copyright Notice These slides are built on top of slides developed by Michael Boehlen. Moreover, some material (text, figures, examples) displayed in these slides is courtesy of Kurt Ranalter. Some examples displayed in these slides are taken from [Cormen, Leiserson, Rivest and Stein, ``Introduction to Algorithms'', MIT Press], and their copyright is detained by the authors. All the other material is copyrighted by Roberto Sebastiani. Every commercial use of this material is strictly forbidden by the copyright laws without the authorization of the authors. No copy of these slides can be displayed in public or be publicly distributed without containing this copyright notice.

05/24/11

M. Böhlen and R. Sebastiani

2

Data Structures and Algorithms  Week 6 ●

Binary Search Trees ● ● ● ●



Tree traversals Searching  Insertion Deletion

Red­Black Trees ● ● ● ●

05/24/11

Properties Rotations  Insertion Deletion M. Böhlen and R. Sebastiani

3

Data Structures and Algorithms  Week 6 ●

Binary Search Trees ● ● ● ●



Tree traversals Searching  Insertion Deletion

Red­Black Trees ● ● ● ●

05/24/11

Properties Rotations  Insertion Deletion M. Böhlen and R. Sebastiani

4

Dictionaries ●

A dictionary D is a dynamic data  structure with operations: – – –



Search(D, k) – returns a pointer x to an  element such that x.key = k (null otherwise) Insert(D, x) – adds the element pointed to  by x to D Delete(D, x) – removes the element pointed  to by x from D

An element has a key and data part.

05/24/11

M. Böhlen and R. Sebastiani

5

Ordered Dictionaries ●

In addition to dictionary functionality,  we may want to support operations: – –



and – –



Min(D) Max(D) Predecessor(D, k) Successor(D, k)

These operations require keys that are  comparable (ordered domain).

05/24/11

M. Böhlen and R. Sebastiani

6

A List­Based Implementation ●



Unordered list

34

14

12

22

18



search, min, max, predecessor, successor: O(n)



insertion, deletion: O(1)

Ordered list

12

14

18

22



search, insert, delete: O(n)



min, max, predecessor, successor:  O(1)

05/24/11

M. Böhlen and R. Sebastiani

34

7

Refresher: Binary Search ●

Narrow down the search range in stages –

05/24/11

findElement(22)

M. Böhlen and R. Sebastiani

8

Run Time of Binary Search ●

● ●

The range of candidate items to be searched is  halved after comparing the key with the  middle element. Binary search runs in O(log n) time. What about insertion and deletion? – – –



search: O(log n) insert, delete: O(n) min, max, predecessor, successor:  O(1)

The idea of a binary search can be extended  to dynamic data structures  binary trees.

05/24/11

M. Böhlen and R. Sebastiani

9

Binary Tree ADT (C) root

struct struct node node {{ int int val; val; struct struct node* node* left; left; struct struct node* node* right; right; struct struct node* node* parent; parent; }} struct struct node* node* root; root;

12

23

5

7 ∅ ∅ 05/24/11



8

51 ∅ ∅

11 ∅ ∅

M. Böhlen and R. Sebastiani

71 ∅

69 ∅ ∅ 10

Binary Tree ADT (C) root

class class node node {{ int int val; val; node node left; left; node node right; right; node node parent; parent; }} node node root; root;

12

23

5

7 ∅ ∅ 05/24/11



8

51 ∅ ∅

11 ∅ ∅

M. Böhlen and R. Sebastiani

71 ∅

69 ∅ ∅ 11

Binary Search Trees ●

A binary search tree (BST) is a binary tree T  with the following properties: – – –



each internal node stores an item (k,e) of a dictionary keys stored at nodes in the left subtree of v are less  than or equal to k keys stored at nodes in the right subtree of v are  greater than or equal to k

Example BSTs for 2, 3, 5, 5, 7, 8

2 3

5 3 2 05/24/11

7

7 5

5

8 M. Böhlen and R. Sebastiani

5

8 12

Data Structures and Algorithms  Week 6 ●

Binary Search Trees ● ● ● ●



Tree traversals Searching  Insertion Deletion

Red­Black Trees ● ● ● ●

05/24/11

Properties Rotations  Insertion Deletion M. Böhlen and R. Sebastiani

13

Tree Walks ●



Keys in a BST can be printed using "tree  walks" Keys of each node printed between keys in the  left and right subtree – inorder tree traversal InorderTreeWalk(x) 01   if x  NIL then 02   InorderTreeWalk(x.left) 03   print x.key 04   InorderTreeWalk(x.right)

05/24/11

M. Böhlen and R. Sebastiani

14

Tree Walks/2 ●





InorderTreeWalk is a divide­and­conquer  algorithm. It prints all elements in monotonically  increasing order. Running time Θ(n).

05/24/11

M. Böhlen and R. Sebastiani

15

Tree Walks/2 ●

4

Inorder tree walk can be thought of as  a projection of the BST nodes onto a  one dimensional interval. 5 3 2

2 05/24/11

10 5

3

5

7

5

7

M. Böhlen and R. Sebastiani

11

10

11 16

Tree Walks/3 Other forms of tree walk: ● A preorder tree walk processes each  node before processing its children. ● A postorder tree walk processes each  node after processing its children.

05/24/11

M. Böhlen and R. Sebastiani

17

Data Structures and Algorithms  Week 6 ●

Binary Search Trees ● ● ● ●



Tree traversals Searching  Insertion Deletion

Red­Black Trees ● ● ● ●

05/24/11

Properties Rotations  Insertion Deletion M. Böhlen and R. Sebastiani

18

Searching a BST 5 3

10

2

5

7

11

4 ●

To find an element with key k in a tree T – – –

05/24/11

compare k with T.key if k key (x->key key) p->key) xx == x->right; x->right; else else xx == x->left; x->left; }} if if (y (y == == NULL) NULL) {r {r == p;p->partent=null} p;p->partent=null} else if (y->key < p->key) else if (y->key < p->key) y->right y->right == p; p; else else y->left y->left == p; p; p->parent p->parent == u; u; return return r; r; }} 05/24/11

M. Böhlen and R. Sebastiani

30

BST Insertion Code (java) ●

Have a ”one step delayed” pointer.

node node insert(node insert(node p, p, node node r) r) {{ //insert //insert pp in in rr node node yy == NULL; NULL; node node xx == r; r; while while (x (x != != NULL) NULL) {{ yy := := x; x; if if (x.key (x.key key (x->key key) u->key) uu := := u->left; u->left; else else uu := := u->right; u->right; }} // // vv points points to to aa parent parent of of xx (if (if any) any) …… 05/24/11

M. Böhlen and R. Sebastiani

41

BST Deletion Code (C)/2 ● ●

……

……

x has less than 2 children Fix pointer of parent of x if if (u->right (u->right == == NULL) NULL) {{ if if (v (v == == NULL) NULL) root root == u->left; u->left; else else if if (v->left (v->left == == u) u) v->left v->left == else else v->right v->right == u->left; u->left; else else if if (u->left (u->left == == NULL) NULL) {{ if if (v (v == == NULL) NULL) root root == u->right; u->right; else else if if (v->left (v->left == == u) u) v->left v->left == else else v->right v->right == u->right; u->right; else else {{

05/24/11

M. Böhlen and R. Sebastiani

u->left; u->left;

u->right; u->right;

42

BST Deletion Code (C)/3 ●

x has 2 children

pp == x->left; x->left; qq == p; p; while while (p->right (p->right != != NULL) NULL) {{ q:=p; q:=p; if if (v (v == == NULL) NULL) root root == p; p; else else if if (v->left (v->left == == u) u) v->left v->left == else else v->right v->right == p; p; p->right p->right == u->right; u->right; if if (q (q != != p) p) {{ q->right q->right == p->left; p->left; p->left p->left == u->left; u->left; }} return return root root 05/24/11

M. Böhlen and R. Sebastiani

p:=p->right; p:=p->right; }} p; p;

43

BST Deletion Code (java) ●

Version without “parent” field

node node delete(node delete(node root, root, node node x) x) {{ uu == root; root; vv == NULL; NULL; while while (u (u != != x) x) {{ vv := := u; u; if if (x.key (x.key