Docsity

Trees & Binary Search Trees - OBJECT-ORIENTED PROGRAMME II | CMSC 132, Study notes of Computer Science

Partial preview of the text

Download Trees & Binary Search Trees - OBJECT-ORIENTED PROGRAMME II | CMSC 132 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 hierarchicaldata structures^ One-to-many relationshipbetween elements Tree node / element^ Contains data^ Referred to by only 1(parent) node^ Contains links to anynumber of (children)nodes

Parent node Children 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

Trees

Terminology^ Level

⇒^ is a measure of a node’s distance from root Definition of level^ If node is the root of the tree, its level is 1^ Else, the node’s level is 1 + its parent’s level Height (depth)

⇒^ max level of any node in tree

Height = 3

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

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 } }

Tree Traversal Examples

Pre-order (prefix)^ +^

×^ 2 3 / 8 4

In-order (infix)^2

×^ 3 + 8 / 4

Post-order (postfix)^ 2 3

×^ 8 4 / +

Breadth-first^ +^

×^ / 2 3 8 4

×^

Expression tree

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

Degeneratebinary tree

Balancedbinary 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 Tree Implementation

Class Node {^ Value data;^ Node left, right;

// null if empty

void insert ( Value data1 ) { … } void delete ( Value data2 ) { … } Node find ( Value data3 ) { … }^ … }

Iterative Search of Binary Tree

Node Find( Node n, Value key) {^ while (n != null) {

if (n.data == key)

// Found it

return n; if (n.data > key)

// In left subtree

n = n.left; else^

// In right subtree

n = n.right;

} return null; } Find( root, keyValue );

Example Binary Searches

Find ( 2 )

10 > 2, left 5 > 2, left 2 = 2, found 45

5 > 2, left 2 = 2, found

Example Binary Searches

Find ( 25 )

10 < 25, right 30 > 25, left 25 = 25, found 45

5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found