Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC2007 Data Structures II

Similar presentations


Presentation on theme: "COSC2007 Data Structures II"— Presentation transcript:

1 COSC2007 Data Structures II
September 7, 2019 COSC2007 Data Structures II Chapter 11 Trees III Trees

2 Topics ADT Binary-Search Tree (BST) Operations Insertion Deletion
Retrieval traversal

3 ADT BST Problem of general BT BST
40 70 60 30 20 10 50 Problem of general BT Searching for a particular item is difficult Solution Use BST BST Each node satisfies the following properties: n’s value > all values in its left subtree TL n’s value < all values in its right subtree TR Both TL and TR are BST Insertion, deletion, and retrieval operations ? 60 70 20 10 40 50 30

4 ADT BST Operations

5 ADT BST Implementation
Implementation of the Structure The record, or item, in TreeNode class of the tree should extend the following class public abstract class KeyedItem { private Comparable searchKey; public KeyedItem (Comparable key) { serachKey =key; } // constructor public Comparable getKey () // returns search key { return searchKey; } // end getKey }

6 Using ADT BST Example BST of names
Each node will be a record representing a Person New Class: Person Class (Key = ID) public class Person extends KeyedItem { private FullName name; // Key item is the id private string phoneNumber; private Address address; public Person(FullName name, String id, String phone, Address addr) { super (id); //key is the ID this.name = name; phoneNumber = phone; address = addr; } public String toString () ( return getKey()+ “ #” + name; // other methods } // end class

7 ADT BST Implementation
Implementation of BST Operations A search algorithm is needed as the basis of some operations: insertion deletion retrieval 57 18 5 40 -8 14 28 56 184 63 229 387 root

8 Searching for a node with a value
57 18 5 40 -8 14 28 56 184 63 229 387 root Suppose I gave you a key, like 28. How would you do it?

9 Searching for a node with a value
57 18 5 40 -8 14 28 56 184 63 229 387 root Search key = 28 Compare against root If <, choose left If ==, return root If >, choose right Recurse on left or right subtree until either find element run off a leaf node (failure) compare and choose

10 Searching for a node with a value
Pseudocode of Search Strategy : search (bst, searchKey) // Searches the BST bst for the item whose key is searchKey if (bst is empty) The desired record is not found else if (searchKey = = search key of root’s item) The desired record is found else if (searchKey < searck key of root’s item) search (Left subtree of bst, serachKey) else search (Right subtree of bst, serachKey) 60 70 20 10 40 50 30

11 Searching for a node with a value
The shape of the tree doesn’t affect the search algorithm. However, the algorithm works more efficiently on some trees than the others Tom Jane Bob Alan Wendy Nancy Ellen Unbalanced BST Skewed BST Balanced BST Full & Complete BST

12 BST Insertion Insertion should preserve the tree's properties
First search for the correct position to insert the given item If the item doesn’t exist in the BST , item will be inserted as a new leaf Search will always terminate at an empty subtree

13 BST Insertion Suppose I want to insert the key 60 to this tree. 57 18
40 -8 14 28 56 184 63 229 387 root Suppose I want to insert the key 60 to this tree.

14 BST Insertion Search for the key, as we did earlier.
If we don’t find it, the place where we ‘fall off the tree’ is where a new node containing the key should be inserted. If we do find it, we have to have a convention about where to put a duplicate item (arbitrarily choose the left subtree). BST Insertion 57 18 5 40 -8 14 28 56 184 63 229 387 root compare and choose Element should be here if it’s in the tree! In this case, we need to create a new node as the left subtree of the node containing 63

15 BST Insertion 60 70 20 10 40 50 30 BST Insertion Pseudocode:
// insert newItem into the BST of which treeNode is the root insertItem (treeNode, NewItem) { if (treeNode is null) Create a new node and let treeNode point to it Create a new node with newItem as its data portion Set the reference in the new node to null } else if (newItem.getKey( ) < treeNode.getItem().getKey ( ) ) treeNode.setLeft(insertItem(treeNode.getLeft(), newItem)) else treeNode.setRight(insertItem(treeNode.getRight(),newItem))

16 BST Deletion Suppose I wanted to remove a key (and associated data item) from a tree. How would I do it? More involved than insertion Several cases to consider: N is a leaf N has only one left child N has only one right child N has two children 57 18 5 40 -8 14 28 56 184 63 229 387 root

17 BST Deletion To remove a key R from the binary search tree first requires that we find R. (we know how to do this) If R is (in) a leaf node, then R’s parent has its corresponding child set to null. If R is in a node that has one child, then R’s parent has its corresponding pointer set to R’s child. If R is in a node that has two children, the solution is more difficult. The deletion strategy should preserve the BST property

18 BST Deletion To remove a key R from the binary search tree first requires that we find R. (we know how to do this) If R is (in) a leaf node, then R’s parent has its corresponding child set to null. If R is in a node that has one child, then R’s parent has its corresponding pointer set to R’s child. If R is in a node that has two children, the solution is more difficult. The deletion strategy should preserve the BST property

19 Delete a leaf node Case 1: N is a Leaf
Set the pointer in the leaf’s parent to NULL

20 Delete a node with one child
Case 2: N has only one child Let N’s parent adopt N’s child

21 Delete a node with two children
Don’t remove the NODE containing the key 18. Instead, consider replacing its key using a key from the left or right subtree. Then remove THAT node from the tree. So what values could we use. What about 14 or 28? Max value from left subtree or minimum value from right subtree! Why are these the one’s to use? 57 18 5 40 -8 14 28 56 184 63 229 387 root

22 Delete a node with two children
57 14 5 40 -8 28 56 184 63 229 387 root 57 28 5 40 -8 14 56 184 63 229 387 root Notice that in both cases, the binary tree property is preserved.

23 BST Deletion deleteItem (rootNode, searchKey) {
//delete from BST with root rootNode the item whose key equals searchKey. Return the root node of the resulting tree if (treeNode == null) throw TreeExceoption // item not found else if ( searchKey equals the key in rootNode item) // item at root, delete the rootNode, a new root of the tree is returned newNode =deleteNode(rootNode, serachKey) return newNode; else if (searchKey is less than the key in rootNode item) newLeft =deleteItem(rootNode.getleft(), searchKey) rootNode.setLeft(newLeft); return rootNode; //return rootNode with new left subtree else newRight=deleteItem(rootNode.getRight(), searchKey) rootNode.setRight(newRight); return rootNode; //return rootNode with new right subtre }

24 BST Deletion // Deletes item in node to which treeNode references .
 deleteNode (treeNode) { // Deletes item in node to which treeNode references . //return the root node of the resulting tree if (treeNode is a leaf) nodePtr=null; //remove leaf from the tree else if (treeNode has only one child C) //c replaces treeNode as the child of treeNode’s parent { if (C is the left child of treeNode) return treeNode.getLeft(); else return treeNode.getRight(); } 60 70 20 10 40 50 30

25 BST Deletion else // treeNode has 2 children
// find the inorder successor of the search key in tree node: it is the leftmost node of the right subtree { replacementItem = findLeftMost (treeNode.getRight()); replacementRChild = deleteLeftMost(treeNode.getRight()) set treeNode’s item to replacement set treeNode’s right child to replacemenrRChild return treeNode }

26 BST Deletion findLeftMost (treeNode)
60 70 20 10 40 50 30 BST Deletion  findLeftMost (treeNode) // return the item that is the leftmost descendent of the tree rooted at treeNode if (treeNode.getLeft() == null) //this is the node you want, so return it items return treeNode.getItem() else return findLeftMost(treeNode.getLeft())

27 BST Deletion deleteLeftMost (treeNode)
60 70 20 10 40 50 30 BST Deletion  deleteLeftMost (treeNode) // delete the node that is the leftmost descendent // of the tree rooted at treeNode, return subtree of deleted node if (treeNode.getLeft() == null) //this is the node you want, it has no left child, but might has a right subtree, the return value of this method is a child reference of treeNode’s parent; thus following “movs up” treeNode’s right subtree return treeNode.getRight() else replacementLChild = deleteLeftMost(treeNode.getLeft()) treeNode.setLeft (replacementLChild) return treeNode

28 BST Retrieval Retrieve: Returns the item with the desired search key
By refining the search algorithm, we can implement the retrieval operation

29 BST Retrieval search (bst, searchKey)
//search theBST for the item whose search key is searchKey if (bst is empty) item not found else if (searchKey == search key of bst’s root item ) the desired record is found else if (searchKey < search key of bst’s root item ) search(left subtree of bst, searchKey) else search(right subtree of bst, searchKey) 60 70 20 10 40 50 30

30 BST Retrieval retrieveItem (treeNode, searchKey) 60 70 20 10 40 50 30
//return item whose search key equals searchKey //if no such item, return null if (treeNode == null) treeItem = null // tree is empty else if (searchKey == treeNode.getItem().getkey() ) //item is at root of some subtree treeItem = treeNode.getItem() else if (searchKey < treeNode.getItem().getkey() ) //search left subtree retrieveItem (treeNode.getleft(), searchKey) else retrieveItem (treeNode.getRight(), searchKey) return treeItem

31 BST Traversal The implementation should include the following methods:
Public Member Methods void preorderTraverse (bst); void inorderTraverse (bst); void postorderTraverse (bst);


Download ppt "COSC2007 Data Structures II"

Similar presentations


Ads by Google