Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Similar documents
Chapter 20: Binary Trees

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Topic Binary Trees (Non-Linear Data Structures)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures and Algorithms for Engineers

March 20/2003 Jayakanth Srinivasan,

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees. (Trees) Data Structures and Programming Spring / 28

Topic 14. The BinaryTree ADT

Trees. Tree Structure Binary Tree Tree Traversals

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CSI33 Data Structures

CSC/MAT-220: Lab 6. Due: 11/26/2018

Programming II (CS300)

Trees, Binary Trees, and Binary Search Trees

Introduction to Binary Trees

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CE 221 Data Structures and Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

tree nonlinear Examples

CS350: Data Structures Tree Traversal

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Trees. Trees. CSE 2011 Winter 2007

Friday Four Square! 4:15PM, Outside Gates

Tree Data Structures CSC 221

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Why Do We Need Trees?

Binary Trees and Binary Search Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Binary Search Trees. See Section 11.1 of the text.

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

If you took your exam home last time, I will still regrade it if you want.

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Binary Trees

Binary Trees. Examples:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Partha Sarathi Mandal

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

TREES Lecture 10 CS2110 Spring2014

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

An Introduction to Trees

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

CSI33 Data Structures

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Wednesday, November 15, 2017

Tree Structures. A hierarchical data structure whose point of entry is the root node

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Recursion, Binary Trees, and Heaps February 18

TREES Lecture 12 CS2110 Fall 2016

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Data Structures - Binary Trees and Operations on Binary Trees

The Binary Search Tree ADT

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

IX. Binary Trees (Chapter 10)

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Binary Trees Fall 2018 Margaret Reid-Miller

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

Data Structures and Algorithms

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Trees. Truong Tuan Anh CSE-HCMUT

Trees. CSE 373 Data Structures

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

BBM 201 Data structures

Binary Trees and Huffman Encoding Binary Search Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Monday, April 15, We will lead up to the Analysis and Synthesis algorithms involved by first looking at three simpler ones.

CSE 230 Intermediate Programming in C and C++ Binary Tree

Binary Trees: Practice Problems

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

CSI33 Data Structures

CS 206 Introduction to Computer Science II

Lecture 26. Introduction to Trees. Trees

Largest Online Community of VU Students

Data Structures And Algorithms

CMSC 341 Lecture 10 Binary Search Trees

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

TREES Lecture 12 CS2110 Spring 2018

CS302 - Data Structures using C++

ECE 242. Data Structures

Transcription:

Laboratory 11: Expression Trees and Binary Search Trees Introduction Trees are nonlinear objects that link nodes together in a hierarchical fashion. Each node contains a reference to the data object, a reference to the left subtree, and a reference to the right subtree. Methods that process trees are often recursive. There are three primary patterns of recursive tree methods: inorder, preorder, and postorder. For inorder methods, the sequence is to do the recursive call on the left subtree, then process the node, then do the recursive call on the right subtree. For preorder methods, the sequence is to process the node, then do the recursive call on the left subtree, then do the recursive call on the right subtree. For postorder methods, the sequence is to do the recursive call on the left subtree, then do the recursive call on the right subtree, then process the node. Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them. For example, a common way to display the nodes of a tree in order would involve the following two methods: public void inorder(){ inordersub( root ); // start the recursive partner off at the root node public void inordersub(treenode p){ if (p!= null){ inordersub(p.getleft()); // print the left subtree System.out.println( p.getelement()); // print the node data inordersub(p.getright()); // print the right subtree The other two traversal patterns would simply change the order of the three statements inside the if statement. To make sure you understand these basic principles, please print out the contents of the following tree as it would appear using an inorder, preorder, and postorder traverse: InOrder: PreOrder: PostOrder:

Part A: Binary Search Trees Although Binary Search Trees are a more complex than Expression Trees, we will begin with a simplified version of a BST so you can get a sense of how trees work in general. Part B will ask you to develop an expression tree implementation. In class Demos We develop a simple (non-generic) Binary Search Tree of String class: 1) basic linking of TreeNodes 2) recursive methods: printing in order and in graphic form 3) iterative methods: add, contains, get 4) inorder, preorder and postorder traversal methods 5) returning an iterator of tree data Development Tasks 1) Complete the three traversal methods inorder, preorder and postorder 2) Make and test a method count, to count the number of nodes in a BST. This method will call its recursive partner countaux on root. CountAux will recursively count the nodes in its left and right subtrees, then return the total of them both plus 1 to include itself. 3) Make a method to compute the height of a BST. This method counts the number of nodes on the longest path from the root to any leaf node in the tree. This statistic is significant because the amount of time it can take to find an item in the tree is related to the height of the tree. int height() // pre: none // post: returns the height of the tree You can compute the height of a tree using a postorder traversal, and the following recursive definition of height: heightaux(subtree) = 0 if subtree is null else = 1 + the larger of heightaux( subtree.left) and heightaux(subtree.right) 4) Copying trees is a tricky operation, since if you are not careful, your copy will be "entangled" with the original tree (the nodes in the two trees will refer to the same data objects). Make a copy method for your BST. This method should make and return a complete copy of the original tree where every String data in every TreeNode has been duplicated (this is a "deep copy"). To duplicate a String, you can invoke the String copy constructor method, similar to the code that is underlined below: TreeNode newnode = new TreeNode(new String(subTree.data)); // using String copy constructor to duplicate a String (you may be familiar with clone, but String does not implement clone since Strings are immutable, and there are a number of reasons to avoid writing clone for your own classes. For now, it s best to use the example above to duplicate a String object.

