CS101-08b Trees Part 2

CS101-08b Trees Part 2 - CJD 08b Trees(Part 2 08b...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CJD 08b Trees (Part 2) 08b Trees (Part 2) Expression Trees, Binary Expression Trees, Binary Search Trees and Heaps Search Trees and Heaps
Image of page 1

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

View Full Document Right Arrow Icon
CJD Expression Trees Expressions Trees – unambiguous representation of arithmetic, relational or logical expressions. Polish notation was invented by Polish logician Jan Lukasiewicz in 1920s using expression trees. Internal nodes are for operators and leaves are for operands. The operators’ operands are the values of its subtrees.
Image of page 2
CJD Expression Trees Example A + B / C – D * E : Order of precedence: / * + - Arithmetic Expressions with strictly binary operators are represented by full binary trees. + A / B D C E - * Observe : Inorder : A + B / C – D * E ( Infix Notation ) Preorder : - + A / B C * D E ( Prefix Notation ) Postorder : A B C / + D E * - ( Postfix Notation )
Image of page 3

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

View Full Document Right Arrow Icon
CJD Creating Expression Trees convert expression into postfix notation create an empty stack for each token (oprtrs or oprnds) of postfix expression (l to r), if the token is an operand, create a node for it push it into the stack if the token is an operator, create a node for it for j = number of operands required by operator downto 1 pop a node from the stack make it the jth child of operator push the operator node into the stack return pop(stack)
Image of page 4
CJD Bottom-Up Tree Creation Given: A + B / C – D * E Postfix: A B C / + D E * - + A / B D C E - * A B A C B A B A A / A A + D + E D + D + + * + - + Stack :
Image of page 5

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

View Full Document Right Arrow Icon
CJD Binary Search Trees For every node x, the key value in x is … larger than the key values of each node in the left subtree and smaller than the key values of each node in the right subtree K < K > K
Image of page 6
CJD A Binary Search Tree class public class BST extends BT { public BTNode search (String k ) { /* … to be specified later … */ } public void insert (String k) { /* … to be specified later … */ } public void delete (String k) { /* … to be described later … */ } }
Image of page 7

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

View Full Document Right Arrow Icon
CJD Searching public BTNode search (String k ) { BTNode n = root; while (n != null) if ( k.compareTo(n.key) == 0 ) return n; // found else if ( k.compareTo(n.key) < 0 ) n = n.left; // search left subtree else n = n.right; // search right subtree return null; // not found }
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern