SNU IDB Lab. Ch 15. Binary Search Trees © copyright 2006 SNU IDB Lab.

64
SNU IDB Lab. Ch 15. Binary Search Trees © copyright 2006 SNU IDB Lab.

description

SNU IDB Lab. Data Structures 3 Bird’s-Eye View Binary search trees Similar to skip-list Search(), insert(), delete(): 0(log n) search efficiently for keys close to a specified key k: 0(n) Find the largest key k 0(n + D) in hashing, 0(log n) in skip-list Indexed binary search trees BST with a variable leftsize Allow dictionary operations by key value and by rank Ex. Get the element the with 10 th smallest key

Transcript of SNU IDB Lab. Ch 15. Binary Search Trees © copyright 2006 SNU IDB Lab.

SNUIDB Lab.

Ch 15. Binary Search Trees

© copyright 2006 SNU IDB Lab.

2SNUIDB Lab.Data Structures

Bird’s-Eye View (0) Chapter 15: Binary Search Tree

BST and Indexed BST

Chapter 16: Balanced Search Tree AVL tree: BST + Balance B-tree: generalized AVL tree

Chapter 17: Graph

3SNUIDB Lab.Data Structures

Bird’s-Eye View Binary search trees

Similar to skip-list Search(), insert(), delete(): 0(log n)

search efficiently for keys close to a specified key k: 0(n) Find the largest key < k, Find the smallest key > k 0(n + D) in hashing, 0(log n) in skip-list

Indexed binary search trees BST with a variable leftsize Allow dictionary operations by key value and by rank Ex. Get the element the with 10th smallest key

4SNUIDB Lab.Data Structures

Table of Contents Binary Search Trees

Definition Binary Search Tree Operations and Implementation Binary Search Tree With Duplicates

Indexed Binary Search Trees

Binary Search Tree Application Histogramming Best-Fit Bin Packing Crossing Distribution

5SNUIDB Lab.Data Structures

Binary Search Tree (1) A binary search tree is a binary tree that may be

empty.

A nonempty binary search tree satisfies the following properties.

1. Every element has a key(or value), and no two elements have the same keys; therefore, all keys are distinct.

2. The keys (if any) in the left subtree of the root are smaller than the key in the root.

3. The keys (if any) in the right subtree of the root are larger than the key in the root.

4. The left and right subtrees of the root are also binary search trees.

6SNUIDB Lab.Data Structures

Binary Search Tree (2)

20

12 18

15 25

22

30

405

2

60

70

8065

Not Binary Search Trees(fail to Property 4)

Binary Search TreesBinary Search Trees

7SNUIDB Lab.Data Structures

Break time Chap 12: Binary Tree and Complete Binary Tree

Chap 13: Heap Heap is a complete binary tree with some properties RemoveTop of Heap gives you “sorting”

Chap 14: Tournament Tree Tournament tree is a complete binary tree with some properties Remove & Replay of Tournament tree gives you “sorting”

Chap 15: Binary Search Tree BST is a binary tree with some properties (not necessarily CBT) Inorder traversal of BST gives you “sorting” Can be skewed and unbalanced so, chap 16 (Balanaced Tree)

8SNUIDB Lab.Data Structures

Indexed Binary Search Tree An indexed binary search tree is derived from an ordinary

binary search tree by adding the field leftSize to each tree node

leftSize: size of the left subtree Allow dictionary operations by key value and by rank

Ex. Get the element the with 10th smallest key

20

12 18

15 25

22

30

405

2

3

1

0

0

0 0

2

1

0

0

9SNUIDB Lab.Data Structures

The ADT BSTree & IndexedBSTreeAbstractDataType BSTree {

operationsget(k) : return the element with key kput(k,x) : put element x with key k into the search treeremove(k) : remove the element with key k and return itascend() : output all elements in ascending order of key

}AbstractDataType IndexedBSTree {

operationsget(k) : return the element with key kget(index) : return the indexth elementput(k,x) : put element x with key k into the search treeremove(k) : remove the element with key k and return itremove(index) : remove the indexth element and return itascend() : output all elements in ascending order of key

}

10SNUIDB Lab.Data Structures

One more review on Interfaces

The interface Dictionary { get(k), put(k,x), remove(k) }

