lec15 - CSE 12 Binary Search Trees A Binary Search Tree ADT...

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

View Full Document Right Arrow Icon
15-1/43 A Binary Search Tree ADT A Linked Implementation of Binary Search Tree Binary Search Tree Operation Time Costs The Importance of Being Balanced AVL trees CSE 12 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
15-2/43 Binary Search Trees “Structural property”: A BST is a binary tree “Ordering property”: For any node X in the tree: all the data elements in X’s left subtree are less than the data element in X, and all the data elements in X's right subtree are greater than the data element in X A binary search tree is a binary tree that has the following invariants A total order must apply to data elements in a BST. .. they must be comparable to each other!
Background image of page 2
15-3/43 Binary Search Trees The element in any node 'splits' the elements in the node's left and right subtrees So every subtree of a BST is a BST Another way to define a BST: A Binary Search Tree (BST) is a binary tree whose elements are ordered such that:
Background image of page 3

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

View Full DocumentRight Arrow Icon
15-4/43 Binary Search Tree ADT Description This ADT describes a binary search tree (BST) whose elements are totally ordered, but otherwise their type is unspecified. Duplicates are not allowed. Properties 1. A BST is a hierarchical data structure. A node in a BST can have 0, 1, or 2 children. 2. The root is the access point to a BST. 3. Elements are inserted such that all the elements in the node’s left subtree are less than the node’s element, and all the elements in the node’s right subtree are greater than the node’s element. This is the binary search tree ordering property. 4. The elements of a BST must be comparable to one another.
Background image of page 4
15-5/43 Binary Search Tree ADT Attributes size : The number of nodes in this BinarySearchTree. root : The root of this BinarySearchTree; or null when the tree is empty. Operations BinarySearchTree ( ) pre-condition: none responsibilities: constructor; create an empty BinarySearchTree post-condition: size is set to 0 root is set to a null value returns: nothing
Background image of page 5

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

View Full DocumentRight Arrow Icon
15-6/43 Binary Search Tree ADT add ( Type element ) pre-condition: element is not null, is not already in the tree and is comparable to the other elements in the tree responsibilities: add an element to the tree such that the BST properties are maintained post-condition: size is incremented by 1; element is added to the tree returns: nothing exception: if element is null, is already in the tree or is not comparable to other elements in the tree remove( Type element ) pre-condition: element is not null and is comparable to the other elements in the tree responsibilities: remove element from the tree such that the BST properties are maintained. If the target is not in the tree, nothing happens and the tree is not changed post-condition: if element was in the tree, size is decremented by 1 and element is removed from the tree returns: nothing
Background image of page 6
15-7/43 Binary Search Tree ADT contains( Type target ) pre-condition: target is not null responsibilities: determine if target is stored in this tree post-condition: the tree is unchanged returns: true if target is found in this tree, false otherwise exception: if target is null
Background image of page 7

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

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

This note was uploaded on 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.

Page1 / 45

lec15 - CSE 12 Binary Search Trees A Binary Search Tree ADT...

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

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