Visitor – Heap – HeapSort

Visitor – Heap – HeapSort -...

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

View Full Document Right Arrow Icon
1 1.0 Introduction This lab covers binary search trees, the visitor design pattern, and heaps and the heapsort algorithm. 2.0 Lab Exercises 2.1 Binary Search tree Adding: Removing: A: A is a leaf node, with no children. So it is simply removed from the binary search tree. B: D B F A I C E J G H D B F I C E J G H D F
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 B has only one right child. The right child (in this case, C) replaces the parent. F: F has both right and left children. Therefore, F is replaces with the leftmost right child. In this case, it is G, which has a right child. G is moved to F’s place, and G is replaced by its right child, H. D: D is simply replaced by the leftmost right node, E. There are no complications about E because it has no children. 2.2 Traversal C D I C E J G H I C E J G H
Background image of page 2
3 There are three methods or traversal: In order traversal: The left sub-tree is visited first, followed by the root node, and then the right sub- tree. Result of traversal of this tree: C E G H I J Preorder traversal: The root is visited first, followed by the left sub-tree and then the right sub-tree. Result of traversal of this tree: E C G I H J Postorder traversal: The left sub-tree is visited first, followed by the right sub-tree, and then finally the root. Result of traversal of this tree: C H J I G E 2.3 Implementation of binary search tree This is the implementation of the binary search tree given in the textbook. The worst case for a binary search tree is when the elements are sorted before they are inserted. In this case, all elements branch off to the right (or left if sorted in descending order). This means that the binary tree degenerates to a linked list, and the big-Oh for accessing/adding an element becomes O(n). Usually, this is O(log (n)). Coding: BinarySearchTree: /** This class implements a binary search tree whose nodes hold objects that implement the Comparable interface. */ public class BinarySearchTree { /** Constructs an empty tree. */ public BinarySearchTree() { root = null ; } /** Inserts a new node into the tree. @param obj the object to insert */ public void add(Comparable obj) { Node newNode = new Node();
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 newNode. data = obj; newNode. left = null ; newNode. right = null ; if ( root == null ) root = newNode; else root .addNode(newNode); } /** Tries to find an object in the tree. @param obj the object to find @return true if the object is contained in the tree */ public boolean find(Comparable obj) { Node current = root ; while (current != null ) { int d = current. data .compareTo(obj) ; if (d == 0) return true ; else if (d > 0) current = current. left ; else current = current. right ; } return false ; } /** Tries to remove an object from the tree. Does nothing if the object is not contained in the tree. @param
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/30/2011 for the course MECHATRONI EN11ME2050 taught by Professor Perera during the Spring '11 term at Asian Institute of Technology.

Page1 / 29

Visitor – Heap – HeapSort -...

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

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