Lecture09

# Lecture09 - Tree Species (Binary Trees, Binary Search...

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

Tree Species (Binary Trees, Binary Search Trees) EECS 233

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

View Full Document
-2- Previous Lecture Binary Tree Representation in Java public class LinkedTree { private class Node { private int key; private String data; private Node left; // reference to left child private Node right; // reference to right child } private Node root; }
-3- Binary Trees and Expressions We ll restrict ourselves to fully parenthesized expressions and to the following binary operators: +, –, *, / Example expression: ((a + (b * c)) – (d / e) ) Tree representation: Leaf nodes are variables or constants; interior nodes are operators. Because the operators are binary, either a node has two children or it has none.

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

View Full Document
-4- Traversing An Expression Tree Inorder gives conventional inﬁx expression. print ( before the recursive call on the left subtree print ) after the recursive call on the right subtree for tree at right: ((a + (b * c)) – (d / e)) parenthesis to avoid ambiguity Preorder gives functional notation. print ( s and ) s as for inorder for tree above: – ( + (a, *(b c)) / (d e)), or – + a * b c / d e Postorder gives the postﬁx expression, which can be computed using a stack. for tree above: a b c * + d e / –
-5- Binary Search Trees Search-tree property: for each node k: all nodes in k s left subtree are < all nodes in s right subtree are >= Our earlier binary-tree example is a search tree: Performing an inorder traversal of a binary search tree visits the nodes in sorted order.

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

View Full Document
-6- Searching An Item in A Binary Search Tree Algorithm for searching for an item with a key k : if k == the root node s key, you re done else if < the root node s key, search the left subtree else search the right subtree Example: search for 7 search for 30?
-7- Implementing Search using Recursion

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## Lecture09 - Tree Species (Binary Trees, Binary Search...

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

View Full Document
Ask a homework question - tutors are online