{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

17TreesBST

# 17TreesBST - CMSC 132 Object-Oriented Programming II Trees...

This preview shows pages 1–11. Sign up to view the full content.

CMSC 132: Object-Oriented Programming II Trees & Binary Search Trees Department of Computer Science University of Maryland, College Park 1

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

View Full Document
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 2
Trees Terminology Root node with no parent Leaf all nodes with no children Interior all nodes with children Root node Leaf nodes Interior nodes 3

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

View Full Document
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 4
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 5

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

View Full Document
Binary Trees Binary tree Tree with 0–2 children per node Left & right child / subtree Binary Tree Left Child Parent Right Child 6
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 7

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

View Full Document
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 8
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 9

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

View Full Document
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(
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 35

17TreesBST - CMSC 132 Object-Oriented Programming II Trees...

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

View Full Document
Ask a homework question - tutors are online