33-Section-Handout

33-Section-Handout - CS106X Winter 2008 Handout 33 Section...

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

View Full Document Right Arrow Icon
CS106X Handout 33 Winter 2008 February 25, 2008 Section Handout Problem 1: Binary Tree Recursion Recall the node definition that came up when we introduced the binary search tree as a data structure. Each node was designed to store some data value (an int , for example) and two embedded pointers: struct node { int value; node *left; node *right; }; The same node data structure can be used to build doubly linked lists as well if we simply interpret the left pointer of a node to point to the preceding node of the list, and the right pointer to point to the next node of the list—specifically, left and prev are synonymous, as are right and next . In this problem, you will write a very short, very recursive procedure that destructively converts a binary search tree into a sorted, doubly linked list. a.) Write a function IdentifyEndpoints that takes a pointer to an arbitrary node within a doubly linked list and returns, by reference, returns the address of the first and last nodes of the list. If a NULL pointer is passed in, you should simply place NULL in both parameters. void IdentifyEndpoints(node *arbitraryNode, node *& front, node *& back); b.) Now write the FlattenTree routine, which destructively reduces an ordered binary search tree to an ordered, doubly linked list reusing the same nodes of the original tree. If myTree were initialized to be the following: then the statement myList = FlattenTree(myTree); myTree 2 4 6 1 3 5 7
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 1 2 3 4 5 6 7 1 2 3 4 5 6 7 RotateLeft(&node3); should result in the following restructuring and assignment (note that myTree still addresses the node which stores the 4—no new memory gets allocated, only existing nodes get used): node *FlattenTree(node *root); Problem 2: Tree Rotations Given the pattern of references in a typical binary search tree, you can often improve the average search time using a simple technique known as rotation . Often, references to any particular element in a binary search tree are clustered in time, in the sense that an access to a particular element is likely to be followed by many other queries for the same element in
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

33-Section-Handout - CS106X Winter 2008 Handout 33 Section...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online