Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.

Similar presentations


Presentation on theme: "Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1."— Presentation transcript:

1 Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1

2 Binary search tree sort 2

3 Binary-search-tree property 3

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. 4

5 Binary-search-tree property 5

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. 6

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

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

9 Sorting by binary-search-tree 6 3 7 2 5 8 1212 6 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 6 4 1 2NIL 3PRINT 7 4 1212 7 7 8

10 Sorting by binary-search-tree 10

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

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

13 Searching for a key 13

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

15 Searching for minimum 15

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

17 Searching for maximum 17

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

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 19

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 20

21 Searching for successor 15 6 18 3 2 4 7 17 13 20 9 Successor of 4 search until y.left = x Case 2: The right subtree of node x is empty 21

22 Searching for successor 22  Go upper until y.left = x

23 Insertion 12 5 18 2 13 17 9 1519 Inserting an item with key 13 23

24 Insertion 24

25 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

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

27 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 27 6 7

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

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

30 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

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

32 Deletion 32

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

34 Red Black Trees 34

35 Balanced search trees 35

36 Red-black trees 36

37 Example of a red-black tree 37

38 Example of a red-black tree 38

39 Example of a red-black tree 39

40 Example of a red-black tree 40

41 Example of a red-black tree 41

42 Height of a red-black tree 42

43 Height of a red-black tree 43

44 Height of a red-black tree 44

45 Height of a red-black tree 45

46 Height of a red-black tree 46

47 Height of a red-black tree 47

48 Proof (continued) 48

49 Query operations 49

50 Modifying operations 50

51 Rotations 51

52 Insertion into a red-black tree 52

53 Insertion into a red-black tree 53

54 Insertion into a red-black tree 54

55 Insertion into a red-black tree 55

56 Insertion into a red-black tree 56

57 Pseudocode 57

58 Graphical notation 58

59 Case 1 59

60 Case 2 60

61 Case 3 61

62 Analysis 62


Download ppt "Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1."

Similar presentations


Ads by Google