CC2203-Les12TreeII-for-viewing

CC2203-Les12TreeII-for-viewing - 12-1/46Lesson 12: Tree...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 12-1/46Lesson 12: Tree II12-2/46Definition Of Binary Search Tree• A binary tree.• Each node has a (key, value) pair.• For every node x, all keys in the left subtree of x are smaller than the key in x.• For every node x, all keys in the right subtree of x are greater than the key in x.12-3/46Example Binary Search Tree201062815403025Only keys are shown.Insert 20, 10, 6, 2, 8, 15, 40, 30, 2512-4/46The Operation get()201062815403025Complexity is O(height)12-5/46The Operation get()public Object get(Object theKey) {// pointer p starts at root and moves looking for theKeyBinaryTreeNode p = root;Comparable searchKey = (Comparable) theKey;while (p != null)// examine p.element.keyif (searchKey.compareTo(((Data) p.element).key) < 0)p = p.leftChild;elseif (searchKey.compareTo(((Data) p.element).key) > 0)p = p.rightChild;else // found matching elementreturn ((Data) p.element).element;// no matching elementreturn null;}demo: javac dataStructures\BinarySearchTree.javajava dataStructures.BinarySearchTree40201062815302512-6/46The Operation put()201062815403025Put a pair whose key is 35.3512-7/46The Operation put()Put a pair whose key is 7.20106281540302535712-8/46The Operation put()201062815403025Put a pair whose key is 18.3571812-9/46The Operation put()201062815403025Complexity of put() is O(height).3571812-10/46The Operation put()public Object put(Object theKey, Object theElement) {BinaryTreeNode p = root, // search pointerpp = null; // parent of pComparable elementKey = (Comparable) theKey;// find place to insert theElementwhile (p != null) {// examine p.element.keypp = p;// move p to a childif (elementKey.compareTo(((Data) p.element).key) < 0)p = p.leftChild;else if (elementKey.compareTo(((Data) p.element).key) > 0)p = p.rightChild;else {// overwrite element with same keyObject elementToReturn = ((Data) p.element).element;((Data) p.element).element = theElement;return elementToReturn;}}201062815403025357Insert 18ppppppppppppdemo: javac dataStructures\BinarySearchTree.javajava dataStructures.BinarySearchTree12-11/46The Operation put()// get a node for theElement and attach to ppBinaryTreeNode r = new BinaryTreeNode(new Data(elementKey, theElement));if (root != null)// the tree is not emptyif (elementKey.compareTo(((Data) pp.element).key) < 0)pp.leftChild = r;elsepp.rightChild = r;else // insertion into empty treeroot = r;return null;}20106281540302535718Insert 18pppr12-12/46The Operation remove()Three cases:sElement is in a leaf....
View Full Document

This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

Page1 / 46

CC2203-Les12TreeII-for-viewing - 12-1/46Lesson 12: Tree...

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

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