Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 – Java Programming II Lecture 35 April 17, 2002.

Similar presentations


Presentation on theme: "CSC 205 – Java Programming II Lecture 35 April 17, 2002."— Presentation transcript:

1 CSC 205 – Java Programming II Lecture 35 April 17, 2002

2 Binary Trees – Recap Full, complete, 2-tree Level and height Jane BobTom AlanEllenNancy 0Jane 1Bob 2Tom 3Alan 4Ellen 5Nancy

3 Array-Based Representation Jane BobTom AlanEllenNancy 0Jane 12 1Bob 34 2Tom5 3Alan 4Ellen 5Nancy 6 06 rootfree Frank Insert Frank

4 Balanced Tree A binary tree is (height) balanced if –the height of any of its node’s left subtree differes from the height of the node’s right subtree no more than 1. Jane BobTom AlanEllenNancy Jane BobTom EllenNancy Frank

5 Traversals of a Binary Tree Traverse a tree means –Visit every node of tree –Visit each node only once –Visit nodes in a given order Three different orders –Preorder –Inorder –Postorder

6 Examples Preorder 60, 20, 10, 40, 30, 50, 70 Postorder 10, 30, 50, 40, 20, 70, 60 Inorder 10, 20, 30, 40, 50, 60, 70

7 Inorder Traversal Algorithm The result of inorder traversal of a BST –The nodes will be visited in order according to their values inorder(binTree) //Traverses the binary tree binTree in inorder. //Visits a node in between the visits to its left //and right subtrees. if (binTree is not empty) { inorder(left subtree of the binTree’s root) Process (e.g. display) the data in the root inorder(left subtree of the binTree’s root) }

8 Preorder Traversal Algorithm Postorder generates prefix representations preorder(binTree) //Traverses the binary tree binTree in preorder. //Visits a node before visiting either of its // left and right subtrees. if (binTree is not empty) { Process (e.g. display) the data in the root preorder(left subtree of the binTree’s root) }

9 Reference-Based Representation Each node contains –An item to be stored –A left and a right references item leftChildrightChild root TreeNode

10 The TreeNode Class Can be used for any binary trees –not necessarily BSTs public class TreeNode { private Object item; private TreeNode leftChild, rightChild; public TreeNode(Object newItem, TreeNode left, TreeNode right) { item = newItem; leftChild = left; rightChild = right; } …… )

11 The BinaryTreeBasis Class An abstract base class to be extended –By trees with special properties, such as BST public abstract class BinaryTreeBasis { protected TreeNode root; public BinaryTreeBasis() {root = null;} public BinaryTreeBasis(Object rootItem) { root = new TreeNode(rootItem, null, null); } public Object getRootItem() throws TreeException { …… } …… )

12 BST Properties satisfied by each node n in a BST –n’s value is greater than all values in its left subtree T L –n’s value is smaller than all values in its right subtree T R –Both T L and T R are BSTs Assume not duplicate values are allowed Items can be stored in a BST should be comparable to each other

13 BST Operations Values stored in a BST are sorted –Can be displayed in order by inorder traversal Operations of BSTs –Insert a new item into a BST –Delete the item with a given search key from a BST –Retrieve the item with a given search key from a BST –Traverse the items in a BST in the 3 orders

14 The Search Strategy search(bst, searchKey) //Searches the binary search tree bst for the item //whose search key is searchKey. if (bst is empty) The desired record is not found else if (searchKey == search key of the root’s item) The desired record is found else if (searchKey < search key of the root’s item) search(Left subtree of bst, searchKey) else search(Right subtree of bst, searchKey) The basis of insertion, deletion, and retrieval.

15 Insertion Use search to determine where into the tree a new item to insert. –search will always terminate at an empty tree. insertItem(treeNode, newItem) //Inserts newItem into the binary search tree of //which treeNode is the root. Let parentNode be the parent of the empty subtree at which search terminates when it seeks newItem’s search key if (search parentNode’s left subtree) Set leftChild of parentNode to reference newItem else Set rightChild of parentNode to reference newItem

16 Deletion Search will be used A separate deleteNode method will also be called deletItem(rootNode, searchKey) //Deletes from the bst with root rootNode the item //whose search key equals searchKey. If no such item //exists, the operation fails & throws TreeException. Locate (by using the search algorithm) the item whose search key equals searchKey ; it occurs in node i if (item is found in node i) deleteNode(i) //defined next else throws a tree exception

17 Deletion – 3 Scenarios Delete is much more complicated There are three different cases to consider Assume that item i is in node N –N is a leaf – simply delete it –N has only one child – replace N w/ its child –N has two children – needs more discussion

18 With Two Children After deleting the node N from a BST, the resultant binary tree should still be a BST The following steps are needed in this case –Locate another node M that is easier to remove from the BST than the node N. That is, M has at most one child. –Copy the item that is in M to N –Remove the node M from the BST

19 Example – deleting the root Jane BobTom AlanEllenNancyWendy N M Nancy Alan, Bob, Ellen, Jane, Nancy, Tom, Wendy

20 Result – still a BST Nancy BobTom AlanEllenWendy

21 The deleteNode Algorithm Recall that the inorder traversal displays the nodes in order according to their values –Either the inorder successor or predecessor of N can be used to replace N –In our example, either Nancy or Ellen would work They are the left-most descendant in the right subtree (Nancy, inorder successor), and the right- most descendant in the left subtree (Ellen, inorder predecessor ), respectively


Download ppt "CSC 205 – Java Programming II Lecture 35 April 17, 2002."

Similar presentations


Ads by Google