Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Size: px
Start display at page:

Download "Advanced Java Concepts Unit 5: Trees. Notes and Exercises"

Transcription

1 Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus on ordered trees because they support efficient searching. Please read the following short discussion about where binary trees are used: Vocabulary H Each node in a tree can have multiple successors and all items, except for the root, have exactly one predecessor. G Q A node s predecessor is called its parent. A node s successors are called its children. A node that has no children is called a leaf. The line that connects a parent to its child is called an edge or branch. A path is a sequence of edges that connect a node and one of its descendants. Path length refers to the number of edges in a path. The level of a node equals the length of the path connecting it to the root. Therefor the root is always at level zero. Note. Apparently these definitions vary a little from source to source but these are the definitions that we will use. Questions. Use the above figure to answer the following questions. The root is Name all the leaves in this tree: Node A is at level The length of the path from Node to Node H is A N E A binary tree is a tree where each node has a maximum of two children, referred to as the left child and right child. We will only work with binary trees. A binary tree is called a recursive structure because each subset is itself a binary tree, called the left or right subtree of the original tree. Page 1

2 Traversing a Tree. Traversal Method Definition ecursive Definition Sample Tree A B C Inorder left-root-right If root is not null: Traverse the left subtree inorder Visit the root Traverse the right subtree inorder Preorder root-left-right If root is not null: Visit the root Traverse the left subtree preorder Traverse the right subtree preorder Postorder left- right- root If root is not null: Traverse the left subtree postorder Traverse the right subtree postorder Visit the root Level order root, level 1, level 2, etc. Inorder traversal produces: BAC Preorder traversal produces: ABC Postorder traversal produces: BCA Level order produces: ABC Example. left subtree inorder root right subtree inorder Inorder traversal yields: W H A X S Y H X S W A Y root left subtree preorder right subtree preorder Preorder traversal yields: X H W A S Y left subtree postorder right subtree postorder Postorder traversal yields: W A H Y S X root root level 1 level 2 Level order traversal yields: X H S W A Y Page 2

3 Ex 1. What is the level of S? What is the level of A? What is the level of W? A S W What is the length of the path from to S? F Inorder traversal yields Preorder traversal yields Postorder traversal yields Level order traversal yields Ex 2. What is the level of P? What is the length of the path from A to? Inorder traversal yields A Preorder traversal yields W E Postorder traversal yields Level order traversal yields Z P How many leaves are in this tree? Ex. P Inorder traversal yields Preorder traversal yields Postorder traversal yields Level order traversal yields B F G H S T Y Z Ex 4. W Inorder traversal yields A Preorder traversal yields B C Postorder traversal yields Level order traversal yields How many leaves are in this tree? D G H E I F Page