The interface BinaryTree { isEmpty(), root(), makeTree(root, left, right), removeLsubtree(), removeRsubtree(), preOrder(), inOrder(), postOrder(), levelOrder() }

Public interface BSTree extends Dictionary { public void ascend(); }

Public interface IndexedBSTree extends BSTree { public Object get(int index); public Object remove(int index); }

11SNUIDB Lab.Data Structures

The Class BinarySearchTree (1)

The subclass: java.lang.Object dataStructures.LinkedBinaryTree Implements BinaryTree interface sEmpty(), root(), makeTree(root, left, right), removeLsubtree(),

removeRsubtree(), preOrder(), inOrder(), postOrder(), levelOrder()

The interface: BSTree (also from Dictionary) get(key), put(key,index), remove(key), ascend();

public class BinarySearchTree extends LinkedBinaryTree implements BSTree

extends BinaryTree interface

implementsDictionary interface

12SNUIDB Lab.Data Structures

The Class BinarySearchTree (2)

Each Element of BSTstatic class Data {

Object element; Comparable key; ...

} The method BinarySearchTree.ascend()

public void ascend() { inOrderOutput(); // LinkedBinaryTree.inOrderOutput()

} Complexity of inOrderOutput() = O(n) for an n-element

tree

13SNUIDB Lab.Data Structures

Searching an Element in BST

Wish to Search for thekey from root to leaf

If (root == null) search is unsuccessful;else if (thekey < key in root) only left subtree is to be searched;else if (thekey > key in root) only right subtree is to be searched;

else (thekey == key in root) search terminates successfully;

Subtrees may be searched similarly in a recursive manner

TimeComplexity = O(height)

14SNUIDB Lab.Data Structures

Inserting an Element in BST

First perform a search for theKey successful replace the old element with new one unsuccessful new element is inserted as a child of the last

node examined during the search

To Put an elementwith key 80

30

405

2 35 80 To Put an element

with key 35

15SNUIDB Lab.Data Structures

Deleting an element in BST

Case 1: If node p is in a leaf discard the leaf Case 2: If node p is in a degree 1 node

If p has no parent the root of its single subtree becomes the new search tree root

If p has parent change the pointer from parent so that it points to p’s only child

Case 3: If node p has two nonempty subtrees Replace this element with the largest element in its left subtree or

the smallest element in its right subtree

TimeComplexity = O(height)

16SNUIDB Lab.Data Structures

Deleting an Element: Ex 1 Remove the element with key 40

Option1: the smallest element in the right subtree takes the position of the deleted element

30

605

2 35 80

32

31 33

85

30

405

2 35 80

32

31 33

8560

17SNUIDB Lab.Data Structures

Deleting an Element: Ex 2 Remove the element with key 40

Option2: the largest element in the left subtree takes the position of the deleted element

30

405

2 35 80

32

31 33

85

30

5

2 35 80

32

31 33

8560

30

605

2 35 80

32 85

30

355

2 32 80

31 33 8560

18SNUIDB Lab.Data Structures

Deleting an Element: Ex 3 Remove the element with key 30

Option1: the largest element in the left subtree becomes the root

30

605

2 35 80

32 85

30

355

2 32 80

31 33 8560

30

60

35 80

32 85

5

352

32 80

31 33 8560

19SNUIDB Lab.Data Structures

Deleting an Element: Ex 4 • Remove the element with key 30

• Option2: the smallest element in the right subtree becomes the root

30

605

2 35 80

32 85

30

355

2 32 80

31 33 8560

30

605

2 35 80

85

31

355

2 32 80

33 8560

20SNUIDB Lab.Data Structures

Height of a Binary Search Tree

Worst case Height of a binary search tree with n element can become as

large as n key[1,2,3…n] in this order 0(n)

Average case O(logn)

21SNUIDB Lab.Data Structures

Binary Search Tree with Duplicates

Permitted to contain two or more elements that have the same key

Same key elements goes to the left subtree So, n elements with same key left-skewed BST whose

height n We can build the new class DBinarySearchTree easily

while (p != null) {pp = p;if (elementKey.compareTo(((Data) pp.element).key) <= 0)

p = p.leftChild;else p = p.rightChild;

}

22SNUIDB Lab.Data Structures

