36
DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION 1 Chapter 7 Chapter 7 Objectives Upon completion you will be able to: 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 Design and implement threaded trees Binary Search Trees Binary Search Trees

Binary-search_tree

Embed Size (px)

DESCRIPTION

Binary Search Tree Insertion and Deletion Algorithm.

Citation preview

Page 1: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

1

Chapter 7Chapter 7

Objectives

Upon completion you will be able to:• 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• Design and implement threaded trees

Binary Search TreesBinary Search Trees

Page 2: Binary-search_tree

A Binary Search Tree is a binary tree with the following properties:

All items in the left subtree are less than the root.

All items in the right subtree are greater or equal to the root.

Each subtree is itself a binary search tree.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

2

Page 3: Binary-search_tree

Basic Property

In a binary search tree,

the left subtree contains key values less than the root

the right subtree contains key values greater than or equal to the root.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

3

Page 4: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

4

7-1 Basic Concepts

Binary search trees provide an excellent structure for searching Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into a list and at the same time for inserting and deleting data into the list.the list.

Page 5: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

5

Page 6: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

6

(a), (b) - complete and balanced trees;

(d) – nearly complete and balanced tree;

(c), (e) – neither complete nor balanced trees

Page 7: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

7

Page 8: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

8

7-2 BST Operations

We discuss four basic BST operations: traversal, search, insert, We discuss four basic BST operations: traversal, search, insert, and delete; and develop algorithms for searches, insertion, and and delete; and develop algorithms for searches, insertion, and deletion.deletion.

• Traversals• Searches• Insertion• Deletion

Page 9: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

9

Page 10: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

10

Page 11: Binary-search_tree

Preorder Traversal

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

11

23 18 12 20 44 35 52

Page 12: Binary-search_tree

Postorder Traversal

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

12

12 20 18 35 52 44 23

Page 13: Binary-search_tree

Inorder Traversal

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

13

12 18 20 23 35 44 52

Inorder traversal of a binary search tree produces a sequenced list

Page 14: Binary-search_tree

Right-Node-Left Traversal

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

14

52 44 35 23 20 18 12

Right-node-left traversal of a binary search tree produces a descending sequence

Page 15: Binary-search_tree

Three BST search algorithms: Find the smallest node

Find the largest node

Find a requested node

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

15

Page 16: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

16

Page 17: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

17

Page 18: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

18

Page 19: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

19

Page 20: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

20

Page 21: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

21

Page 22: Binary-search_tree

BST Insertion

To insert data all we need to do is follow the branches to an empty subtree and then insert the new node.

In other words, all inserts take place at a leaf or at a leaflike node – a node that has only one null subtree.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

22

Page 23: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

23

Page 24: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

24

Page 25: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

25

Page 26: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

26

30

30 30

30

Page 27: Binary-search_tree

Deletion

There are the following possible cases when we delete a node: The node to be deleted has no children. In this case, all we need to do is delete the node.

The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node’s parent.

The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent.

The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

27

Page 28: Binary-search_tree

Deletion from the middle of a tree

Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

28

Page 29: Binary-search_tree

Deletion from the middle of a tree

We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data.

We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data.

Either of these moves preserves the integrity of the binary search tree.

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

29

Page 30: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

30

Page 31: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

31

(continued)

Page 32: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

32

27 27

27 27

Page 33: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

33

7-3 Binary Search Tree ADT

We begin this section with a discussion of the BST data structure We begin this section with a discussion of the BST data structure and write the header file for the ADT. We then develop 14 and write the header file for the ADT. We then develop 14 programs that we include in the ADT.programs that we include in the ADT.

• Data Structure• Algorithms

Page 34: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

34

Page 35: Binary-search_tree

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

35

Page 36: Binary-search_tree

Homework: Preparation for the final test

Chapter 6 (pp. 265-282)

Chapter 7 (Sections 7.1; 7.2)

p. 292: ex. 1; p. 293: ex. 6, ex. 12

P. 337: ex. 3, ex. 4, ex.6, ex. 7

P. 338: ex. 13, ex. 14

DATA STRUCTURES: A PSEUDOCODE APPROACH WITH C, SECOND EDITION

36