57
1 Geometric Data Structures

Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

1

GeometricData Structures

Page 2: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

2

Data Structure

Definition: A data structure is a particular way of organizing and storingdata in a computer for efficient search and retrieval, including associatedalgorithms to perform search, update, and related operations on the datastructure.

Page 3: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

3

Interval Tree

Splay Tree

Segment Tree

ListArray Hash Table

Fibonacci Heap

Binary Tree

Quad Tree

kd-Tree

Range Tree

Skip List

Partition Tree

Priority Search Tree

(a,b)-Tree

Page 4: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

4

Literature

Elmar Langetepe, Gabriel ZachmannGeometric Data Structures for Computer GraphicsA K Peters Ltd., 2006.

Mark de Berg, Otfried Cheong, Marc van Kreveld, Mark OvermarsComputational Geometry: Algorithms and ApplicationsSpringer-Verlag, third edition, 2008.

Page 5: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

5

Hanan SametFoundations of Multidimensional and Metric Data StructuresMorgan Kaufmann, 2006. 1024 pages.

Dinesh P. Mehta, Sartaj SahniHandbook of Data Structures and ApplicationsChapman & Hall/CRC, 2004. 1392 pages.

Page 6: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

6

Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford SteinIntroduction to Algorithms 3rd ed.MIT Press, 2009

Robert SedgewickAlgorithms in C++. Fundamentals, Data Structures, Sorting, Searching Pts. 1-4 3rd ed.Addison-Wesley, 1998.

Page 7: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

7

Kurt MehlhornData Structures and Algorithms 1:Sorting and SearchingSpringer, 1984.

Kurt MehlhornData Structures and Algorithms 3:Multi-Dimensional Searching and Computational GeometrySpringer, 1984.

Rolf KleinAlgorithmische Geometrie 2nd ed.Springer, 2005.

Franco Preparata, Michael ShamosComputational Geometry.An IntroductionSpringer, 1985.

Page 8: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

8

Michael Goodrich, Roberto TamassiaAlgorithm Design.Foundations, Analysis and Internet ExamplesWiley, 2001.

Thomas Ottmann, Peter WidmayerAlgorithmen und Datenstrukturen 4th ed.Spektrum, 2002.

Page 9: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

9

Abstract Data Type

Definition: An abstract data type is a set of data values and associatedoperations that are precisely specified independent of any particularimplementation.

A data structure is an actual implementation of an abstract data type.

Page 10: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

9

Abstract Data Type

Definition: An abstract data type is a set of data values and associatedoperations that are precisely specified independent of any particularimplementation.

A data structure is an actual implementation of an abstract data type.

Page 11: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

10

Dictionary

Abstract data type dictionary maintains a subset S of a universe U underthe operations

• Insert(x)• Delete(x)• Search(x)

alternative names: Lookup(x), Retrieve(x), Find(x)

This data type is also called set.

Page 12: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

11

Ordered Dictionary

Abstract data type ordered dictionary maintains a subset S of an ordereduniverse U under the operations

• Insert(x)• Delete(x)• Search(x)• Predecessor(x)• Successor(x)

This data type is also called ordered set.

Page 13: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

12

Order-Statistics Dictionary

Abstract data type order-statistics dictionary maintains a subset S of anordered universe U under the operations

• Insert(x)• Delete(x)• Search(x)• Rank(x)• Select(k)

Rank(x) computes the rank of x in S, i.e., |{s ∈ S : s≤ x}|.Select(k) computes the k-th smallest element in S.

This data type is also called dynamic order-statistics set.

Page 14: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

13

Monotone Priority Queue

Abstract data type priority queue maintains a subset S of an ordereduniverse U under the operations

• Insert(x)• Delete-Min()

Page 15: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

14

Priority Queue

Abstract data type priority queue maintains a set of pairs of items andkeys from an ordered universe U under the operations

• Insert(x,key)• Delete-Min()• Decrease-Key(x,key)

Page 16: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

15

Application:

Dijkstra’s algorithm for single source shortest paths:

