Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root...

42
Data Structures and Algorithms 1 Binary Search Trees لثنائيةر البحث ا أشجا

Transcript of Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root...

Page 1: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Data Structures and Algorithms

1

Binary Search Trees

أشجار البحث الثنائية

Page 2: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Binary Search Trees

2

The Binary Search Tree (BST)

Deleting Nodes from a BST

Binary Search Tree ADT

Other Search Trees

Page 3: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

4. Binary Search Trees

3

BST

Page 4: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

4. The Binary Search Tree (BST)

4

A Binary Search Tree (BST) is a Dictionary

implemented as a Binary Tree. It is a form of

container that permits access by content.

It supports the following main operations: Insert : Insert item in BST

Remove : Delete item from BST

Search : search for key in BST

Page 5: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

BST

5

A BST is a binary tree that stores keys or key-data pairs in

its nodes and has the following properties:

A key identifies uniquely the node (no duplicate keys)

If (u , v , w) are nodes such that (u) is any node in the left subtreeof (v) and (w) is any node in the right subtree of (v) then:

key(u) < key(v) < key(w)

v

u w

Page 6: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Examples Of BST

6

Page 7: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Examples Of BST

7These are NOT BSTs.

Page 8: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Searching Algorithm

8

if (tree is empty)

target is not in the tree

else if (the target key is the root)

target found in root

else if (target key smaller than the root’s key)

search left sub-tree

else

search right sub-tree

Page 9: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Searching Algorithm

(Pseudo Code)

9

Searches for the item with same key as k

in the tree (t).

Bool search(t,k)

{

if (t is empty)return false;

else if (k == key(t))return true;

else if (k < key(t))

return search(tleft, k);

else

return search(tright, k);

}

Page 10: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Searching for a key

10

Search for the node containing e:

Maximum number of comparisons is tree height, i.e. O(h)

Page 11: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Building a Binary Search Tree

11

Tree created from root downward

Item 1 stored in root

Next item is attached to left tree if value is smaller or

right tree if value is larger

To insert an item into an existing tree, we must first

locate the item’s parent and then insert

Page 12: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Algorithm for Insertion

12

if (tree is empty)

insert new item as root

else if (root key matches item)

skip insertion (duplicate key)

else if (new key is smaller than root)

insert in left sub-tree

else insert in right sub-tree

Page 13: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Insertion (Pseudo Code)

13

Inserts key (k)in the tree (t)

Bool insert(t, k)

{ if (t is empty)

{

create node containing (k)and attach to (t);

return true;

}

else if (k == key(t)) return false;

else if (k < key(t)) return insert(tleft, k);

else return insert(tright, k);

}

Page 14: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Example: Building a Tree

Insert: 40,20,10,50,65,45,30

14

Page 15: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Effect of Insertion Order

15

The shape of the tree depends on the

order of insertion. Shape determines the

height (h) of the tree.

Since cost of search is O(h), the

insertion order will affect the search cost.

The previous tree is full, and h =

log2(n+1) so that search cost is O(log2n)

Page 16: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Effect of Insertion Order

16

The previous tree would look like a linked list if we have inserted in the order 10,20,30,…. Its height would be h = n and its search cost would be O(n)

O(n)O(log n)

Page 17: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Linked Representation

17

The nodes in the BST will be implemented as a linked

structure: left element right

16 45

32

40

32

16 45

40

t

Page 18: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Traversing a Binary Search Tree

18

Recursive inorder traversal of tree with root (t)

traverse ( t )

{

if (t is not empty)

traverse (tleft);

visit (t);

traverse (tright);

}

Page 19: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Find Minimum Key

19

Find the minimum key in a tree with root (t)

Minkey ( t )

{

if (tleft is not empty) return MinKey(tleft);

else return key(t);

}

Page 20: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Other Traversal Orders

20

Pre-order (a.k.a. Depth-First traversal) can be implemented

using an iterative (non-recursive) algorithm. In this case, a

stack is used

If the stack is replaced by a queue and left pointers are

exchanged by right pointers, the algorithm becomes Level-order

traversal (a.k.a. Breadth-First traversal)

باستخدام (a.k.a. Depth-First traversal))يمكن تنفيذ االجتياز قبل الترتيب

.في هذه الحالة ، يتم استخدام مكدس(. غير عودية)خوارزمية تكرارية