Table of Contents Binary Search Trees

Definition Binary Search Tree Operations and Implementation Binary Search Tree With Duplicates

Indexed Binary Search Trees

Binary Search Tree Application Histogramming Best-Fit Bin Packing Crossing Distribution

23SNUIDB Lab.Data Structures

Indexed Binary Search Tree BST with a variable leftsize Allow dictionary operations by key value and by rank

Ex. Get the element the with 10th smallest key

20

12 18

15 25

22

30

405

2

3

1

0

0

0 0

2

1

0

0

24SNUIDB Lab.Data Structures

Search in Indexed BST Looking for the element whose rank is 3

LeftSize field of the root(20) is 3 So the element we desire is in the left subtree of 20 LeftSize field of the root(15) is 1 So the element we desire is in the right subtree of 15 So the element whose rank is 3 is 18 20

12 18

15 25

22

3

1

0

0

0 0

25SNUIDB Lab.Data Structures

Table of Contents Binary Search Trees

Definition Binary Search Tree Operations and Implementation Binary Search Tree With Duplicates

Indexed Binary Search Trees

Binary Search Tree Application Histogramming Best-Fit Bin Packing Crossing Distribution

26SNUIDB Lab.Data Structures

Histogramming (1) A collection of n keys and must output a list of the distinct keys

and the number of times each occurs in the collection.

27SNUIDB Lab.Data Structures

Histogramming (2)

keys [2, 4, 2, 2, 3, 4, 2, 6, 4, 2]

N 개 Input

int[] h = new int[r+1]

1234560 11 3 05

00

28SNUIDB Lab.Data Structures

Simple Histogramming code using Integer Array

public class SimpleHistogramming { public static void main(String[] args) { int n = keyboad.readInteger(); //number of elements

int r = keyboad.readInteger(); //between 0 or r

int[] h = new int[r + 1]; // declaring histogram table for(int i=1; i <= n; i++) { System.out.println(“Enter element “ + i);

h[keyboad.readInteger()]++;}

System.out.println(“Output histogram”);for(int i=0; i <= r; i++) if(h[i] != 0) System.out.println(i + “ “ + h[i]);}

}

29SNUIDB Lab.Data Structures

Histogramming with a BST (1)

public class BSTWithVisit extends BST { public void put (Object theKey, Object theElement, Method theVisit) {

if (no element with key equal to theKey) put theElement into the search treeElse method theVisit(e) is invoked; }

}public class Histogramming { // top-level member class public static class ElementType { …. } // static data member static method add1; // static initializer static { …}

// increment the count of e by 1 public static void add1(object e) { ((ElementType) e).count++; }}

30SNUIDB Lab.Data Structures

Histogramming with a BST (2)public static void main(String [] args) { BSTWithVisit theTree = new BSTWithVisit(); for (int i = 1; i <= n; i++) { ElementType e = new ElementType

(keyboard.readInteger()); theTree.put(new MyInteger(e.key), e, theAdd1); } // output distinct elements and their counts theTree.ascend(); }

31SNUIDB Lab.Data Structures

Histogramming with BST (1)

Input [2, 4, 2, 2, 3, 4, 2, 6, 4, 2] Blue number :key , Red Number :count

21

21

41

22

41

23

41

23

41

31

1) 2) 3) 4) 5)

32SNUIDB Lab.Data Structures

Histogramming with BST (2)

6) 7) 8)23

42

31

24

42

31

24

42

31

61

9)24

43

31

61

10)25

43

31

61

Input [2, 4, 2, 2, 3, 4, 2, 6, 4, 2] Blue number :key , Red Number :count

33SNUIDB Lab.Data Structures

Table of Contents Binary Search Trees

Indexed Binary Search Trees

Binary Search Tree Application Histogramming Best-Fit Bin Packing Crossing Distribution

34SNUIDB Lab.Data Structures

Best-Fit Bin Packing Best-Fit Bin Packing

Object I is packed into the bin with the least unusedCapacity that is at least objectSize[I]

To pack n objects into bins of capacity c in the following manner If (find the best bin for i from search tree)

delete it from the BST reduce its unused capacity by objectSize[i] reinsert it into the BST

Else // (not find a bin with enough capacity) start a new bin & insert the new bin into a BST