Dijkstra(G(V,E),s)1 for each v ∈V2 do d[v]← ∞

3 Q.Insert(v,d[v])4 Q.Decrease-key(s,0)5 while Q is not empty6 do u← Q.Delete-min()7 for each neighbor w of u8 do if d[u]+dist(u,w) < d[w]9 then d[w]← d[u]+dist(u,w)

10 Q.Decrease-key(w,d[w])

Page 17: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

16

Disjoint Set

A disjoint-set data structure maintains a partition under the operations

• Find(x)• Union(x,y)

This data type is also called union-find data structure.

Page 18: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

17

Planar Point Location

A planar subdivision is a partitioning of the plane into vertices, edges,and faces. In the planar point location problem we are given a planarsubdivision S and a query point q and the task is to find the vertex oredge or face containing q = (qx,qy).

Page 19: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

18

Klee’s Measure Problem

Given a collection of intervals,what is the length of their union?

Given a collection of axis-aligned rectangles,what is the area of their union?

Page 20: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

18

Klee’s Measure Problem

Given a collection of intervals,what is the length of their union?

Given a collection of axis-aligned rectangles,what is the area of their union?

Page 21: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

19

Range Queries

Given a set S of n points in Rd and a family R of subspaces of Rd .Elements of R are called ranges. Report or count all elements in Sintersecting a given query range. R is called range space.

Page 22: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

20

orthogonal range searching orthogonal range searching

simplex range searching halfspace range searching

Page 23: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

21

Stabbing Queries

Given a set S of subsets of Rd . Report or count all elements in Sintersected by a given query object, e.g. a point or a line (segment).

Page 24: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

22

Interval Overlap

Maintain a set of closed intervals

I = {[x1,x′1], [x2,x′2], . . . , [xn,x′n]}w

such that an emptiness stabbing query with a closed interval

[q,q′]

can be answered quickly.

Remember, we want to check whether any ofthe intervals in I overlaps [q,q′].

Page 25: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

22

Interval Overlap

Maintain a set of closed intervals

I = {[x1,x′1], [x2,x′2], . . . , [xn,x′n]}w

such that an emptiness stabbing query with a closed interval

[q,q′]

can be answered quickly. Remember, we want to check whether any ofthe intervals in I overlaps [q,q′].

Page 26: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

23

Six cases:

Page 27: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

24

Application:

Intersection detection of axis-parallel rectangles using plane-sweep

����������������������������������������

����������������������������������������

���������������������������

���������������������������

����������������������������������������

����������������������������������������

������������������������������

������������������������������

��������������������������������������������

��������������������������������������������

������������������������������������������������������������������

������������������������������������������������������������������

������������������������������

������������������������������

����������������������������������������

�������������������������������������������������

���������������������������

������������������������������������

��������������������������������������������������������������������������������������������������������������

��������������������������������������������������������������������������������������������������������������

������������������������

������������������������

“Sweep” a horizontal line from top to bottom.Maintain the intersection intervals of the rectangles and the sweep line ina data structure appropriate for interval overlap queries.Check for overlap at the upper sides of the rectangles.

Page 28: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

25

Sweep line reaches an upper side:

����������������������������������������

����������������������������������������

?

Overlap(I) Insert(I)

Page 29: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

26

Sweep line reaches a lower side:

����������������������������������������

����������������������������������������

Delete(I)

Page 30: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree.

A binary tree without internal nodes is called empty. If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v. Aninternal node without children is a leaf. If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 31: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree. A binary tree without internal nodes is called empty.

If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v. Aninternal node without children is a leaf. If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 32: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree. A binary tree without internal nodes is called empty. If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v.

Aninternal node without children is a leaf. If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 33: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree. A binary tree without internal nodes is called empty. If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v. Aninternal node without children is a leaf.

If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 34: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree. A binary tree without internal nodes is called empty. If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v. Aninternal node without children is a leaf. If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 35: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

27

Binary Tree

