Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.

Similar presentations


Presentation on theme: "Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation."— Presentation transcript:

1 Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation of the binary search tree ADT Write application programs using the binary search tree ADT Design and implement a list using a BST Binary Search Trees

2 Data Structures: A Pseudocode Approach with C, Second Edition 2 7-1 Basic Concepts Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list. A binary search tree (BST) is a binary tree with following properties: All items in the left of the tree are less than the root.All items in the left of the tree are less than the root. All items in the right subtree are greater than or equal to the root.All items in the right subtree are greater than or equal to the root. Each subtree is itself a binary search tree.Each subtree is itself a binary search tree.

3 Data Structures: A Pseudocode Approach with C, Second Edition 3

4 4

5 5 7-2 BST Operations There are four basic BST operations: traversal, search, insert, and delete. Traversals Searches Insertion Deletion

6 Data Structures: A Pseudocode Approach with C, Second Edition 6 Preorder Traversal : 23 18 12 20 44 35 52 Postorder Traversal: 12 20 8 35 52 44 23 Inorder Traversal: 12 18 20 23 35 44 52

7 Data Structures: A Pseudocode Approach with C, Second Edition 7 Searches

8 Data Structures: A Pseudocode Approach with C, Second Edition 8

9 9

10 10

11 Data Structures: A Pseudocode Approach with C, Second Edition 11

12 Data Structures: A Pseudocode Approach with C, Second Edition 12

13 Data Structures: A Pseudocode Approach with C, Second Edition 13

14 Data Structures: A Pseudocode Approach with C, Second Edition 14

15 Data Structures: A Pseudocode Approach with C, Second Edition 15 Deletion To delete a node from a binary search tree, we must first locate it. There are four cases for deletion: 1.The node to be deleted has no children. All we need to do is delete the node. 2.The node to be deleted has only a right subtree. We need to delete the node and attach the right subtree to the deleted node’s parent. 3.The node to be deleted has only a left subtree. We need to delete the node and attach the left subtree to the deleted node’s parent. 4.The node to deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to a very unbalanced trees. Rather, we try to maintain existing structure by finding data to take place of the deleted data.

16 Data Structures: A Pseudocode Approach with C, Second Edition 16 When the node to deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to a very unbalanced trees. Rather, we try to maintain existing structure by finding data to take place of the deleted data. 1.We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data 2.Or we can find the smallest node in the deleted node’s right subtree and move its data to replace the deleted node’s data Regardless of which logic we choose, we will be moving data from a leaf or leaf-like node that can be deleted.

17 Data Structures: A Pseudocode Approach with C, Second Edition 17

18 Data Structures: A Pseudocode Approach with C, Second Edition 18 (continued)

19 Data Structures: A Pseudocode Approach with C, Second Edition 19

20 Data Structures: A Pseudocode Approach with C, Second Edition 20 7-3 Binary Search Tree ADT Discussion of the BST data structure includes: Data Structure Algorithms

21 Data Structures: A Pseudocode Approach with C, Second Edition 21

22 Data Structures: A Pseudocode Approach with C, Second Edition 22

23 Data Structures: A Pseudocode Approach with C, Second Edition 23

24 Data Structures: A Pseudocode Approach with C, Second Edition 24

25 Data Structures: A Pseudocode Approach with C, Second Edition 25

26 Data Structures: A Pseudocode Approach with C, Second Edition 26

27 Data Structures: A Pseudocode Approach with C, Second Edition 27

28 Data Structures: A Pseudocode Approach with C, Second Edition 28

29 Data Structures: A Pseudocode Approach with C, Second Edition 29

30 Data Structures: A Pseudocode Approach with C, Second Edition 30

31 Data Structures: A Pseudocode Approach with C, Second Edition 31

32 Data Structures: A Pseudocode Approach with C, Second Edition 32

33 Data Structures: A Pseudocode Approach with C, Second Edition 33

34 Data Structures: A Pseudocode Approach with C, Second Edition 34

35 Data Structures: A Pseudocode Approach with C, Second Edition 35

36 Data Structures: A Pseudocode Approach with C, Second Edition 36

37 Data Structures: A Pseudocode Approach with C, Second Edition 37

38 Data Structures: A Pseudocode Approach with C, Second Edition 38

39 Data Structures: A Pseudocode Approach with C, Second Edition 39

40 Data Structures: A Pseudocode Approach with C, Second Edition 40

41 Data Structures: A Pseudocode Approach with C, Second Edition 41

42 Data Structures: A Pseudocode Approach with C, Second Edition 42

43 Data Structures: A Pseudocode Approach with C, Second Edition 43

44 Data Structures: A Pseudocode Approach with C, Second Edition 44

45 Data Structures: A Pseudocode Approach with C, Second Edition 45

46 Data Structures: A Pseudocode Approach with C, Second Edition 46

47 Data Structures: A Pseudocode Approach with C, Second Edition 47

48 Data Structures: A Pseudocode Approach with C, Second Edition 48

49 Data Structures: A Pseudocode Approach with C, Second Edition 49

50 Data Structures: A Pseudocode Approach with C, Second Edition 50 7-4 BST Applications This section develops two applications that use the BST ADT. We begin the discussion of BST Applications with a simple application that manipulates integers. The second application, student list, requires a structure to hold the student's data. Integer Application Student List Application

51 Data Structures: A Pseudocode Approach with C, Second Edition 51

52 Data Structures: A Pseudocode Approach with C, Second Edition 52

53 Data Structures: A Pseudocode Approach with C, Second Edition 53

54 Data Structures: A Pseudocode Approach with C, Second Edition 54

55 Data Structures: A Pseudocode Approach with C, Second Edition 55

56 Data Structures: A Pseudocode Approach with C, Second Edition 56

57 Data Structures: A Pseudocode Approach with C, Second Edition 57

58 Data Structures: A Pseudocode Approach with C, Second Edition 58

59 Data Structures: A Pseudocode Approach with C, Second Edition 59

60 Data Structures: A Pseudocode Approach with C, Second Edition 60

61 Data Structures: A Pseudocode Approach with C, Second Edition 61

62 Data Structures: A Pseudocode Approach with C, Second Edition 62

63 Data Structures: A Pseudocode Approach with C, Second Edition 63

64 Data Structures: A Pseudocode Approach with C, Second Edition 64

65 Data Structures: A Pseudocode Approach with C, Second Edition 65

66 Data Structures: A Pseudocode Approach with C, Second Edition 66

67 Data Structures: A Pseudocode Approach with C, Second Edition 67

68 Data Structures: A Pseudocode Approach with C, Second Edition 68

69 Data Structures: A Pseudocode Approach with C, Second Edition 69

70 Data Structures: A Pseudocode Approach with C, Second Edition 70

71 Data Structures: A Pseudocode Approach with C, Second Edition 71

72 Data Structures: A Pseudocode Approach with C, Second Edition 72

73 Data Structures: A Pseudocode Approach with C, Second Edition 73


Download ppt "Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation."

Similar presentations


Ads by Google