# Binary tree - 1 Objectives The objectives of this lab are...

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

Page | 1 1 Objectives The objectives of this lab are to: Understand and implement binary search tree. ˆ Learn to use and implement Visitor design pattern. ˆ Understand heap and heapsort. This lab report contains details, observations and solutions to the exercises given in the lab. Lab Contents: 1 Objectives Page 1 2 Lab Exercises Page 2 2.1 Binary Search Trees Page 2.1.1 Exercise P16.8 2.1.2 Exercise P16.17 2.2 Heap and Heapsort Page 4 Homework Exercises Page 4.1 Implementation of InOrderVisitor Exercise P16.17 Page 4.2 Implentation PostOrderVisitor Exercise P16.17 4.3 Exercise R16.20 5 References Page 2 Lab Exercises

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

View Full Document
Page | 2 2.1 Binary Search Trees The simulation of a binary tree for a sequence of operations in given below. a) b) c) d) D D B D B A D B A C
Page | 3 e) f) g) h) D B A C F D B A C F E D B A C F E I I D B A C F E G

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

View Full Document
Page | 4 i) j) k) G I D B A C F E H G I D B A C F E H J G I H J D B C F E
Page | 5 l) m) n) G I H J D C F E H I J D C G E H I J E C G

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

View Full Document
Page | 6 Traversal schemes for the final binary search tree 1) In order traversal scheme C E G H I J 2) Pre order traversal scheme E C G I H J 3) Post order traversal C H J I G E Post order traversal scheme is the worst traversal scheme for a binary search tree. Here the In-Lab exercise 1 was implemented. 2.1.1 In-Lab Exercise 1: Exercise P16.8 The solution to this exercise contains two classes. The codes for the two classes are given below. ‘IntTree’ class code: public class IntTree { public IntTree() { root = null ; } public void insert( int aNum) { Node newNode = new Node(); newNode. data = aNum; newNode. left = null ; newNode. right = null ; if ( root == null ) root = newNode; else root .addNode(newNode); } public boolean find( int aNum) { Node current = root ; while (current != null ) { if (aNum == current. data ) return true ; else if (aNum < current. data ) current = current. left ; else current = current. right ; } return false ; } public void remove( int aNum) { Node toBeRemoved = root ; Node parent = null ; boolean found = false ; while (!found && toBeRemoved != null ) { if (aNum == toBeRemoved. data ) found = true ; else {
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 16

Binary tree - 1 Objectives The objectives of this lab are...

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

View Full Document
Ask a homework question - tutors are online