Implementation By using a binary search tree with duplicates

O(nlogn) By using a balanced search tree such as AVL

worst case Θ(nlogn)

35SNUIDB Lab.Data Structures

Best-Fit Bin Packing Example (1)

The object O is packed into 9 bins (a, b, … g, h, i) The unused capacity of the bins forms a binary search tree for 9 bins

If ObjectSize[O] is 4, we want a bin whose unused size is slightly over 4

36SNUIDB Lab.Data Structures

Best-Fit Bin Packing Example (2)

When a new object O that is to be packed requires 4 Units At bin h : unused size = 6, so object O fit into bin h;

the candidate bin h & move to the left subtree At bin b : unused size = 3, so not adequate; move to the right subtree At bin i : unused size = 5, so object O fit into bin i; the candidate bin i Subtree is empty: the bin search terminates with the candidate bin i

37SNUIDB Lab.Data Structures

Best-Fit Bin Packing Example (3)

When a new object I that is to be packed requires 7 Units At bin h : unused size = 6, so not adequate; move to the right subtree At bin c : unused size = 12, so object I fit into bin c;

the candidate = bin c; move to the left subtree At bin d : unused size = 6, so not adequate; move to the right subtree At bin e : unused size = 8, so object I fit into bin e; the candidate = bin e Subtree is empty : the bin search terminates with the candidate bin e

38SNUIDB Lab.Data Structures

getGreaterThanOrEqual() in The class DBinarySearchTreeWithGE

// GE stands for GreaterThanEqualTopublic class DBinarySearchTreeWithGE extends DBinarySearchTree { public Object getGreaterThanOrEqual (Object thekey) { BinaryTreeNode currentNode = root;

Object bestElement = null; // element with smallest key >= theKey found so far

while(currentNode != null) {if (key in currentNode >= theKey) {

bestElement = currentNode.element; currentNode = currentNode.leftChild;

} else currentNode.rightChild; } return bestElement;

}}

39SNUIDB Lab.Data Structures

bestFitPack() in DBinaryTreeSearchTreeWithGE

public static void bestFitPack(in t [] ObjectSize, int binCapacity){ int n = objectSize.length – 1;

int binsUsed = 0;DBinaryTreeSearchTreeWithGE theTree = new DBinaryTreeSearchTreeWithGE;

for (int i = 1; i <= n; i++) { BinNode bestBin = (BinNode) theTree.getGreaterThanOrEqual (new Integer(ObjectSize[i])); if (bestBin == null) // start with a new bin bestBin = new BinNode(++binUsed, binCapacity); else // delete the best bin from the BST bestBin = (BinNode)theTree.remove(new Integer(bestBin.unusedCapacity);

// reinsert the best bin with new unused capacity into the BST bestBin.unusedCapacity -= objectSize[i]; if (bestBin.unusedCapacity > 0) theTree.put(new Integer(bestBin.unusedCapacity), bestBin);

}}

40SNUIDB Lab.Data Structures

Table of Contents Binary Search Trees

Indexed Binary Search Trees

Binary Search Tree Application Histogramming Best-Fit Bin Packing Crossing Distribution

41SNUIDB Lab.Data Structures

Crossing Distribution Routing channel with n pins on both the top and bottom of the

channel An wire Wi connects one top pin i and one bottom pin Ci Wire Wi is to the left of wire Wj iff Wi < Wj

If this is the case for circuit design, wire crossing can cause a short circuit

We want to minimize the number of crossings

Let Ki be the number of pairs (Wi, Wj) such that wire Wi and wire Wj cross each other

42SNUIDB Lab.Data Structures

Channel Routing and Crossings: i = top pin num & Ki = crossing num (1)

1234567891 0

1234567891 0

i ki Crossings i ki Crossings1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

Fig 15.7

C=[8,7,4,2,5,1,9,3,10,6]

43SNUIDB Lab.Data Structures

Channel Routing and Crossings: i = top pin num & Ki = crossing num (2)

1234567891 0

1234567891 0

i ki Crossings i ki Crossings1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

44SNUIDB Lab.Data Structures

Channel Routing and Crossings : i = top pin num & Ki = crossing num (3)

1234567891 0

1234567891 0

i ki Crossings i ki Crossings1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

45SNUIDB Lab.Data Structures

