csc 205 – java programming ii lecture 35 april 17, 2002
TRANSCRIPT
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
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
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
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
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
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) }
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) }
Reference-Based Representation
• Each node contains – An item to be stored– A left and a right references
itemleftChild rightChild
root
TreeNode
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; } ……)
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 { …… }
……)
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
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
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.
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
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
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
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
Example – deleting the root
Jane
Bob Tom
Alan Ellen Nancy Wendy
N
M
Nancy
Alan, Bob, Ellen, Jane, Nancy, Tom, Wendy
Result – still a BST
Nancy
Bob Tom
Alan Ellen Wendy
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