Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.

Similar presentations


Presentation on theme: "Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk."— Presentation transcript:

1 Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk

2 Binary search tree sort

3 Binary-search-tree property

4 Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields. The three pointer fields left, right and p point to the nodes corresponding to the left child, right child and the parent respectively. NIL in any pointer field signifies that there exists no corresponding child or parent. The root node is the only node in the BTS structure with NIL in its p field.

5 Binary-search-tree property

6 Inorder-tree walk During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit.

7 Preorder-tree walk In this case we visit the root node before the nodes in either subtree.

8 Preorder-tree walk We visit the root node after the nodes in its subtrees.

9 Sorting by binary-search-tree 5 3 7 2 5 8 1212 5 3 1212 1 NIL 3PRINT 2 4 NIL 3 PRINT 3 4 1 2NIL 3PRINT 5 4NIL 2 1 2NIL 3PRINT 8 4NIL 5 3 PRINT 5 4 1 2NIL 3PRINT 7 4 1212 7 7 8

10 Sorting by binary-search-tree

11 Sorting by binary-search- tree(contnd.)

12 Searching for a key 15 6 18 3 2 4 7 17 13 20 9 Search for 13

13 Searching for a key

14 Searching for minimum 15 6 18 3 2 4 7 17 13 20 9 Minimum

15 Searching for minimum

16 Searching for maximum 15 6 18 3 2 4 7 17 13 20 9 Maximum

17 Searching for maximum

18 Searching for successor The successor of a node x is the node with the smallest key greather than key [ x ]

19 Searching for successor 15 6 18 3 2 4 7 17 13 20 9 Successor of 15 Case 1: The right subtree of node x is nonempty

20 Searching for successor 15 6 18 3 2 4 7 17 13 20 9 Successor of 13 Case 2: The right subtree of node x is empty

21 Searching for successor

22 Insertion 12 5 18 2 13 17 9 1519 Insering an item with key 13

23 Insertion

24 Deletion 15 5 16 2 18 12 23 20 Deleting an item with key 13 (z has no children) 10 13 z 5 3 6 7

25 Deletion 15 5 16 2 18 12 23 20 Deleting an item with key 13 (z has no children) 10 5 3 6 7

26 Deletion 15 5 16 z 2 18 12 23 20 Deleting a node with key 16 (z has only one child ) 10 13 5 3 6 7

27 Deletion 15 5 2 12 Deleting a node with key 16 (z has only one child ) 10 13 5 3 6 7 18 23 20

28 Deletion 15 5 16 2 18 12 23 20 Deleting a node with key 5 (z has two children) 10 z 5 3 6 y 7 13

29 Deletion 15 5 16 2 18 12 23 20 Deleting a node with key 5 (z has two children) 10 z 5 3 6 y 7

30 Deletion 15 5 16 2 18 12 23 20 Deleting a node with key 5 (z has two children) 10 6 3 7

31 Deletion

32 Analysis of BST sort The expected time to built the tree is asymptotically the same as the running time of quicksort

33 Hight of a randomly built binary search tree

34 Convex functions

35 Jensens’s inequality Proof: We assume that f has a Taylor expansion with µ=E(X).

36 Analysis of BST height

37

38 Analysis(contd.)

39 Exponential height recurrence

40

41

42

43

44 Solving the recurrence

45

46 Solving the recurrence (contnd.)

47

48

49

50


Download ppt "Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk."

Similar presentations


Ads by Google