lecture21-july31

# lecture21-july31 - Lecture 21 Announcements A Binary Search...

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

Lecture 21 Announcements Topics – more Binary search trees Variant trees Remaining course readings are: Ch. 9, 10.8, Table 10.4 , Ch. 12 •Assignment 4 – Q&A •Exam 2 – Aug. 14 A special kind of binary tree in which: Each node contains a distinct key value, The key values in the tree can be compared using “greater than” and “less than”, and The key value of each node in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree. A Binary Search Tree (BST) is Model Node Class for a BTree // this class models a generic node of a binary tree // with left, right and parent references class Btnode <T> { T nodeValue; // the data value being stored Btnode <T> left; // reference to left subtree Btnode <T> right; // reference to right subtree Btnode <T> parent; // reference to parent node Btnode (T val, Btnode <T> leftTemp, Btnode <T> rightTemp, Btnode <T> parentTemp) { nodeValue = val; left = leftTemp; right = rightTemp; parent = parentTemp; } } Btnode would be within class Btree<T> { . . . } Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’ ‘D’ ‘Q’ ‘L’ ‘B’ ‘S’ Three BTree Traversals Recursive Btree Example // algorithm for counting the # of nodes // in a given binary tree if tree is NULL return 0 else return (CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1)

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

View Full Document
{ // in main int number = CountNodes(root); } int CountNodes(Btnode tree) // Recursive function that counts the # of nodes in a Btree { if (tree = = NULL) {return 0; } else { return CountNodes(tree.left) + CountNodes(tree.right) + 1; } } Count the nodes program Update Operations • Insertion into a BST is relatively easy but deletion is hard • The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values. Insert Operations Insert the value 32 into the Btree below The nodes in this example have parent references too 25 40 12 35 20 root, t Insert Operations: 1 st of 3 steps The method begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35. 25 40 12 35 20 parent t (a) Step 1: Compare 32 and 25. Traverse the right subtree . Insert Operations: 2 nd of 3 steps Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/15/2009 for the course EE 322C taught by Professor Nettles during the Summer '08 term at University of Texas at Austin.

### Page1 / 8

lecture21-july31 - Lecture 21 Announcements A Binary Search...

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

View Full Document
Ask a homework question - tutors are online