Docsity

Binary Search Trees: Implementation and Usage, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS261–DataStructures
Binary Search Trees
Binary Search Tree
• Binary search trees are binary tree’s where every node’s
object value is:
–Greater than or equal to all its descendents in the left subtree
–Less than or equal to all its descendents in the right subtree
• An in-order traversal will return the elements in sorted
order
• If the tree is reasonably full, searching for an element is
O(log n)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Binary Search Trees: Implementation and Usage and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Binary Search Trees

Binary Search Tree

• Binary search trees are binary tree’s where every node’s

object value is:

  • Greater than or equal to all its descendents in the left subtree
  • Less than or equal to all its descendents in the right subtree

• An in-order traversal will return the elements in sorted

order

• If the tree is reasonably full, searching for an element is

O(log n )

Binary Search Tree: Example

Alex

Abner Angela

Adela Alice Audrey

Adam Agnes Allen Arthur

Abigail

Binary Search Tree (BST): Implementation

public class BinarySrchTree implements Bag, Sorted, FindMin { protected final Comparator test; protected BSTNode root = new BSTSentinel(); protected int count = 0; public BinarySrchTree(Comparator t) { test = t; } public BinarySrchTree() { test = DefaultComparator; } // Implementation of the Collection interface. public boolean isEmpty() { return root.isEmpty(); } public int size() { return count; } public Enumeration elements() { return new InorderTreeTraversal(root); } // Implementation of the Bag interface. ... // Implementation of the FindMin interface. ... // Private, inner classes. private class BSTNode extends BinaryNode() { ... } private class BSTSentinel extends BSTNode() { ... } }

Binary Search Tree: Example with Sentinels

Alex

Abner Angela

Abigail Adela Alice Audrey

Adam Agnes Allen Arthur

Sentinel

There is actually only one sentinel node

that is referenced multiple times

BST Implementation (cont.)

public class BinarySrchTree implements Bag, Sorted, FindMin { ... // Implementation of the Bag interface. ... public boolean containsElement(Object val) { return root.contains(val); } public boolean findElement(Object val) { return root.find(val); } public synchronized void addElement(Object val) { root = root.add(val); count++; } public synchronized void removeElement(Object val) { root = root.remove(val); count--; } // Implementation of the FindMin interface. ... public Object getFirst() { return root.getFirst(); } public void removeFirst() { root = root.removeFirst(); count--; } }

The individual nodes do all the work!

BST Implementation: isEmpty

public class BinarySrchTree implements Bag, Sorted, FindMin { ... public boolean isEmpty() { return root.isEmpty(); } ... private class BSTNode extends BinaryNode { ... public boolean isEmpty() { return false; } ... } private class BSTSentinel extends BSTNode { ... public boolean isEmpty() { return true; } ... } }

BSTSentinel returns true to the isEmpty inquiry while a

normal BSTNode responds false

BST Implementation: FindMin

// Binary search tree. public Object getFirst() { return root.getFirst(); } public void removeFirst() { root = root.removeFirst(); count--; } ...

// BSTNode. public Object getFirst() { // “First” object is always if (leftBST().isEmpty()) return obj; // at the leftmost node. return leftBST().getFirst(); } public BSTNode removeFirst() { if (leftBST().isEmpty()) return rightBST(); left = leftBST().removeFirst(); return this; } ...

// BSTSentinel: these methods only called when the tree is empty. public Object getFirst() { throw new NoSuchElementException(); } public BSTNode removeFirst() { throw new NoSuchElementException(); }

BST Implementation: removeFirst Example

Alex

Adela Angela

Adam Agnes Alice Audrey

Allen Arthur

Sentinel

After second call to removeFirst

New “First”

element

BST Implementation: containsElement

// Binary search tree. public boolean containsElement(Object val) { return root.contains(val); } ...

// BSTNode. public boolean contains(Object val) { int result = test.compare(val, obj); if (result == 0) return true; if (result < 0) return leftBST().contains(val); return rghtBST().contains(val); } ...

// BSTSentinel. public boolean contains(Object val) { return false; }

Return false if it reaches a sentinel node without finding the

value

BST Implementation: findElement

// Binary search tree. public Object findElement(Object val) { return root.find(val); } ...

// BSTNode. public Object find(Object val) { int result = test.compare(val, obj); if (result == 0) return obj; if (result < 0) return leftBST().contains(val); return rghtBST().contains(val); } ...

// BSTSentinel. public Object find(Object val) { return null; }

BST Implementation: addElement

// Binary search tree. public synchronized void addElement(Object val) { root = root.add(val); count++; } ...

// BSTNode. public BSTNode add(Object val) { // Find position (sentinel) to insert. if (test.compare(val, obj) < 0) left = leftBST().add(val); else rght = rghtBST().add(val); return this; } ...

// BSTSentinel. public BSTNode add(Object val) { // Reached sentinel node. BSTNode node = new BSTNode(val); // Create new BSTNode. node.left = node.rght = this; // Both children are sentinels. return node; }

BST Implementation: removeElement

// Binary search tree. public synchronized void removeElement(Object val) { root = root.remove(val); count--; }

// BSTNode. public BSTNode remove(Object val) { int result = test.compare(val, obj); if (result == 0) { // Found element. if (rghtBST().isEmpty()) return leftBST(); obj = rghtBST().getFirst(); rght = rghtBST().removeFirst(); } else if (result < 0) left = leftBST().remove(val); else rght = rghtBST().remove(val); return this; }

// BSTSentinel. public BSTNode remove(Object val) // Should not have { throw new NoSuchElementException(); } // reached sentinel.

BST Implementation: removeElement

Alex

Abner Angela

Abigail Adela Alice Audrey

Adam Agnes Allen Arthur

Sentinel

Before call to removeElement

Element to

remove

Replace with: rghtBST().getFirst()

BST Implementation: removeElement

Alex

Abner Arthur

Abigail Adela Alice

Adam Agnes Allen

Sentinel

Audrey

After call to removeElement