Binary Search Tree

# Binary Search Tree - Binary Search Tree binary search...

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

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; } }

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

View Full Document
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;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

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

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

View Full Document
Ask a homework question - tutors are online