{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Visitor &acirc;€“ Heap &acirc;€“ HeapSort

# Visitor &acirc;€“ Heap &acirc;€“ HeapSort -...

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

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 I E

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

View Full Document
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
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();

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

View Full Document
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 obj the object to remove */ public void remove(Comparable obj) { // Find node to be removed Node toBeRemoved = root ; Node parent = null ; boolean found = false ; while (!found && toBeRemoved != null ) { int d = toBeRemoved. data .compareTo(obj) ; if (d == 0) found = true ; else { parent = toBeRemoved; if (d > 0) toBeRemoved = toBeRemoved. left ; else toBeRemoved = toBeRemoved. right ; } } if (!found) return ; // toBeRemoved contains obj // If one of the children is empty, use the other if (toBeRemoved. left == null || toBeRemoved. right == null ) {
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

Visitor &acirc;€“ Heap &acirc;€“ HeapSort -...

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

View Full Document
Ask a homework question - tutors are online