Definition: A binary tree contains exactly one external node or iscomprised of three disjoint sets of nodes: An internal node v called rootnode, a binary tree called its left subtree and a binary tree called its rightsubtree. A binary tree without internal nodes is called empty. If the leftsubtree is nonempty, its root is called the left child of v. Likewise, if theright subtree is nonempty, its root is called the right child of v. Aninternal node without children is a leaf. If w is a (left or right) child of u,the node u is called the parent of w.

Examples:

internal node

root

external node

Page 36: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

28

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Page 37: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

29

Definition: The height of a node v in a binary tree is the length of thelongest path from v to a leaf.

The height of a binary tree T is the heightof the root of T .

Definition: The depth of a node v in a binary tree T is the length of thepath form the root of T to v.

Definition: A node v in a binary tree is an ancestor of a node w if wbelongs to the set of nodes of one of v’s children.

Page 38: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

29

Definition: The height of a node v in a binary tree is the length of thelongest path from v to a leaf. The height of a binary tree T is the heightof the root of T .

Definition: The depth of a node v in a binary tree T is the length of thepath form the root of T to v.

Definition: A node v in a binary tree is an ancestor of a node w if wbelongs to the set of nodes of one of v’s children.

Page 39: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

29

Definition: The height of a node v in a binary tree is the length of thelongest path from v to a leaf. The height of a binary tree T is the heightof the root of T .

Definition: The depth of a node v in a binary tree T is the length of thepath form the root of T to v.

Definition: A node v in a binary tree is an ancestor of a node w if wbelongs to the set of nodes of one of v’s children.

Page 40: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

29

Definition: The height of a node v in a binary tree is the length of thelongest path from v to a leaf. The height of a binary tree T is the heightof the root of T .

Definition: The depth of a node v in a binary tree T is the length of thepath form the root of T to v.

Definition: A node v in a binary tree is an ancestor of a node w if wbelongs to the set of nodes of one of v’s children.

Page 41: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

30

Binary Search Tree

Definition: A binary search tree is a binary tree that has a key Kassociated to each internal node such that the keys of the nodes in theleft subtree are smaller than K and the keys of the nodes in the rightsubtree are larger than K.

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Binary search trees implement the abstract data type ordered dictionary.

Page 42: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

30

Binary Search Tree

Definition: A binary search tree is a binary tree that has a key Kassociated to each internal node such that the keys of the nodes in theleft subtree are smaller than K and the keys of the nodes in the rightsubtree are larger than K.

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Binary search trees implement the abstract data type ordered dictionary.

Page 43: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

31

Search

To search for key K, start at the root andwalk down the tree. Use the key Kv of thecurrent node v to navigate through the tree:If K = Kv, we are done. If K < Kv, go to theleft subtree, if K > Kv go to the right subtree.In the figure on the right we search for key H.

[Source: Sedgewick; Algorithmenin Java. c©Pearson Education]

Lemma:Searching for a key in a binary search tree of height h takes time O(h).Here and in the sequel we assume that comparing keys takes constant time.

Page 44: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

31

Search

To search for key K, start at the root andwalk down the tree. Use the key Kv of thecurrent node v to navigate through the tree:If K = Kv, we are done. If K < Kv, go to theleft subtree, if K > Kv go to the right subtree.In the figure on the right we search for key H.

[Source: Sedgewick; Algorithmenin Java. c©Pearson Education]

Lemma:Searching for a key in a binary search tree of height h takes time O(h).Here and in the sequel we assume that comparing keys takes constant time.

Page 45: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

32

Insert

To insert a key K, we search for K asdescribed above. If we find K, we aredone. Otherwise, the search will end inan external node. We replace theexternal node by a new internal node vand associate K with v. In the figure onthe right we insert key M.

[Source: Sedgewick; Algorithmenin Java. c©Pearson Education]

Lemma:Insertion of a key in a binary search tree of height h takes time O(h).

Page 46: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

32

Insert

To insert a key K, we search for K asdescribed above. If we find K, we aredone. Otherwise, the search will end inan external node. We replace theexternal node by a new internal node vand associate K with v. In the figure onthe right we insert key M.

[Source: Sedgewick; Algorithmenin Java. c©Pearson Education]

