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

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

View Full Document Right Arrow Icon
Tree Species (Binary Trees, Binary Search Trees) EECS 233
Background image of page 1

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

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

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

View Full DocumentRight Arrow Icon
-4- Traversing An Expression Tree Inorder gives conventional infix 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 postfix expression, which can be computed using a stack. for tree above: a b c * + d e / –
Background image of page 4
-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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
-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?
Background image of page 6
-7- Implementing Search using Recursion
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online