4 Practice-It! Do the traveral1, traversal2, and traversal problems under Binary Trees in CSE 14. The remaining exercises in Binary Trees involve adding methods to the IntTree class. This class uses a nested class named IntTreeNode which has three instance variables (left, right, and data) that are accessible from within the IntTree class. Here is the solution for the size exercise: public int size(){ return size( overalloot ); private int size( IntTreeNode root ){ if ( root == null ) return 0; else return 1 + size( root.left ) + size( root.right ); Use the above solution as a model and solve the following problems. countleaves sum countleftnodes depthsum doublepositives numempty height isfull printlevel writetree Binary Expression Tree A binary expression tree consists of either a single root (containing an operand) or stores a mathematical expression according to this rule: The root contains an operator that will be applied to the results of evaluating the expressions in the left and right subtrees, each of which is a binary expression tree. Each of these trees is a binary expression tree. 1. What does a preorder traversal yield? 2. What does a postorder traversal yield?. What does an inorder traversal yield? 4. What is the value of this expression? (use any of the above expressions) What does a preorder traversal yield? 6. What does a postorder traversal yield? 7. What does an inorder traversal yield? 8. What is the value of this expression? 9 * + 2 Page 4

5 9. What does a preorder traversal yield? 10. What does a postorder traversal yield? 11. What does an inorder traversal yield? 12. What is the value of this expression? * Notice. A preorder traversal yields the prefix form. A postorder traversal yields the postfix form. An inorder traversal yields the infix form except that you might have to add parentheses. The level of the nodes indicates the precedence. The operation at the root will always be the last operation performed. Operations in the highest-level nodes (which is the bottom) are performed first. The evaluation of a binary expression tree is a recursive process because each subtree is itself another binary expression tree. Binary Search Tree. In a binary search tree, the left child is always less than its parent. The right child is always greater than the parent. There should be no duplicates. Actually, depending on whose definition you use there may be duplicates. In this course there are never any duplicates. Here is an example of a binary search trees Because of the recursive nature of a tree, everything in the left subtree of a binary search tree is less than the root. And everything in the right tree is greater than the root. For a binary search tree, an inorder traversal gets the nodes in ascending order from least to greatest. Page 5

6 Adding a value to a BST is rather simple. Compare the new value to the root value. If the root is null, then the value becomes the root node. If the new value is less than the root value, then compare it to the left node and repeat the process. If the new value is greater than the root value, then compare it to the right node and repeat the process. Sample Problem 1. Create a BST by adding these numbers, in order: 8,, 5, 9 Step 1. The first number becomes the root. 8 8 Step 2. Since the is less than 8, it becomes the left node. 8 Step. Since the 5 is less than 8, we move its left node. Since 5 is greater than the, the new node becomes the right child of the node Step 4. Since the 9 is greater the 8, it goes to the right. Sample Problem 2. 5 The BST to the right results from adding the following numbers in order 5, 0, 9, 7,, -1, Note that a different shape tree will result if the order of the numbers is changed Ex 5. Which of these trees are valid binary search trees? S Tree A T T W S Tree B S Tree C W W T Page 6

7 Ex 6. Draw a binary search tree that consists of these numbers: 2, 6, 8, 14 (added in this order). Ex 7. Draw a binary search tree that consists of these numbers: 6, 2, 8, -2, 5, 7, 14 (added in this order). Ex 8. Draw a binary search tree that consists of these numbers: H, B, S, N, C,, J (added in this order). Ex 9. Draw a binary search tree that consists of the letters in: MANICUE (added in this order). Ex 10. Below is a binary search tree. If 15 is added to the tree, show where it would be placed Ex 11. < x < Ex 12. < x < 1 x 5 x Page 7

8 To delete a node from a tree is a little more complicated. There are three cases to consider. In all cases you must first find the item to remove AND keep a reference to its parent. Case 1. The item to be removed has no children. Solution. Set the appropriate pointer in the parent (left or right child) to null. Case 2. The item to be removed has 1 child. Solution. Set the appropriate reference in the parent (of the node to be deleted) to the child. Example. Suppose we want to remove the Case. The item to be removed has 2 children. (This is the tricky one.) Solution. One approach is to (1) search for the smallest item in the item's right subtree. (2) replace the data to be removed with the data from the smallest item. () remove the smallest item from the right subtree (which will be easy because it will have (at most) one child. Example. Suppose we want to remove the Step 1. Find the smallest value in the right subtree (11) 11 4 Step 2. eplace the 7 with the Step. emove the original 11. Ex 1. edraw this tree after the 49 is deleted Page 8

9 Ex 14. edraw this tree after the 65 is deleted Ex 15. edraw this tree after the 20 is deleted Ex 16. edraw this tree after the 80 is deleted Page 9

10 Ex 17. edraw this tree after the 70 is deleted and then 44 is deleted Ex 18. Draw a binary search where the numbers have been added in this order:, 14, 44, 24 Then the 14 is deleted and then 14 is added to the tree again. Ex 19. Draw a binary search where the numbers have been added in this order: 60, 50, 40, 0, 45 Then 60 is deleted, 56 is added, and 40 is deleted. Ex 20. Draw a binary search where the numbers have been added in this order: 5, 70, 51, 66, 75, 81, 77 Then 70 is deleted and 5 is deleted. Ex 21. Add these numbers in this order: 70, 2, 45, 58, 82, 7. Then remove the 70 and add 61. What does a preorder traversal yield? What does a postorder traversal yield? What does an inorder traversal yield? Ex 22. Add these numbers in this order: 5,, 9, 1, 7, 4, 6. Then remove the 9 and add 8. What does a preorder traversal yield? What does a postorder traversal yield? What does an inorder traversal yield? Page 10

11 Implementation of a Binary Search Tree. Here s the first version our BST of Strings. public class BST{ private Node root; public BST(){ root = null; public void add( String str ){ Node node = new Node( str ); if ( root == null ) root = node; else add( root, node ); private void add( Node n, Node newnode ){ // precondition: n is not null int comparevalue = newnode.compareto( n ); if ( comparevalue == 0 ) // already in the tree return; // no duplicates allowed else if ( comparevalue < 0 && n.left == null ) n.left = newnode; else if ( comparevalue < 0 ) add( n.left, newnode ); else if ( comparevalue > 0 && n.right == null ) n.right = newnode; else add( n.right, newnode ); private class Node implements Comparable<Node> { private String s; private Node left, right; private Node( String s ){ if ( s == null ) throw new IllegalArgumentException( "What?!" ); this.s = s; left = null; right = null; public int compareto( Node n ){ return s.compareto( n.s ); public boolean equals( Node n ){ if ( n == null ) return false; return this.s.equals( n.s ); Page 11

12 Program 1. Add and complete the following methods for the BST class. public void postorder(){ if ( root == null ) return; else postorder( root ); System.out.println(); private void postorder(node n ){ Complete this method. It should traverse and print out the contents of the tree in post-order. This can be done with 5 lines of code. Program 2. Add and complete the following methods for the BST class. public String findsmallest(){ This returns the least string in the tree (but does not delete it). You should write a private recursive helper method. If the root is null then return null. Program. Here s the big one. Write the remove method. public void remove( String str ){ Delete the string from the tree following the guidelines discussed earlier. If the string is not in the tree then do nothing. This is not a simple method. At some point you will make a recursive call to a method. When you do, you will need to pass the string, the node you are checking, and the parent of the node you are checking. You may want to write a search method that searches the tree for str. Then, if it finds it, call a private helper method to delete that node. Page 12

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises dvanced Java Concepts Unit 5: Trees. Notes and Exercises Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

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

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 Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex

More information

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

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Binary Trees and Binary Search Trees

Binary Trees and Binary Search Trees Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe

More information

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD Data Structures Trees By Dr. Mohammad Ali H. Eljinini Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use

More information

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

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

Principles of Computer Science

Principles of Computer Science Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1 Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary

More information

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

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it. Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 49 Module 09 Other applications: expression tree

More information

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

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected. Chapter 4 Trees 4-1 Trees and Spanning Trees Trees, T: A simple, cycle-free, loop-free graph satisfies: If v and w are vertices in T, there is a unique simple path from v to w. Eg. Trees. Spanning trees:

More information

Associate Professor Dr. Raed Ibraheem Hamed

Associate Professor Dr. Raed Ibraheem Hamed Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 Department of Computer Science _ UHD 1 What this Lecture

More information

CS350: Data Structures Tree Traversal

CS350: Data Structures Tree Traversal Tree Traversal James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Defining Trees Recursively Trees can easily be defined recursively Definition of a binary

More information

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

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

We have the pointers reference the next node in an inorder traversal; called threads

We have the pointers reference the next node in an inorder traversal; called threads Leaning Objective: In this Module you will be learning the following: Threaded Binary Tree Introduction: Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked

More information

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

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them. 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

More information

Binary Trees and Huffman Encoding Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees Binary Trees and Huffman Encoding Binary Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary is a

More information

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012 CMPSCI 187: Programming With Data Structures Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012 Binary Search Trees Why Binary Search Trees? Trees, Binary Trees and Vocabulary The BST

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted, [1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n

More information

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information

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

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement

More information

Partha Sarathi Mandal

Partha Sarathi Mandal MA 252: Data Structures and Algorithms Lecture 16 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati Deletion in BST Three cases Case 1:

More information

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 Name : Section (eg. AA) : TA : This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The

More information

Binary Trees. Examples:

Binary Trees. Examples: Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected

More information

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

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! ADTʼs Weʼve Studied! Position-oriented ADT! List! Stack! Queue! Value-oriented ADT! Sorted list! All of these are linear! One previous item;

More information

Lecture 23: Binary Search Trees

Lecture 23: Binary Search Trees Lecture 23: Binary Search Trees CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki 1 BST A binary tree is a binary search tree iff it is empty or if the value of every node is both greater than or equal

More information

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

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

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS ASSIGNMENTS h0 available and due before 10pm on Monday 1/28 h1 available and due before 10pm on Monday 2/4 p1 available and due before 10pm on Thursday 2/7 Week 2 TA Lab Consulting - See schedule (cs400

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

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

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1 CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive

More information

Friday Four Square! 4:15PM, Outside Gates

Friday Four Square! 4:15PM, Outside Gates Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.

More information

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) IX. Binary Trees (Chapter 10) -1- A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following: a. If Loc >= length

More information

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

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 Chapter 7 Trees 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 Tree Terminology Parent Ancestor Child Descendant Siblings

More information

STUDENT LESSON AB30 Binary Search Trees

STUDENT LESSON AB30 Binary Search Trees STUDENT LESSON AB30 Binary Search Trees Java Curriculum for AP Computer Science, Student Lesson AB30 1 STUDENT LESSON AB30 Binary Search Trees INTRODUCTION: A binary tree is a different kind of data structure

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Binary Search Tree (2A) Young Won Lim 5/17/18

Binary Search Tree (2A) Young Won Lim 5/17/18 Binary Search Tree (2A) Copyright (c) 2015-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

tree nonlinear Examples

tree nonlinear Examples The Tree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example 10-2

More information

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

Introduction to Computers and Programming. Concept Question

Introduction to Computers and Programming. Concept Question Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 7 April 2 2004 Concept Question G1(V1,E1) A graph G(V, where E) is V1 a finite = {}, nonempty E1 = {} set of G2(V2,E2) vertices and

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

CMSC 341. Binary Search Trees CMSC 341 BST

CMSC 341. Binary Search Trees CMSC 341 BST CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary

More information

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

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

Trees, Binary Trees, and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

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

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false. Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.

More information

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge. Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Topic 14. The BinaryTree ADT

Topic 14. The BinaryTree ADT Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

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

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization. Chapter Contents Chapter 18 Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals of a General Tree Java Interfaces for Interfaces for All

More information

Binary Search Trees. See Section 11.1 of the text.

Binary Search Trees. See Section 11.1 of the text. Binary Search Trees See Section 11.1 of the text. Consider the following Binary Search Tree 17 This tree has a nice property: for every node, all of the nodes in its left subtree have values less than

More information

Cpt S 122 Data Structures. Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Motivation Trees are one of the most important and extensively

More information

Recursion, Binary Trees, and Heaps February 18

Recursion, Binary Trees, and Heaps February 18 Recursion, Binary Trees, and Heaps February 18 19 20 Recursion Review BST: Begin Slides Early Dismissal Problems on board BST Problems 23 24 25 26 27 Binary Tree problems QUIZ: Drawing trees and Traversals

More information

Introduction to Binary Trees

Introduction to Binary Trees Introduction to inary Trees 1 ackground ll data structures examined so far are linear data structures. Each element in a linear data structure has a clear predecessor and a clear successor. Precessors

More information

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must

More information

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees Chapter 11 Copyright McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. Chapter Summary Introduction to Trees Applications

More information

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech Trees Dr. Ronaldo Menezes Hugo Serrano (hbarbosafilh2011@my.fit.edu) Introduction to Trees Trees are very common in computer science They come in different variations They are used as data representation

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

More information

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

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

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

CSE 230 Intermediate Programming in C and C++ Binary Tree CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu Introduction to Tree Tree is a non-linear data structure

More information

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks

More information

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees Issued: 24. 08. 2016 Exercise

More information

Binary Trees: Practice Problems

Binary Trees: Practice Problems Binary Trees: Practice Problems College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Warmup Problem 1: Searching for a node public boolean recursivesearch(int

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Binary Search Trees 1

Binary Search Trees 1 Binary Search Trees 1 The Problem with Linked Lists 8Accessing a item from a linked list takes O(N) time for an arbitrary element 8Binary trees can improve upon this and reduce access to O( log N ) time

More information

Successor/Predecessor Rules in Binary Trees

Successor/Predecessor Rules in Binary Trees Successor/Predecessor Rules in inary Trees Thomas. nastasio July 7, 2003 Introduction inary tree traversals are commonly made in one of three patterns, inorder, preorder, and postorder. These traversals

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node 6-TREE Data Structure Management (330701) Tree: A tree is defined as a finite set of one or more nodes such that There is a special node called the root node R. The remaining nodes are divided into n 0

More information

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010 Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International

More information

TREES Lecture 10 CS2110 Spring2014

TREES Lecture 10 CS2110 Spring2014 TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there

More information

CS 171: Introduction to Computer Science II. Binary Search Trees

CS 171: Introduction to Computer Science II. Binary Search Trees CS 171: Introduction to Computer Science II Binary Search Trees Binary Search Trees Symbol table applications BST definitions and terminologies Search and insert Traversal Ordered operations Delete Symbol

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

Tree Data Structures CSC 221

Tree Data Structures CSC 221 Tree Data Structures CSC 221 Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition:

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6 CS61B Lecture #20 Today: Trees Readings for Today: Data Structures, Chapter 5 Readings for Next Topic: Data Structures, Chapter 6 Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 1 A Recursive

More information

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Trees Chapter 11 Chapter Summary Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Introduction to Trees Section 11.1 Section Summary Introduction to Trees

More information

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE373 Fall 2013, Midterm Examination October 18, 2013 CSE373 Fall 2013, Midterm Examination October 18, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop

More information

Binary Trees Fall 2018 Margaret Reid-Miller

Binary Trees Fall 2018 Margaret Reid-Miller Binary Trees 15-121 Fall 2018 Margaret Reid-Miller Trees Fall 2018 15-121 (Reid-Miller) 2 Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also

More information