ينى ، تصبح إذا تم استبدال المكدس بطابور وتم تبديل المؤشرات اليسرى بال مؤشرات اليم

(k.a. Breadth-First traversal)الخوارزمية اجتياز المستوى

Page 21: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Iterative Preorder Traversal

االجتياز أثناء الترتيب التكراري

21

void iterative_preorder ( )

{

t = root;

Let s be a stack

s.push (t);

while(!s.stackIsEmpty())

{ s.pop(t); process(t->key);

if ( t right is not empty) s.push(t right);

if ( t left is not empty) s.push(t left);

}

}

Page 22: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Pre-Order Traversal

22

Traversal order: {D,B,A,C,F,E,G}

D

B F

A C E G

1

2

34

5

6 7

Page 23: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Level Order Traversal

اجتياز مستوى الترتيب

23

void levelorder ( )

{

t = root;

Let q be a queue;

q.enqueue(t);

while(!q.queueIsEmpty())

{ q.dequeue(t); process(t->key);

if ( t left is not empty) q.enqueue(t left);

if ( t right is not empty) q.enqueue(t right);

}

}

Page 24: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Level-Order Traversal

24

Traversal order: {D,B,F,A,C,E,G}

A C E G

B F

D

1

2 3

45

6 7

Page 25: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

5. Deleting Nodes from a BST

25

Page 26: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Deleting a ROOT Node

26

Page 27: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Deleting a ROOT Node

27

Page 28: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Deleting a ROOT Node (Special Case)

28

Page 29: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Deleting a ROOT Node (Alternative)

29

Page 30: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Deleting an Internal Node

30

Page 31: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Search for Parent of a Node

31

To delete a node, we need to find its parent.

To search for the parent (p) of a node (x) with key (k)

in tree (t):

Set x = t; p = null; found = false;

While (not found) and (x is not empty)

{

if k < key(x) descend left (i.e. set p = x; x = xleft)

else

if k > key(x) descend right (i.e. set p = x;x = xright)

else found = true

}

Notice that:

P is null if (k) is in the root or if the tree is empty.

If (k) is not found, p points to what should have been its parent.

Page 32: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Algorithm to remove a Node

32

Let

k = key to remove its node

t = pointer to root of tree

x = location where k is found

p = parent of a node

sx = inorder successor of x

s = child of x

Page 33: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Algorithm to remove a Node

33

Remove (t,k)

{

Search for (k) and its parent;

If not found, return;

else it is found at (x) with parent at (p):

Case (x) has two children:

Find inorder successor (sx) and its parent (p);

Copy contents of (sx) into (x);

Change (x) to point to (sx);

Now (x) has one or no children and (p) is its parent

Page 34: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Algorithm to remove a Node

34

Case (x) has one or no children:

Let (s) point to the child of (x) or null if there are no children;

If p = null then set root to null;

else if (x) is a left child of (p), set pleft = s;

else set pright = s;

Now (x) is isolated and can be deleted

delete (x);

}

Page 35: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Example: Delete Root

35

40

20

10 30

60

50 70

xp = null

Page 36: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Example: Delete Root

36

40

20

10 30

60

50 70

x

p

sx

Page 37: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Example: Delete Root

37

50

20

10 30

60

50 70

p

x

S = null

Page 38: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

Example: Delete Root

38

50

20

10 30

60

50

70

x

null

delete

Page 39: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

6. Self-Balancing Binary Search

Trees

39

Binary Search Trees have

worst case performance of O(n), and

best case performance of O(log n)

There are many other search trees that are

balanced trees.

Examples are: AVL Trees, Red-Black trees

Page 40: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

AVL Trees

40

An AVL tree is a self balancing binary search tree in which

the heights of the right subtree and left subtree of the root differ by at

most 1

the left subtree and the right subtree are themselves AVL trees

rebalancing is done when insertion or deletion causes violation of the

AVL condition.

Page 41: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

AVL Tree

41

Notice that:

N(h) = N(h-1) + N(h-2) + 1

h-1

h-2h

Page 42: Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root downward Item 1 stored in root Next item is attached to left tree if value is smaller

AVL Tree

42

tree AVLthe ofheight caseworst the is This

)(log)log(44.1

2

51

5

11)(

:numbers Fibonaccifor formula

eapproximat the use can we and series Fibonacci a is This

}1)2({}1)1({}1)({

Also

3

NONhOr

hN

hNhNhN

h