sahnisol2-1 - Data Structures & Algorithms Exam 2, Sample 1...

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

View Full Document Right Arrow Icon
Data Structures & Algorithms Exam 2, Sample 1 75 Minutes Solutions 1. (a) To perform the operation, we can do a postorder traversal of the binary tree. During the visit step, we exachange the left and right children of the node being visited. To implement this strategy we need to define a public and a private swapTree method. The public method can be defined as below: public void swapChildren() {swapChildren(root);} The private method is a class (i.e., static) method that does the actual swapping of children by performing a postorder traversal. The code is given below. private static void swapChildren(BinaryTreeNode t) {// Swap the children of every node in the subtree t. // Do a postorder traversal; swap left and right child // pointers in the visit step. if (t != null) { // swap in the left subtree swapChildren(t.leftChild); // swap in the right subtree swapChildren(t.rightChild); // swap children of t BinaryTreeNode temp = t.rightChild; t.rightChild = t.leftChild; t.leftChild = temp;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2009 for the course EEL 3472 taught by Professor Rakov during the Spring '08 term at University of Florida.

Page1 / 3

sahnisol2-1 - Data Structures & Algorithms Exam 2, Sample 1...

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

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