Lemma:Insertion of a key in a binary search tree of height h takes time O(h).

Page 47: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

33

Delete

To delete a key K, we search for K as describedabove. If we do not find K, we are done.Otherwise, let vK be the node containing K. If vKhas at most one child, we remove vK . If there is achild vc, we make vc a child of vK ’s parent.Otherwise, if vK has two children, we search forK’s successor, i.e., for the leftmost internal node vlin vK ’s right subtree. Node vl has at most onechild! We exchange the keys of vK and vl andremove vl (handling the child as described above,if any). In the figure we delete key E. The key ofthe leftmost internal node in the right subtree is G.

[Source: Sedgewick;Algorithmen in Java.c©Pearson Education]

Lemma:

Deletion of a key in a binary search tree of height h takes time O(h).

Page 48: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

33

Delete

To delete a key K, we search for K as describedabove. If we do not find K, we are done.Otherwise, let vK be the node containing K. If vKhas at most one child, we remove vK . If there is achild vc, we make vc a child of vK ’s parent.Otherwise, if vK has two children, we search forK’s successor, i.e., for the leftmost internal node vlin vK ’s right subtree. Node vl has at most onechild! We exchange the keys of vK and vl andremove vl (handling the child as described above,if any). In the figure we delete key E. The key ofthe leftmost internal node in the right subtree is G.

[Source: Sedgewick;Algorithmen in Java.c©Pearson Education]

Lemma:

Deletion of a key in a binary search tree of height h takes time O(h).

Page 50: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

35

Lemma:The worst-case height of a binary search tree for n keys is Θ(n).

Search O(n) worst-case

Insert O(n) worst-case

Delete O(n) worst-case

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Page 51: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

36

Balanced Binary Search Trees

Definition: A node v in an binary search tree T is α-weight-balanced,for some 1

2 ≤ α < 1, if

size(lc[v]) ≤ α · size(v) (1)

size(rc[v]) ≤ α · size(v) (2)

Here and later on, lc(v) and rc(v) denote the left and right child of a node v, respectively.

Definition: A binary search tree T is α-weight-balanced if all nodes in Tare α-weight-balanced.

Definition: A binary search tree T of size n is α-height-balanced if

h(T ) ≤ blog(1/α) nc=: hα(n) (3)

Page 52: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

36

Balanced Binary Search Trees

Definition: A node v in an binary search tree T is α-weight-balanced,for some 1

2 ≤ α < 1, if

size(lc[v]) ≤ α · size(v) (1)

size(rc[v]) ≤ α · size(v) (2)

Here and later on, lc(v) and rc(v) denote the left and right child of a node v, respectively.

Definition: A binary search tree T is α-weight-balanced if all nodes in Tare α-weight-balanced.

Definition: A binary search tree T of size n is α-height-balanced if

h(T ) ≤ blog(1/α) nc=: hα(n) (3)

Page 53: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

36

Balanced Binary Search Trees

Definition: A node v in an binary search tree T is α-weight-balanced,for some 1

2 ≤ α < 1, if

size(lc[v]) ≤ α · size(v) (1)

size(rc[v]) ≤ α · size(v) (2)

Here and later on, lc(v) and rc(v) denote the left and right child of a node v, respectively.

Definition: A binary search tree T is α-weight-balanced if all nodes in Tare α-weight-balanced.

Definition: A binary search tree T of size n is α-height-balanced if

h(T ) ≤ blog(1/α) nc=: hα(n) (3)

Page 54: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

37

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Page 55: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

38

Rotation

You can restructure a tree using rotations as illustrated below.Rotations preserve the ordering of the keys associated with the nodes.

yx

y x

Page 56: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

39

Right Rotation

Left Rotation

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]

Page 57: Geometric Data Structures - NTUAgtsat/collection/computational... · Data Structures and Algorithms 1: Sorting and Searching Springer, 1984. Kurt Mehlhorn Data Structures and Algorithms

39

Right Rotation Left Rotation

[Source: Sedgewick; Algorithmen in Java. c©Pearson Education]