Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Lecture 5 1. Binary search tree sort 2.

Similar presentations


Presentation on theme: "Binary Search Trees Lecture 5 1. Binary search tree sort 2."— Presentation transcript:

1 Binary Search Trees Lecture 5 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 13

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 13

32 Deletion 32


Download ppt "Binary Search Trees Lecture 5 1. Binary search tree sort 2."

Similar presentations


Ads by Google