ch 15. binary search trees - seoul national...
TRANSCRIPT
SNU
IDB Lab.
Ch 15. Binary Search Trees
© copyright 2006 SNU IDB Lab.
2SNU
IDB 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
3SNU
IDB 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
4SNU
IDB 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
5SNU
IDB 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.
6SNU
IDB 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
7SNU
IDB 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)
8SNU
IDB 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
9SNU
IDB Lab.Data Structures
The ADT BSTree & IndexedBSTree
AbstractDataType BSTree {operations
get(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
}
10SNU
IDB 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 BSTreeextends Dictionary { public void ascend();
}
� Public interface IndexedBSTreeextends BSTree{ public Object get(int index);public Object remove(int index);
}
11SNU
IDB 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 BinarySearchTreeextends LinkedBinaryTreeimplements BSTree
extends BinaryTree interface
implementsDictionary interface
12SNU
IDB 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
13SNU
IDB 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)
14SNU
IDB 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 nodeexamined during the search
� To Put an element
with key 80
30
405
2 35 80� To Put an element
with key 35
15SNU
IDB 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’sonly 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)
16SNU
IDB Lab.Data Structures
Deleting an Element: Ex 1Remove the element with key 40
Option1: the smallest element in the right subtreetakes the position of the deleted element
30
605
2 35 80
32
31 33
85
30
405
2 35 80
32
31 33
8560
17SNU
IDB Lab.Data Structures
Deleting an Element: Ex 2Remove the element with key 40
Option2: the largest element in the left subtreetakes 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
18SNU
IDB Lab.Data Structures
Deleting an Element: Ex 3
Remove the element with key 30
Option1: the largest element in the left subtreebecomes 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
19SNU
IDB Lab.Data Structures
Deleting an Element: Ex 4
• Remove the element with key 30• Option2: the smallest element in the right subtreebecomes 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
20SNU
IDB 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)
21SNU
IDB 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;
}
22SNU
IDB 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
23SNU
IDB 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
24SNU
IDB 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 1820
12 18
15 25
22
3
1
0
0
0 0
25SNU
IDB 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
26SNU
IDB 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.
27SNU
IDB Lab.Data Structures
Histogramming (2)
k e y s [ 2 , 4 , 2 , 2 , 3 , 4 , 2 , 6 , 4 , 2 ]
N 개 Input
int[] h = new int[r+1]
1 2 3 4 5 60 11 3 05
0
0
28SNU
IDB Lab.Data Structures
Simple Histogramming code using Integer Array
public class SimpleHistogramming { public static void main(String[] args) {
int n = keyboad.readInteger(); //number of elementsint 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]);
}}
29SNU
IDB 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 tree
Else method theVisit(e) is invoked; }
}
public class Histogramming {// top-level member class
public static class ElementType { …. }// static data memberstatic method add1;// static initializerstatic { …}
// increment the count of e by 1public static void add1(object e) { ((ElementType) e).count++; }
}
30SNU
IDB 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();
}
31SNU
IDB 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)
32SNU
IDB 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
33SNU
IDB Lab.Data Structures
Table of Contents
� Binary Search Trees
� Indexed Binary Search Trees
� Binary Search Tree Application � Histogramming
� Best-Fit Bin Packing
� Crossing Distribution
34SNU
IDB 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 mannerIf (find the best bin for i from search tree)
delete it from the BSTreduce its unused capacity by objectSize[i]reinsert itinto 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)
35SNU
IDB 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 binsforms a binary search treefor 9 bins� If ObjectSize[O] is 4, we want a bin whose unused size is slightly over 4
36SNU
IDB 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
37SNU
IDB 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
38SNU
IDB 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 farwhile(currentNode != null) {
if (key in currentNode >= theKey) { bestElement = currentNode.element;currentNode = currentNode.leftChild;
} else currentNode.rightChild;}return bestElement;
}}
39SNU
IDB 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 BSTbestBin.unusedCapacity -= objectSize[i]; if (bestBin.unusedCapacity > 0)
theTree.put(new Integer(bestBin.unusedCapacity), bestBin);}
}
40SNU
IDB Lab.Data Structures
Table of Contents
� Binary Search Trees
� Indexed Binary Search Trees
� Binary Search Tree Application � Histogramming
� Best-Fit Bin Packing
� Crossing Distribution
41SNU
IDB 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 Wjcross each other
42SNU
IDB Lab.Data Structures
Channel Routing and Crossings:i = top pin num & Ki = crossing num (1)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
Fig 15.7
C=[8,7,4,2,5,1,9,3,10,6]
43SNU
IDB Lab.Data Structures
Channel Routing and Crossings:i = top pin num & Ki = crossing num (2)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
44SNU
IDB Lab.Data Structures
Channel Routing and Crossings :i = top pin num & Ki = crossing num (3)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
45SNU
IDB Lab.Data Structures
Channel Routing and Crossings :i = top pin num & Ki = crossing num (4)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
46SNU
IDB Lab.Data Structures
Channel Routing and Crossings :i = top pin num & Ki = crossing num (5)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
47SNU
IDB 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
48SNU
IDB Lab.Data Structures
Splitting the crossings (1)
Fig 15.9
1 2 3 4 5 6 7 8 9 10
Fig 15.7
0106 725
1019614
084 6 833
8 10273 4 5 6 8 1062
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
49SNU
IDB 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
1 2 3 4 5 6 7 8 9 10
Fig 15.7
50SNU
IDB Lab.Data Structures
Splitting the crossings (3)
8
10
5
9
2
Bi
7
3
6
4
1
Bi
0106 8 25
1019614
084 6 8 33
8 10274 6 22
0601
CrossingskiiCrossingskii
10
9
6
5
4
Ci
3
7
2
1
8
Ci
01005
095 6 7 8 4 4
080 3
070 2
062 3 4 5 6 8 1071
CrossingskiiCrossingskii
Top
K = 11
Bottom
K = 11
51SNU
IDB 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
52SNU
IDB 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] 1 2 3 … … .
int[] theB = new int[n+1] 1 2 3 … … .
int[] theC = new int[n+1] 1 2 3 … … .
53SNU
IDB Lab.Data Structures
Crossing Distribution using a Linear List (2)
// scan wires right to leftint currentWire = n;while (crossingsNeeded > 0) { // need more crossings in top halfif (k[currentWire] < crossingsNeeded) { // use all crossings from currentWirelist.add(k[currentWire], new Integer(currentWire));crossingsNeeded -= k[currentWire]; }
else { // use only crossingsNeeded crossings from currentWirelist.add(crossingsNeeded, new Integer(currentWire));crossingsNeeded = 0; }
currentWire--;}
// determine wire permutation at centerfor (int i = 1; i <= currentWire; i++) theX[i] = i; // first currentWire wires have same orderingfor (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 permutationfor (int i = 1; i <= n; i++) theB[i] = theC[theX[i]]; // compute bottom-half permutation
54SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (1)
� Examine the wires in the order n, n-1,…,1
� Put Ci into indexed binary search tree
61
(a)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
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
55SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (2)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Examine wire 9 & insert C9 = 10 into the tree
Pass over the root that has leftSize =1
From 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)
56SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (3)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Examine wire 8 & insert C8 = 3 into the tree
Since C8 is the smallest entry in the tree, no wires are crossed
So, k8 = 0 because there is no left subtree of node 3
(K10 = 0, K9 = 1, K8 = 0)
62
10131
(c)
57SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (4)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Examine wire 7 & insert C7 = 9 into the tree
Determine that C7 is the 3rd-smallest entry by keeping a running sum of the lefeSize values of the nodes whose right subtree we enter
Since C7 is the 3rd-smallest entry in the tree, we conclude that its bottom endpoint is to the right of two others in the tree
So, k7 = 2
(K10 = 0, K9 = 1, K8 = 0, K7 = 2)
62
10231
91
(d)
58SNU
IDB 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)
59SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (6)
66
10233
9111 52
21 41
66
10333
9211 52
21 41 71
(h) (i)
60SNU
IDB Lab.Data Structures
Counting the number of Crossingsusing Indexed BST (7)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Finally, examine wire 1 & insert C1 = 8 into the tree
The sum of the lefeSize values of the nodes whose right subtreewe enter is 6 + 1 = 7
Wire 1 has a bottom endpoint that is to the right of seven of the wires in the tree
So, 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
61SNU
IDB Lab.Data Structures
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
•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 lineto 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)
62SNU
IDB 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
63SNU
IDB 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
64SNU
IDB 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 keyObject put(Object key, Object value): Inserts value with key as the key
Object remove(Object key): Removes the node containing key
}