CS101-08b Trees Part 2

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

• 27

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

CJD 08b Trees (Part 2) 08b Trees (Part 2) Expression Trees, Binary Expression Trees, Binary Search Trees and Heaps Search Trees and Heaps

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

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.
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 )

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

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)
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 :

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

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
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 … */ } }

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

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 }
This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• maam
• CJD

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern