Docsity

Object-Oriented Programming II: Trees and Binary Search Trees, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 132:
Object-Oriented Programming II
Trees & Binary Search Trees
Department of Computer Science
University of Maryland, College Park
Trees
Trees are hierarchical
data structures
One-to-many relationship
between elements
Tree node / element
Contains data
Referred to by only 1
(parent) node
Contains links to any
number of (children)
nodes
Parent node
Children nodes
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Object-Oriented Programming II: Trees and Binary Search Trees and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Trees & Binary Search Trees

Department of Computer Science

University of Maryland, College Park

Trees

Trees are hierarchical

data structures

One-to-many relationship between elements

Tree node / element

Contains data Referred to by only 1 (parent) node Contains links to any number of (children) nodes

Parent node

Children nodes

Trees

Terminology

Root โ‡’ node with no parent Leaf โ‡’ all nodes with no children Interior โ‡’ all nodes with children Root node

Leaf nodes

Interior nodes

Trees

Terminology

Sibling โ‡’ node with same parent Descendent โ‡’ children nodes & their descendents Subtree โ‡’ portion of tree that is a tree by itself โ‡’ a node and its descendents

Subtree

Siblings

Tree Traversal

Often we want to

  1. Find all nodes in tree
  2. Determine their relationship

Can do this by

  1. Walking through the tree in a prescribed order
  2. Visiting the nodes as they are encountered

Process is called tree traversal

Tree Traversal

Goal

Visit every node in binary tree

Approaches

Depth first Preorder โ‡’ parent before children Inorder โ‡’ left child, parent, right child Postorder โ‡’ children before parent Breadth first โ‡’ closer nodes first

Tree Traversal Methods

Pre-order

  1. Visit node // first
  2. Recursively visit left subtree
  3. Recursively visit right subtree

In-order

  1. Recursively visit left subtree
  2. Visit node // second
  3. Recursively right subtree

Post-order

  1. Recursively visit left subtree
  2. Recursively visit right subtree
  3. Visit node // last

Tree Traversal Methods

Breadth-first

BFS(Node n) {

Queue Q = new Queue(); Q.enqueue(n); // insert node into Q while ( !Q.empty()) { n = Q.dequeue(); // remove next node if ( !n.isEmpty()) { visit(n); // visit node Q.enqueue(n.Left()); // insert left subtree in Q Q.enqueue(n.Right());// insert right subtree in Q } }

Types of Binary Trees

Degenerate

Mostly 1 child / node Height = O(n) Similar to linear list

Balanced

Mostly 2 child / node Height = O( log(n) ) Useful for searches

Degenerate binary tree

Balanced binary tree

Binary Search Trees

Key property

Value at node Smaller values in left subtree Larger values in right subtree Example X > Y X < Z

Y

X

Z

Binary Search Trees

Examples

Binary search trees

Non-binary search tree

Binary Tree Implementation

Class Node {

Value data;

Node left, right; // null if empty

void insert ( Value data1 ) { โ€ฆ }

void delete ( Value data2 ) { โ€ฆ }

Node find ( Value data3 ) { โ€ฆ }

Example Binary Searches

Find ( 2 )

10 > 2, left

5 > 2, left 2 = 2, found

5 > 2, left

2 = 2, found

Example Binary Searches

Find ( 25 )

10 < 25, right 30 > 25, left

25 = 25, found

5 < 25, right 45 > 25, left

30 > 25, left 10 < 25, right

25 = 25, found

Binary Search Properties

Time of search

Proportional to height of tree Balanced binary tree O( log(n) ) time Degenerate tree O( n ) time Like searching linked list / unsorted array

Requires

Ability to compare key values

Binary Search Tree Construction

How to build & maintain binary trees?

Insertion Deletion

Maintain key property (invariant)

Smaller values in left subtree Larger values in right subtree

Binary Search Tree โ€“ Deletion

Algorithm

  1. Perform search for value X
  2. If X is a leaf, delete X
  3. Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree

Observation

O( log(n) ) operation for balanced tree Deletions may unbalance tree

Example Deletion (Leaf)

Delete ( 25 )

10 < 25, right 30 > 25, left

25 = 25, delete

Example Deletion (Internal Node)

Delete ( 10 )

Replacing 10 with largest value in left subtree

Replacing 5 with largest value in left subtree

Deleting leaf

Example Deletion (Internal Node)

Delete ( 10 )

Replacing 10 with smallest value in right subtree

Deleting leaf Resulting tree

Polymorphic Binary Tree Implement.

Interface Tree {

Tree insert ( Value data1 ) { โ€ฆ }

Class EmptyTree implements Tree {

Tree insert ( Value data1 ) { โ€ฆ }

Class NonEmptyTree implements Tree {

Value data;

Tree left, right; // Either Empty or NonEmpty

Tree insert ( Value data1 ) { โ€ฆ }

Example : Standard Binary Tree

Class Node {

Node left, right;

Node X { left = Y; right = Z; }

Node Y { left = null; right = null; }

Node W { left = null; right = null; }

Node Z { left = null; right = W; }

Example : Polymorphic Binary Tree

Class EmptyTree {

Class NonEmptyTree {

Tree left, right;

NonEmpty X { left = Y; right = Z; }

NonEmpty Y { left = S; right = S; }

NonEmpty W { left = S; right = S; }

NonEmpty Z { left = S; right = W; }

EmptyTree S { }