data structures( 数据结构 ) course 8: search trees. 2 西南财经大学天府学院 chapter 8...

13
Data Structures( Data Structures( 数数数数 数数数数 ) ) Course 8: Search Trees Course 8: Search Trees

Upload: lenard-roberts

Post on 27-Dec-2015

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

Data Structures(Data Structures( 数据结数据结构构 ))

Course 8: Search TreesCourse 8: Search Trees

Page 2: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

2西南财经大学天府学院

Chapter 8 search treesChapter 8 search trees

Binary search trees and AVL trees 8-1 Binary search treesProblem: Store ordered data in an array structure :

efficient search algorithm, inefficient insertion and deletion

Linked list: efficient insertion and deletion ,inefficient search

Binary search trees can satisfy both of themDefinition: the properties of a binary search tree1. All items in the left subtree are less than the root2. All items in the right subtree are greater than or

equal to the root3. Each subtree is itself a binary search tree.

(a)

(d)(c)

(b)

1717

6 19

17

196

143

17

6

3

17

19

22

(e)

Figure 8-2 binary search trees

(a)

(d)(c)

(b)

17

6 11

17

196

223

17

22 19

17

196

1511

Figure 8-3 invalid binary search trees

Page 3: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

3西南财经大学天府学院

binary search tree traversals Algorithms are identical to the ones in chapter 7

Operations on binary search Operations on binary search treestrees

binary search tree search algorithmsFind smallest nodeFind largest nodeFind requested node

Follow the left branches until we get to a leafAlgorithm findsmallestBST( val root <pointer>)To find the smallest node in a BSTPre root is a pointer to a nonempty BST or subtreeReturn address of smallest node1 if (root->left null) 1 return(root)2 endif3 return findsmallestBST (root->left)end findsmallestBST

Follow the right branches to the last node in the tree Algorithm findlargestBST( val root <pointer>)To find the largest node in a BSTPre root is a pointer to a nonempty BST or subtreeReturn address of largest node1 if (root->right null) 1 return(root)2 endif3 return findlargestBST (root->right)end findlargestBST

Assume: To find T, Comparing T with root, T<root go left T>root go rightAlgorithm searchBST(val root <pointer>, val argument <key>)Search a binary search tree for a given valuePre root is the root to a binary tree or subtree argument is the key value requested Return the node address if the value is foundNull if the node is not in the tree1 if (root is null) 1 return null2 end if3 if (argument < root->key ) 1 return searchBST (root->left, argument)4 else if (argument > root->key ) 1 return searchBST (root->right, argument)5 else 1 return root6 end if end searchBST

23

4418

2012

Figure 8-4 a binary search trees

5235

Preorder traversal: 23 18 12 20 44 35 52

Postorder traversal: 12 20 18 35 52 44 23

Inorder traversal : 12 18 20 23 35 44 52

Page 4: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

4西南财经大学天府学院

Insert nodeInsert node

Steps: 1. Follow the branches to an empty subtree

2. Insert the new node All inserts take place at a leaf or a leaflike node that has only one null branch

23

4418

2012 5235

After inserting 19

23

4418

2012 5235

19

After inserting 38

23

4418

2012 5235

19 38

After inserting 23

23

4418

2012 5235

19 3823

Note: node with equal values are found in the right subtree

Page 5: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

5西南财经大学天府学院

Iterative insert (algorithm)Iterative insert (algorithm)

1 if (root is null) 1 root = new2 else 1 pwalk = root 2 loop ( pwalk not null ) 1 parent = pwalk 2 if ( new->key < pwalk->key ) 1 pwalk = pwalk->left 3 else 1 pwalk = pwalk->right 4 end if 3 end loopLocation for new node found 4 if (new->key < parent->key ) 1 parent->left = new 5 else 1 parent->right = new 6 end if3 end if4 returnend insertBST

Algorithm insertBST ( ref root <pointer>, val new <pointer> )Insert node containing new data into BST using iterationPre root is address of first node in a BST new is address of node containing data to be insertedPost new node inserted into the tree

Algorithm 8-4 iterative binary search tree insert

23

18

12 20

19new parent

Final position when pwalk null

null

pwalk

Page 6: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

6西南财经大学天府学院

Recursive insert Recursive insert node(algorithm)node(algorithm)

