Docsity

Binary Search Trees BinaryTree<E> Class (cont.), Summaries of Data Structures and Algorithms

Toggle sidebar
bg1
11/27/2017
1
Section 6.4
Binary Search Trees
BinaryTree<E> Class (cont.)
public class BinaryTree<E> {
// Data members/fields...just the root is needed
- Node<E> root;
// Constructor(s)
+ BinaryTree()
+ BinaryTree(Node<E> node)
+ BinaryTree(E data, BinaryTree<E> leftTree, BinaryTree<E> rightTree)
// Methods: Accessors
+ E getData() throws BinaryTreeException
+ boolean isEmpty()
+ void clear()
+ BinaryTree<E> getLeftSubtree()
+ BinaryTree<E> getRightSubtree()
+ boolean isLeaf()
+ int height()
- int height(Node<E> node)
+ String toString()
- void preOrderTraverse(Node<E> node, int depth, StringBuilder sb)
} // BinaryTree<E> class
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

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

  • Node root;

// 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()

  • int height(Node node)

+ 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