{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Tutorial 11

Tutorial 11 - after deleting 13 5 25 15 20 from the larger...

This preview shows pages 1–5. Sign up to view the full content.

CS 134 Tutorial 11: Binary Search Trees (BSTs) and Sorting CS 134 Tutors Winter 2008 1 Binary Search Trees 1.1 BST Insertions and Deletions Insertions: When inserting into a binary search tree, we always insert a leaf node, such that the tree remains a binary search tree. Deletions: There are four cases when deleting a node from a binary search tree: The node does not exist: Do nothing The node is a leaf: Remove the leaf The node has one child: Replace the node with its nonempty subtree (shift the tree up). The node has two children: Replace the node with its in-order predecessor or its in-order successor , and then delete the in-order predecessor/successor from the tree. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1.2 Example For the following binary search tree, add nodes to the picture to create the resulting tree from inserting integers 28, 7, 16, 35 in that order. Then redraw the tree that results from deleting integers 13, 5, 25, 15, 20 from the new tree in that order. Tree after inserting 28, 7, 16, 35 is (insert the nodes below): 17 15 30 20 12 5 10 25 Tree

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: after deleting 13, 5, 25, 15, 20 from the larger tree: 2 1.3 BST Recursive Method Write a method to eﬃciently count the number of elements in the range [ lo , hi ] in a binary search tree containing Integer objects with no duplicates. What is the asymptotic runtime of your method in the worst case? In the best case? public class MyClass { public static int count(BinaryTreeInterface tree, int lo, int hi) // pre: tree is a BST containing unique Integer objects // post: returns the number of Integer objects in the tree // with values between lo and hi inclusive { } } Worst Case: Best Case: 3 1.4 BST Induction Proof Prove by induction that the inorder traversal of a binary search tree lists the elements in ascend-ing order. Proof. Let n be the number of nodes in a binary search tree T . Let Rt be the root element of T . Let In ( T ) be the inorder traversal of T . 4 2 Sorting Exercises Please complete the Selection and Insertion Sorting exercises. 5...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

Tutorial 11 - after deleting 13 5 25 15 20 from the larger...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online