Binary Search Tree

Binary Search Tree - Binary Search Tree binary search tree...

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

View Full Document Right Arrow Icon
Binary Search Tree binary search tree (BST) BST is a binary tree (linked structure with left and right pointers) that is either empty or the keys (basis which comparisons are done to check if they’re equal) in left subtree of root < key at root < keys in right subtree of root. Subtrees are also BSTs BST node public class BSTNode<T extends Comparable<T>> { public T data; public BSTNode<T> left, right; public BSTNode(T data, BSTNode<T> left, BSTNode<T> right) { this.data = data; this.left = left; this.right = right; } }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Dictionary - data structure that allows you to search, insert and delete, and every key is unique public class BST<T extends Comparable<T>> { private BSTNode<T> root; private int size; public BST() { root = null; size = 0; } public T search(T target) best O(1), worst O(n) { BSTNode<T> ptr = root; while (ptr != null) { int c = target.data.compareTo(ptr.data)) if (c == 0) { return ptr.data; } else if (c < 0) { ptr = ptr.left; } else if (c > 0) { ptr = ptr.right; } } return null;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/29/2012 for the course CS 112 taught by Professor Sesh during the Spring '07 term at Rutgers.

Page1 / 4

Binary Search Tree - Binary Search Tree binary search tree...

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

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