Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Data Structures( 数据结构 ) Course 8: Search Trees

2 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem: 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 them Definition: the properties of a binary search tree 1.All items in the left subtree are less than the root 2.All items in the right subtree are greater than or equal to the root 3.Each subtree is itself a binary search tree. (a) (d)(c) (b) 17 619 17 196 143 17 6 3 19 22 (e) Figure 8-2 binary search trees (a) (d)(c) (b) 17 611 17 196 223 17 2219 17 196 1511 Figure 8-3 invalid binary search trees

3 3 西南财经大学天府学院 binary search tree traversals Algorithms are identical to the ones in chapter 7 Operations on binary search trees binary search tree search algorithms Find smallest node Find largest node Find requested node Follow the left branches until we get to a leaf Algorithm findsmallestBST( val root ) To find the smallest node in a BST Pre root is a pointer to a nonempty BST or subtree Return address of smallest node 1 if (root->left null) 1 return(root) 2 endif 3 return findsmallestBST (root->left) end findsmallestBST Follow the right branches to the last node in the tree Algorithm findlargestBST( val root ) To find the largest node in a BST Pre root is a pointer to a nonempty BST or subtree Return address of largest node 1 if (root->right null) 1 return(root) 2 endif 3 return findlargestBST (root->right) end findlargestBST Assume: To find T, Comparing T with root, T root go right Algorithm searchBST(val root, val argument ) Search a binary search tree for a given value Pre root is the root to a binary tree or subtree argument is the key value requested Return the node address if the value is found Null if the node is not in the tree 1 if (root is null) 1 return null 2 end if 3 if (argument key ) 1 return searchBST (root->left, argument) 4 else if (argument > root->key ) 1 return searchBST (root->right, argument) 5 else 1 return root 6 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

4 4 西南财经大学天府学院 Insert 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 20125235 After inserting 19 23 4418 20125235 19 After inserting 38 23 4418 20125235 1938 After inserting 23 23 4418 20125235 193823 Note: node with equal values are found in the right subtree

5 5 西南财经大学天府学院 Iterative insert (algorithm) 1 if (root is null) 1 root = new 2 else 1 pwalk = root 2 loop ( pwalk not null ) 1 parent = pwalk 2 if ( new->key key ) 1 pwalk = pwalk->left 3 else 1 pwalk = pwalk->right 4 end if 3 end loop Location for new node found 4 if (new->key key ) 1 parent->left = new 5 else 1 parent->right = new 6 end if 3 end if 4 return end insertBST Algorithm insertBST ( ref root, val new ) Insert node containing new data into BST using iteration Pre root is address of first node in a BST new is address of node containing data to be inserted Post new node inserted into the tree Algorithm 8-4 iterative binary search tree insert 23 18 1220 19 new parent Final position when pwalk null null pwalk

6 6 西南财经大学天府学院 Recursive insert node(algorithm) Algorithm addBST ( ref root, val new ) Insert node containing new data into BST using recursion Pre root is address of current node in a BST new is address of node containing data to be inserted Post new node inserted into the tree 1 if (root is null) 1 root = new 2 else locate null subtree for insertion 1 if ( new->key key ) 1 addBST (root->left, new ) 2 else 1 addBST (root->right, new ) 3 end if 4 return end insertBST algorithm 8-5 add node to BST recursively

7 7 西南财经大学天府学院 Delete node 1.locate it 2.delete: there are 4 possible cases The node has no children: set the delete node’s parent to null, recycle its memory, and return Has 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 data Find 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.

8 8 西南财经大学天府学院 dltptr (a) Delete node (44) that has no children before 23 1844 23 18 after

9 9 西南财经大学天府学院 1 if (root is null) 1 return false 2 endif 3 if(dltkey 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 if 6 endif end deleteBST Algorithm deleteBST ( ref root, val dltKey ) Delete a node from a BST Pre root is pointer to tree containing data to be deleted dltkey is key of node to be deleted Post node deleted and memory recycled, if dltkey not found, root unchanged Return true if node deleted, false if not found Base case 1 Node not find Base case 2 After node deleted

10 10 西南财经大学天府学院 Summary 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.

11 11 西南财经大学天府学院 Summary 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.

12 12 西南财经大学天府学院 Exercises Create a binary search tree using the following data entered as sequence set : 7,10,14,23,33,56,66,70,80 Insert 44 and 50 into the tree created in above exercise Create a binary search tree using the following data entered as sequence set :70,60,80,50,65,75,85,45,55,90 Delete node contain 60 and node contain 85 from the binary search tree above

13 13 西南财经大学天府学院 Team 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 list Insert 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


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

Similar presentations


Ads by Google