Lecture18BinarySearchTrees

Lecture18BinarySearchTrees - child. If you encounter a null...

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

View Full Document Right Arrow Icon
Binary Search Trees
Background image of page 1

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

View Full DocumentRight Arrow Icon
Binary search Search cuts interval in half each step 1,000 500 250 125 63 32 16 8 4 2 1
Background image of page 2
How many steps for 2000 items in list?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Hmm, requires an array Arrays are slow for insertion operations.
Background image of page 4
Answer: a Treelike structure Allow fast insertion and removal of elements Specially designed for fast searching
Background image of page 5

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

View Full DocumentRight Arrow Icon
Binary Search Tree All nodes in a binary search tree fulfill the property that the descendants to the left have smaller data values than the node data value and the descendants to the right have larger data values.
Background image of page 6
BinarySearchTree.java public class BinarySearchTree { public BinarySearchTree() {…} private Node root; private class Node { public Comparable data; public Node left; public Node right; public Node (….) { … } }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Insertion Algorithm To insert data into the tree, use the following algorithm: If you encounter a non-null node reference, look at its data value. If the data value of that node is larger than the one you want to insert, continue the process with the left child. If the existing data value is smaller, continue the process with the right
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: child. If you encounter a null node reference, replace it with the new node. Try this sequence: Juliet, Tom, Dick, Harry, Romeo add method public void add ( Comparable obj) { Node newNode = new Node(obj); newNode.data = obj; newNode.left = null; newNode.right = null; if (root == null) root = newNode; else root.addNode(newNode); } Node class public void addNode (Node newNode) { int comp = newNode.data.compareTo(data); if (comp < 0) { if (left == null) left = newNode; else left.addNode(newNode); } else if (comp > 0) { if (right == null) right = newNode; else right.addNode(newNode); } } Consider add for two sets of data Random Sorted Which is faster? find method 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; }...
View Full Document

Page1 / 12

Lecture18BinarySearchTrees - child. If you encounter a null...

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

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