csc 205 – java programming ii lecture 35 april 17, 2002

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

Upload: kelvin-morfin

Post on 15-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

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

CSC 205 – Java Programming II

Lecture 35

April 17, 2002

Page 2: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Binary Trees – Recap

• Full, complete, 2-tree

• Level and height

Jane

Bob Tom

Alan Ellen Nancy

0 Jane

1 Bob

2 Tom

3 Alan

4 Ellen

5 Nancy

Page 3: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Array-Based Representation

Jane

Bob Tom

Alan Ellen Nancy

0 Jane 1 2

1 Bob 3 4

2 Tom 5 -1

3 Alan -1 -1

4 Ellen -1 -1

5 Nancy -1 -1

60 6

root free

FrankInsert Frank

Page 4: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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

Bob Tom

Alan Ellen Nancy

Jane

Bob Tom

Ellen Nancy

Frank

Page 5: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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

Page 6: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Examples

60

20 70

10 40

30 50

Preorder60, 20, 10, 40, 30, 50, 70

Postorder10, 30, 50, 40, 20, 70, 60

Inorder10, 20, 30, 40, 50, 60, 70

Page 7: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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) }

Page 8: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Preorder Traversal Algorithm

• Postorder generates prefix representations

• 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) preorder(left subtree of the binTree’s root) }

Page 9: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Reference-Based Representation

• Each node contains – An item to be stored– A left and a right references

itemleftChild rightChild

root

TreeNode

Page 10: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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; } ……)

Page 11: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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 { …… }

……)

Page 12: CSC 205 – Java Programming II Lecture 35 April 17, 2002

BST

• Properties satisfied by each node n in a BST– n’s value is greater than all values in its left

subtree TL

– n’s value is smaller than all values in its right subtree TR

– Both TL and TR are BSTs

• Assume not duplicate values are allowed • Items can be stored in a BST should be

comparable to each other

Page 13: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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

Page 14: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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.

Page 15: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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 terminates @ parentNode’s left subtree) Set leftChild of parentNode to reference newItem

else Set rightChild of parentNode to reference newItem

Page 16: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Deletion

• Search will be used

• A separate deleteNode method will also be calleddeletItem(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

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

Deletion – 3 Scenarios

• Delete is much more complicated

• There are three different cases to considerAssume 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

Page 18: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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

Page 19: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Example – deleting the root

Jane

Bob Tom

Alan Ellen Nancy Wendy

N

M

Nancy

Alan, Bob, Ellen, Jane, Nancy, Tom, Wendy

Page 20: CSC 205 – Java Programming II Lecture 35 April 17, 2002

Result – still a BST

Nancy

Bob Tom

Alan Ellen Wendy

Page 21: CSC 205 – Java Programming II Lecture 35 April 17, 2002

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