17TreesBST

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

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

View Full Document Right Arrow Icon
CMSC 132: Object-Oriented Programming II Trees & Binary Search Trees Department of Computer Science University of Maryland, College Park 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Binary Trees Binary tree Tree with 0–2 children per node Left & right child / subtree Binary Tree Left Child Parent Right Child 6
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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(
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online