Download Binary Search Trees BinaryTree Class (cont.) and more Summaries Data Structures and Algorithms in PDF only on Docsity!
Section 6.
Binary Search Trees
BinaryTree Class (cont.)
public class BinaryTree {
// Data members/fields...just the root is needed
// Constructor(s)
+ BinaryTree()
+ BinaryTree(Node node)
+ BinaryTree(E data, BinaryTree leftTree, BinaryTree rightTree)
// Methods: Accessors
+ E getData() throws BinaryTreeException
+ boolean isEmpty()
+ void clear()
+ BinaryTree getLeftSubtree()
+ BinaryTree getRightSubtree()
+ boolean isLeaf()
+ int height()
+ String toString()
- void preOrderTraverse(Node node, int depth, StringBuilder sb)
} // BinaryTree class
Coding…
Run the BinaryTree code…
Overview of a Binary Search Tree
Recall the definition of a binary search tree:
A set of nodes T is a binary search tree if either of the
following is true
T is empty
If T is not empty, its root node has two subtrees, TL and TR,
such that TL and TR are binary search trees and the value in
the root node of T is greater than all values in TL and less
than all values in TR
Searching a Binary Tree
Searching for "kept"
Performance
Search a tree is generally O(log n )
If a tree is not very full, performance will be worse
Searching a tree with only
right subtrees, for example,
is O (n )
Implementing Binary Search Trees
public class BST <E extends Comparable> extends BinaryTree { // Inherit all of the functionality of BinaryTree class // Plus, add the following to it… private int size = 0; // Methods … } // class BST
Since it is a search tree, we will need to inherently provide
for ordering (comparison) of elements in the tree.
By, specifying that type E extends Comparable, we will
be able to freely use the compareTo() method for
ordering.
Implementing Binary Search Trees
public class BST <E extends Comparable> extends BinaryTree { // Inherit all of the functionality of BinaryTree class // Plus, add the following to it… private int size = 0; // Methods **+ int size()
- void clear()**
- void add(E item)
- E find(E item)
- boolean contains(E item)
- E delete(E item)
- boolean remove(E item) } // class BST
Implementing Binary Search Trees
public void add(E item) { root = add(root, item); size++; } // add(item)
root
BST
root
BST Node Node Node null
Implementing Binary Search Trees
private void add(Node node, E item) { // Insert item (preserving BST property) at or below node. // Returns node at/under which the item was inserted. } // add(item)
Implementing Binary Search Trees
private void add(Node node, E item) { // Insert item (preserving BST property) at or below node. // Returns node at/under which the item was inserted. if (node == null) { // Insert item right here } else { // if item is less than or equal to data at node // insert below node’s left } else { // if item is greater than item at node // insert below node’s right } } // add(item)
Implementing Binary Search Trees
private void add(Node node, E item) { // Insert item (preserving BST property) at or below node. // Returns node at/under which the item was inserted. if (node == null) { // Insert item right here return new Node(item); } else { // if item is less than or equal to data at node // insert below node’s left if (item.compareTo(node.data) <= 0) { node.left = add(node.left, item); return node; } } else { // item is greater than item at node // insert below node’s right node.right = add(node.right, item); return node; } } // add(item)
Implementing Binary Search Trees
private E find(Node node, E item) { // Find and return item at or below node if (node == null) { // item does not exist } else { // item is equal to item at node… } else { // item is less than item at node… } else { // item is greater than item at node } } // find(node, item)
Implementing Binary Search Trees
private E find(Node node, E item) { // Find and return item at or below node if (node == null) { // item does not exist return null; } int compResult = item.compareTo(node.data); else if (compResult == 0){ // item is equal to item at node… return node.data; } else if (compResult < 0) { // item is less than item at node…look below the left subtree return find(node.left, item); } else { // item is greater than item at node…look below right subtree return find(node.right, item); } } // find(node, item)
Implementing Binary Search Trees
public class BST<E extends Comparable> extends BinaryTree { // Inherit all of the functionality of BinaryTree class // Plus, add the following to it… private int size = 0; // Methods
- int size()
- void clear()
- void add(E item)
- E find(E item) **+ boolean contains(E item)
- E delete(E item)
- boolean remove(E item)** } // class BST
Removal from a Binary Search Tree
If the item to be removed has no children, simply
delete the reference to the item
If the item to be removed has only one child,
change the reference to the item so that it
references the item’s only child
Removing from a Binary Search Tree
(cont.)
Section 6.
Heaps and Priority Queues
Heaps and Priority Queues
A heap is a complete binary tree with the following
properties
The value in the root is
a smallest/largest item in the
tree
Every nonempty subtree is
a heap
Min Heap: Value in root is smallest item in tree
Max Heap: Value in root is largest item in tree
Heaps and Priority Queues
A heap is a complete binary tree with the following
properties
The value in the root is
a smallest/largest item in the
tree
Every nonempty subtree is
a heap
Min Heap: Value in root is smallest item in tree
Max Heap: Value in root is largest item in tree
Note: A heap is a complete binary tree.
Inserting an Item into a Heap (cont.)
Inserting an Item into a Heap (cont.)
Inserting an Item into a Heap (cont.)
Removing an Item from a Heap
Removing an Item from a Heap (cont.)
Removing an Item from a Heap (cont.)
Removing an Item from a Heap (cont.)
Implementing a Heap
public class heap {
// Data Structure
// Constructor(s)
// Methods
+ add(E item)
+ remove(E item)
} // class heap