{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


31-Section-Handout - CS106X Autumn 2010 Handout 31 November...

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

View Full Document Right Arrow Icon
CS106X Handout 31 Autumn 2010 November 8 th , 2010 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); 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): 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 Document Right Arrow Icon
2 1 2 3 4 5 6 7 1 2 3 4 5 6 7 RotateLeft(&node3); 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 the near future. Rotations can be used to bubble frequently access nodes toward the root of the tree, so subsequent searches can succeed in less time.
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.

{[ snackBarMessage ]}

Page1 / 6

31-Section-Handout - CS106X Autumn 2010 Handout 31 November...

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

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