The usual way to copy a tree is with a recursive pair of methods that implement a pre-order traversal. The copy method for BST can simply create a new empty BST, then invoke the copyaux method passing newbst and root as parameters. The copyaux method will then visit the data in its subtree parameter and add a copy of that data into the newbst, and invoke itself again on the left and right subtrees in preorder fashion. Back in copy, after the recursive copyaux method completes, the newbst is returned. Test your method using the code provided. Part B: Expression Trees Expression trees are another great way to explore the use of heirarchical data structures. In this section you will 1) create an implementation of the Expression Tree ADT using a linked tree structure. 2) define a build method that builds an expression tree from a pre-fix arithmetic expression 3) define an evaluate method that evaluates an expression tree OVERVIEW Although you ordinarily write arithmetic expressions in linear form, you treat them as hierarchical entities when you evaluate them. When evaluating the following arithmetic expression, for example, (1+3)*(6-4) you have to evaluate the two subexpressions in parentheses before multiplying their results together. You can express the above in the following tree form, indicating the order of operations by level in tree (the lowest levels must be evaluated before the higher levels). We will develop an expression tree where each node contains a char variable indicating either a digit or an operator. Then we will define methods to build and evaluate expression trees. 1. To begin, open the ExpressionTree project folder. Examine the two different program shells. The TreeNode class is contained inside the ExprTree class as it was for the BST. 2. Now examine the TestExprTree program. This program accepts a string representing a prefix expression, then passes a StringTokenizer based on this expression to the build method for the ExprTree object, which passes it on to its recursive partner buildsub. We commonly write arithmetic expressions in infix form-that is, with each operator (*,/,-,+) placed between two operands (numbers) such as (1+3)*(6-4)

To aid the construction of our tree, we will request the expression to be entered in prefix form, so the previous expression will be typed in as follows: * + 1 3 6 4 (but with no spaces) As the buildsub method recursively processes the StringTokenizer expression, the tree is constructed, one node at a time, as the following sequence of diagrams indicates: Algorithm for buildsub: Read the next arithmetic operator or numeric value. Create a node containing the operator or numeric value. i f the node contains an operator then Recursively build the subtrees that correspond to the operator's operands. else the node is a leaf node. No additional recursive calls needed.

The StringTokenizer object is a convenient aid for us to extract characters from our expression string. It works like the String iterator you built a couple labs back. By passing the StringTokenizer object to the recursive build method you have a convenient way for the recursive calls to consume the expression in the proper sequence as the tree is being built. Which traversal method is indicated by the algorithm listed above? Write the build method and its recursive partner buildsub, then view how the tree appears in the console when it's displayed. Here are some sample expressions you can try out to see how they appear. 3. The expression method prints outputs the corresponding arithmetic expression in fully parenthesized form. Implement this method now and test it. 4. The evaluate method evaluates the expression that the tree represents. To write this method you'll need to convert the numeric char data into double, which you can do with the statement tokenval = (double)character.getnumericvalue(thistoken);