Algorithm addBST ( ref root <pointer>, val new <pointer> )Insert node containing new data into BST using recursionPre root is address of current node in a BST new is address of node containing data to be insertedPost new node inserted into the tree1 if (root is null) 1 root = new2 else locate null subtree for insertion 1 if ( new->key < root->key ) 1 addBST (root->left, new ) 2 else 1 addBST (root->right, new ) 3 end if 3 end if4 returnend insertBST

algorithm 8-5 add node to BST recursively

Page 7: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

7西南财经大学天府学院

Delete nodeDelete node

1.locate it2.delete: there are 4 possible cases

The node has no children: set the delete node’s parent to null, recycle its memory , and returnHas only a right subtree: attach the right subtree to the delete node’s parent and recycle its memory.Has only a left subtree: attach the left subtree to the delete node’s parent and recycle its memory.Has two subtree: to delete a node from the middle of a tree, to keep the balance, we must find a node,…, two ways:

Find the largest node in the deleted node’s left subtree, move its data to replace the deleted node’s dataFind the smallest node in the deleted node’s right subtree, move its data to replace the deleted node’s data

Regardless of which logic we use, we will be moving data from a leaf or leaflike node that can be deleted.

Page 8: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

8西南财经大学天府学院

(c) Delete node (18) with no left child

23

18 44

before dltptr

12

after

52

23

12 44

52

(b) Delete node (44) with no left child

23

18 44

before dltptr

12

23

18 52

12

after

52

dltptr

(a) Delete node (44) that has no children

before 23

18 44

23

18

after

(d) Delete node (23) with two children

23

12 44

before dltptr

8

after

52

14

12 44

5214 8

tree

Page 9: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

9西南财经大学天府学院

1 if (root is null) 1 return false2 endif3 if(dltkey < root->data.key ) 1 return deleteBST (root->left,dltkey)4 elseif (dltkey > root->data.key ) 1 return deleteBST (root->right,dltkey)5 else //delete node found—test for leaf node 1 if (root->left null) 1 dltptr = root 2 root = root->right 3 recycle ( dltptr ) 4 return true 2 elseif ( root->right null ) 1 dltptr = root 2 root = root->left 3 recycle ( dltptr ) 4 return true 3 else // node to be deleted not a leaf (or leaflike), find largest node on left subtree 1 dltptr = root->left 2 loop ( dltptr->right not null ) 1 dltptr = dltptr->right // node found, move data and delete leaf node 3 root->data = dltptr->data 4 return deleteBST (root->left,dltptr->data.key) 4 end if6 endifend deleteBST

Algorithm deleteBST ( ref root <pointer>, val dltKey <key> ) Delete a node from a BST Pre root is pointer to tree containing data to be deleted dltkey is key of node to be deletedPost node deleted and memory recycled, if dltkey not found, root unchangedReturn true if node deleted, false if not found

Base case 1Node not find

Base case 2After node deleted

Page 10: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

10西南财经大学天府学院

SummarySummary

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 than or equal to the root.Each subtree is itself a binary search tree.The inorder traversal of the binary search tree produces an ordered list.In a binary search tree, the node with the larges value is the rightmost node. To find the largest node, we follow the right branches until we get to a null right pointer.To search for a value in a binary search tree ,we follow the left or right branch down the tree ,depending on the value of the new node, until we find a null subtree.

Page 11: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

11西南财经大学天府学院

SummarySummary

To delete a node from a subtree, we must consider four cases:

The node to be deleted has no children.The node to be deleted has only a left subtree.The node to be deleted has only a right subtree.The node to be deleted has two subtrees.

Page 12: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

12西南财经大学天府学院

ExercisesExercises

Create a binary search tree using the following data entered as sequence set :7,10,14,23,33,56,66,70,80Insert 44 and 50 into the tree created in above exerciseCreate a binary search tree using the following data entered as sequence set :70,60,80,50,65,75,85,45,55,90Delete node contain 60 and node contain 85 from the binary search tree above

Page 13: Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

13西南财经大学天府学院

Team workTeam work

Create a binary search tree using the following data entered as sequence set :70,60,80,50,65,75,85,45,55,90, output the ordered listInsert 34 and 79 into the tree created in above exercise and Delete node contain 60 and node contain 85 from the binary search tree above