Channel Routing and Crossings : i = top pin num & Ki = crossing num (4)

1234567891 0

1234567891 0

i ki Crossings i ki Crossings1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

46SNUIDB Lab.Data Structures

Channel Routing and Crossings : i = top pin num & Ki = crossing num (5)

1234567891 0

1234567891 0

i ki Crossings i ki Crossings1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

47SNUIDB Lab.Data Structures

Distributing the Crossings Given the crossings, a special care must be taken at the crossing

points For example, putting an insulator As such, distributing the crossings within the channel is meaningful

To balance the routing complexity in the top and lower halves of the channel

The top (bottom) half should have k/2 crossings

In Fig15.7, the sum of crossings K = 22 In Fig15.9, we have exactly 11 crossings in each half of the channel

Idea: Splitting the crossings

48SNUIDB Lab.Data Structures

Splitting the crossings (1)

Fig 15.9

1234567891 0

Fig 15.7

i ki Crossings i ki Crossings

1 7 2 3 4 5 6 8 10 6 0

2 6 3 4 5 6 8 10 7 2 8 10

3 3 4 6 8 8 0

4 1 6 9 1 10

5 2 6 7 10 0

49SNUIDB Lab.Data Structures

Splitting the crossings (2)

Top Pin = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10]Permutation A = [1, 4, 6, 3, 7, 2, 9, 5, 10, 8] // from top line to center lineCenter Pin = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Permutation B = [8, 1, 2, 7, 3, 4, 5, 6, 9, 10] // from center line to bottom lineCi = BAi, 1 ≤ i ≤ n // from top line to bottom line

Fig 15.9

1234567891 0Fig 15.7

50SNUIDB Lab.Data Structures

Splitting the crossings (3)i ki Bi Crossings i ki Bi Crossing

s1 0 1 6 0 22 2 4 4 6 7 2 9 8 10

3 3 6 4 6 8 8 0 54 1 3 6 9 1 10 10

5 2 7 6 8 10 0 8i ki Ci Crossings i ki Ci Crossing

s1 7 8 2 3 4 5 6 8 10 6 0 42 0 1 7 0 5

3 0 2 8 0 64 4 7 5 6 7 8 9 0 9

5 0 3 10 0 10

Top

K = 11

Bottom

K = 11

51SNUIDB Lab.Data Structures

Crossing Distribution Algorithm

Goal Given C (the original permutation) & K (the total # of

crossings), We develop an algorithm to compute the permutations A and B

So that the top half of the channel has k/2 crossings

Time Complexity The crossing numbers ki and the total # of crossings K can be

computed in Θ(n^2) time By examining each wire pair (i, j)

The partitioning of C into A and B can then be computed in O(n^2) time

By using an ArrayLinearList

52SNUIDB Lab.Data Structures

Crossing Distribution using a Linear List (1)

// create data structures ArrayLinearList list = new ArrayLinearList(n); int [] theA = new int [n + 1]; // top-half permutation int [] theB = new int [n + 1]; // bottom-half permutation int [] theX = new int [n + 1]; // center connections int crossingsNeeded = theK / 2; // remaining num of crossings needed in top half

int[] theA = new int[n+1] 123…….

int[] theB = new int[n+1] 123…….

int[] theC = new int[n+1] 123…….

53SNUIDB Lab.Data Structures

Crossing Distribution using a Linear List (2)

// scan wires right to left int currentWire = n; while (crossingsNeeded > 0) { // need more crossings in top half if (k[currentWire] < crossingsNeeded) { // use all crossings from currentWire list.add(k[currentWire], new Integer(currentWire)); crossingsNeeded -= k[currentWire]; } else { // use only crossingsNeeded crossings from currentWire list.add(crossingsNeeded, new Integer(currentWire)); crossingsNeeded = 0; } currentWire--; }// determine wire permutation at center for (int i = 1; i <= currentWire; i++) theX[i] = i; // first currentWire wires have same ordering for (int i = currentWire + 1; i <= n; i++) // ordering of remaining wires is from list theX[i] = ((Integer) list.get(i - currentWire - 1)).intValue(); for (int i = 1; i <= n; i++) theA[theX[i]] = i; // compute top-half permutation for (int i = 1; i <= n; i++) theB[i] = theC[theX[i]]; // compute bottom-half permutation

54SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (1)

Examine the wires in the order n, n-1,…,1 Put Ci into indexed binary search tree

61

(a)

1234567891 0

1234567891 0

Examine wire 10 & insert C10 = 6 into an empty tree- The # outside the node: its leftSize value- The # inside the node: its key (or C value)k10 = 0 (because there is no left subtree

55SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (2)

1234567891 0

1234567891 0

Examine wire 9 & insert C9 = 10 into the treePass over the root that has leftSize =1From leftSize, we know that wire 9’s bottom endpoint is to the right of exactly one of the wires seen so far

So, k9 = 1 (k10 = 0, k9 = 1)

61

101

(b)

56SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (3)

1234567891 0

1234567891 0

Examine wire 8 & insert C8 = 3 into the treeSince C8 is the smallest entry in the tree, no wires are crossedSo, k8 = 0 because there is no left subtree of node 3

(K10 = 0, K9 = 1, K8 = 0)

62

10131

(c)

57SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (4)

1234567891 0

1234567891 0

Examine wire 7 & insert C7 = 9 into the treeDetermine that C7 is the 3rd-smallest entry by keeping a running sum of the lefeSize values of the nodes whose right subtree we enterSince C7 is the 3rd-smallest entry in the tree, we conclude that its bottom endpoint is to the right of two others in the treeSo, k7 = 2

(K10 = 0, K9 = 1, K8 = 0, K7 = 2)

62

10231

91(d)

58SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (5)

63

10232

9111

64

10232

9111 51

65

10233

9111 51

21

(e) (f) (g)

59SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (6)

66

10233

9111 52

21 41

66

10333

9211 52

21 41 71

(h) (i)

60SNUIDB Lab.Data Structures

Counting the number of Crossings using Indexed BST (7)

1234567891 0

1234567891 0

Finally, examine wire 1 & insert C1 = 8 into the treeThe sum of the lefeSize values of the nodes whose right subtree we enter is 6 + 1 = 7Wire 1 has a bottom endpoint that is to the right of seven of the wires in the treeSo, k1 = 7( K10 = 0, K9 = 1, K8 = 0, K7 = 2, K6 = 0,

K5 = 2 , K4 = 1 , K3 = 3 , K2 = 6 , K1 = 7)

66

10433

93

11 52

21 41

8

1

(j)

7

1

61SNUIDB Lab.Data Structures

1234567891 0

1234567891 0

•We get (h) after 9 crossings (less than 11 crossings).•Inorder traversal of (h) yields (1, 2, 3, 4, 5, 6, 9, 10) which are points in the bottom line.•We get (6, 4, 8, 3, 5, 10, 7, 9) from bottom line to top line•Insert wire s=2 after the second wire in the sequence to get the new wire sequence (6,4,2,8,3,5,10,7,9)•The remaining wires 1 through s-1 are added at the front to obtain (1,6,4,2,8,3,5,10,7,9)• theA and theB may obtained from theX• for (int i = 1; i <= n; i++) theB[i] = theC[theX[i]]; // compute bottom-half permutation

Crossing Distribution using Indexed BST (8)

62SNUIDB Lab.Data Structures

Summary (0) Chapter 15: Binary Search Tree

BST and Indexed BST

Chapter 16: Balanced Search Tree AVL tree: BST + Balance B-tree: generalized AVL tree

Chapter 17: Graph

63SNUIDB Lab.Data Structures

Summary (1) Binary search trees

Similar to skip-list Search(), insert(), delete(): 0(log n)

search efficiently for keys close to a specified key k: 0(n) Find the largest key < k, Find the smallest key > k 0(n + D) in hashing, 0(log n) in skip-list

Indexed binary search trees BST with a variable leftsize Allow dictionary operations by key value and by rank Ex. Get the element the with 10th smallest key

64SNUIDB Lab.Data Structures

Sahni class: dataStructures.BinarySearchTree(p.572)

public class BinarySearchTree extends LinkedBinaryTree {

constructors BinarySearchTree(): Constructs an empty binary search tree

methods Object get(Object key): Returns the node containing key Object put(Object key, Object value): Inserts value with key as the key Object remove(Object key): Removes the node containing key}