CS 206 Introduction to Computer Science II

Size: px
Start display at page:

Download "CS 206 Introduction to Computer Science II"

Transcription

1 CS 206 Introduction to Computer Science II 07 / 15 / 2016 Instructor: Michael Eckmann

2 Today s Topics Questions? Comments? Binary trees implementation Binary search trees Michael Eckmann - Skidmore College - CS Summer 2016

3 Implementation of binary trees Similar to how we implemented a linked list of nodes, we can implement a binary tree of nodes where each node contains a reference to a left child and a right child (each of which could be null.) public class BTNode { private int data; public BTNode left; public BTNode right; } // constructor that sets left and right to null public BTNode(int i) { data = i; left = null; right = null; } Michael Eckmann - Skidmore College - CS Summer 2016

4 Implementation of binary trees public class BinaryTree { private BTNode root; } // constructor that sets root to null public BinaryTree() { root = null; } // plenty more methods here to insert nodes, etc... Michael Eckmann - Skidmore College - CS Summer 2016

5 A few operations on binary trees getleftmost node Starting at root, follow the left until you hit a node whose left is null. That node is the leftmost node. getrightmost node Starting at root, follow the right until you hit a node whose right is null. That node is the rightmost node. According to these definitions, will the leftmost and rightmost nodes always be leaves? Michael Eckmann - Skidmore College - CS Summer 2016

6 A few operations on binary trees The leftmost node can have a right child and the rightmost node can have a left child, so the leftmost and rightmost nodes in a binary tree aren't necessarily leaves. Later we'll talk about how to create recursive methods to remove these nodes. Michael Eckmann - Skidmore College - CS Summer 2016

7 Traversals of binary trees There are three typical ways to traverse a binary tree preorder, postorder and inorder. preorder process root process nodes in left subtree with a recursive call process nodes in right subtree with a recursive call Example on the board of a preorder traversal. Michael Eckmann - Skidmore College - CS Summer 2016

8 Traversals of binary trees postorder process nodes in left subtree with a recursive call process nodes in right subtree with a recursive call process root Example on the board of a postorder traversal. Michael Eckmann - Skidmore College - CS Summer 2016

9 Traversals of binary trees inorder process nodes in left subtree with a recursive call process root process nodes in right subtree with a recursive call Example on the board of an inorder traversal. Applet: Michael Eckmann - Skidmore College - CS Summer 2016

10 Implementing the traversals of binary trees Let's assume the processing we're doing to each node is just printing the data in that node. So, for preorder traversal we need to do the following print the root's data do a preorder traversal of the left subtree do a preorder traversal of the right subtree Notice the recursion? Michael Eckmann - Skidmore College - CS Summer 2016

11 Implementing the traversals of binary trees The only issue is when to stop the recursion. To do a preorder traversal of the left subtree there has to be a left subtree. If there is no left subtree (that is, left == null) then don't traverse that anymore. Same for a preorder traversal of the right subtree - there has to be a right subtree to traverse. If there is no right subtree (that is, right == null) then don't traverse that anymore. Michael Eckmann - Skidmore College - CS Summer 2016

12 Implementing the traversals of binary trees So, for preorder traversal we need to do the following print the root's data if (left!= null) do a preorder traversal of the left subtree if (right!= null) do a preorder traversal of the right subtree Michael Eckmann - Skidmore College - CS Summer 2016

13 Implementing binary tree traversals // this method lives inside the BinaryTree class and can only be called // from within it private void preorderprint(btnode node) { } System.out.println(node.data); if (node.left!= null) preorderprint(node.left); if (node.right!= null) preorderprint(node.right); Michael Eckmann - Skidmore College - CS Summer 2016

14 Implementing binary tree traversals // we need a way to start the traversal off... // this method lives inside the BinaryTree class public void preorderprint() { } if (root!= null) preorderprint(root); Michael Eckmann - Skidmore College - CS Summer 2016

15 binary tree representation Besides representing a binary tree as a structure of nodes where each node has data and references to other nodes (left and right). we can store a binary tree in an array. The 0 th index will hold the root data, the 1 st and 2 nd will hold the root's left and right children's data respectively. The root's left child's left and right children's data are in 3 rd and 4 th indices etc. Each level of the binary tree is stored in contiguous indices. Michael Eckmann - Skidmore College - CS Summer 2016

16 binary tree representation Node i's children are at 2i + 1 and 2i + 2. Example: root is at 0, it's left child is at 1 and right is at 2. i=0, 2i+1 = 1, 2i+2 = 2 Another example: the root's right child is at 2 and that node's children are at 5 and 6. i=2, 2i+1 = 5, 2i+2 = 6 Now, given an index of a node, how can we determine where the parent lives? Michael Eckmann - Skidmore College - CS Summer 2016

17 binary tree representation This scheme only works really well for perfect binary trees or at least complete binary trees. Why? Because if we know the number of nodes in the tree, that is the number of elements in the array. Problem when we want to add to the tree. Possible solution is to create a really large array and keep track of the index of the deepest, right node and never allow any code to look at any indices higher than that except when adding nodes. Problem if the tree is not complete. We'll have holes in the array. Michael Eckmann - Skidmore College - CS Summer 2016

18 binary tree representation Problem if the tree is not complete. We'll have holes in the array. Why is this a problem? What possible solutions are there to storing an incomplete binary tree in an array? Michael Eckmann - Skidmore College - CS Summer 2016

19 binary tree representation What possible solutions are there to storing an incomplete binary tree in an array? Could keep another array of booleans whose elements are true if the index holds a node, false if the index does not hold a node. Or if the binary tree array is an array of references, ones that do not hold a node can be null. Michael Eckmann - Skidmore College - CS Summer 2016

20 Abstract data types Now is a good time to bring up the term abstract data type (ADT). We just discussed a few different ways of storing / representing a binary tree. But these different representations were assumed to allow the same exact operations, such as traversing the tree in some order, or finding the children of a node, or finding all the ancestors of a node, etc. So, to a user of the binary tree data type, the implementation doesn't matter. Only the interface to the data type is needed to be known (e.g. What methods can be called and the parameters they take). How the data type was implemented is not needed to be known by the user of the data type. Michael Eckmann - Skidmore College - CS Summer 2016

21 Abstract data types This is a kind of information hiding. Hide how it's implemented, while still providing a way to use it. For example: The first programming assignment required a way to store a list of the students' names and scores. Different implementations were with a linked list, an Arraylist, and an array. Ideally the class containing your main method shouldn't have had to know anything about which way the list was implemented. It should have just needed to use the list by calling addstudent, changescore etc. Michael Eckmann - Skidmore College - CS Summer 2016

22 Abstract data types Information hiding is a good thing to keep in mind while coding. Think about what operations/methods are needed to be provided for users of your class and provide them, but hide everything else. This will help you to keep your classes separated well. Anyone have any comments? Michael Eckmann - Skidmore College - CS Summer 2016

23 Binary Search Trees Binary Search Trees (BSTs) Definition Example of a BST and verification that it is one Are BST's unique for a given set of data? Algorithms print keys in ascending order Search for a key Find minimum key Find maximum key Insert a key

24 Binary Search Trees Binary Search Trees (BSTs) A tree that is both a binary tree and a search tree. we know the definition of a tree and a binary tree so: We just need to define search tree. A search tree is a tree where every subtree of a node has data (aka keys) less than any other subtree of the node to its right. the keys in a node are conceptually between subtrees and are greater than any keys in subtrees to its left and less than any keys in subtrees to its right.

25 Binary Search Trees A binary search tree is a tree that is a binary tree and is a search tree. In other words it is a tree that has at most two children for each node and every node's left subtree has keys less than the node's key, and every right subtree has keys greater than the node's key. Definitions taken from (The National Institute of Standards and Technology)

26 Binary Search Trees Each node in a BST has key left reference right reference parent reference

27 Binary Search Trees F / \ B H / \ \ A D K Assume Alphabet ordering of letters. Let's verify whether it is indeed a binary search tree. What properties does it need to have again? Does it satisfy those properties?

28 Binary Search Trees Are BST's unique for a given set of keys? Let's build a tree for the list of keys 13, 45, 10, 9, 54, 11, 42 Choose 13 to put in the root and go from there

29 Binary Search Trees Are BST's unique for a given set of keys? Let's build a tree for the list of keys 13, 45, 10, 9, 54, 11, 42 What if we change the order that we insert the keys? What if we choose a different key as the root to start?

30 Binary Search Trees Let's look at algorithms to do the following Print keys in ascending order Search for a key Find minimum key Find maximum key Insert a key Delete a key Height of a BST

31 Print the Keys To print the keys in increasing order we use inorder traversal. Recursive description of inorder traversal In-order traversal of a tree Start with x being the root check if x is not null then 1) In-order traversal of left(x) 2) print key(x) 3) In-order traversal of right(x) Let's apply this algorithm to the tree and see what it does. What is the running time complexity of this algorithm for a tree that has n nodes?

32 Search for a key To search in a binary search tree for a key k, start with x being the root. Here's an iterative solution of the search public BSTNode searchkey(int searchkey) { BSTNode temp = root; } while (temp!= null && temp.key!= searchkey) { if (temp.key > searchkey) temp = temp.left; else temp = temp.right; } return temp; // may be null or not (if null then key wasn't found) What's the running time of this?

33 Search for a key To search in a binary search tree for a key k, start with x being the root. Here's a recursive solution of the search public BSTNode treesearch(bstnode temp, int k) { if (temp == null k == temp.key) return temp; if (k < temp.key) return treesearch(temp.left, k) else return treesearch(temp.right, k) } What's the running time of this?

34 Search for a key What's the running time of this? On the order of the height of the tree. What if the binary search tree is complete (or full)?

35 Find Minimum key in a BST How might we devise an algorithm to find the minimum? Where in the tree is the minimum value?

36 Find Minimum key in a BST How might we devise an algorithm to find the minimum? Where in the tree is the minimum value? It is in the leftmost node while (temp.left!= null) temp = temp.left; return temp;

37 Find Maximum key in a BST How might we devise an algorithm to find the maximum? Where in the tree is the maximum value?

38 Find Maximum key in a BST How might we devise an algorithm to do find the maximum? Where in the tree is the maximum value? It is in the rightmost node while (temp.right!= null) temp = temp.right; return temp; Running times of these?

39 Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere?

40 Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere? No, it has to follow the rules of BST's so the resulting tree after insert must be a BST. z is the node to insert and z.key is its key and its left, right and parents are null. Need to keep track of where we are in the tree as we traverse it and the parent of where we are because we might have to go back up the tree. par will be a pointer to the parent of temp as we go through the tree

41 Insert a key in a BST insert(bstnode z) // insert node z in tree T { BSTNode temp = root; BSTNode par = null; // search the tree to find the place it should go while (temp!= null) { } par = temp; if (z.key < temp.key) temp = temp.left; else temp = temp.right; // continued on next slide

42 Insert a key in a BST // now we know x is null and the parent of x is par so, z.parent = par; // insert the key either at the root or to the left or right of par if (par = null) root = z; else if (z.key < par.key) else par.left = z; par.right = z; } // any fear of overwriting a node? What if par.left or par.right // contain a node?

43 Insert a key in a BST // any fear of overwriting a node? What if par.left or par.right // contain a node? They can't contain a node because of the first half of the algorithm guarantees it Examples: Let's insert C into the original tree. Then let's insert E.

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 02 / 24 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Trees binary trees two ideas for representing them in code traversals start binary

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

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

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 05 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? binary search trees Finish delete method Discuss run times of various methods Michael

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

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

If you took your exam home last time, I will still regrade it if you want. Some Comments about HW2: 1. You should have used a generic node in your structure one that expected an Object, and not some other type. 2. Main is still too long for some people 3. braces in wrong place,

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 26 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Balanced Binary Search trees AVL trees Michael Eckmann - Skidmore College - CS

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

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

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal CS 127 Fall 2003 Yet more fun with binary trees In our last episode We looked at the complexity of searching a binary tree average complexity is O(lg n), in most cases We pondered the question, How many

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

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

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Introduction to Trees. D. Thiebaut CSC212 Fall 2014 Introduction to Trees D. Thiebaut CSC212 Fall 2014 A bit of History & Data Visualization: The Book of Trees. (Link) We Concentrate on Binary-Trees, Specifically, Binary-Search Trees (BST) How Will Java

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 09 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? More examples Change making algorithm Greedy algorithm Recursive implementation

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

B-Trees. Based on materials by D. Frey and T. Anastasio

B-Trees. Based on materials by D. Frey and T. Anastasio B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of

More information

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees! relim Updates Regrades are live until next Thursday @ :9M A few rubric changes are happening Recursion question: -0pts if you continued to print Exception handling write the output of execution of that

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

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 11 / 2015 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Sorting Searching Michael Eckmann - Skidmore College - CS 106 - Summer 2015

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

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

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures Outline Chapter 7: Trees 1 Chapter 7: Trees Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists

More information

Tree Travsersals and BST Iterators

Tree Travsersals and BST Iterators Tree Travsersals and BST Iterators PIC 10B May 25, 2016 PIC 10B Tree Travsersals and BST Iterators May 25, 2016 1 / 17 Overview of Lecture 1 Sorting a BST 2 In-Order Travsersal 3 Pre-Order Traversal 4

More information

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2018 TREES Lecture 12 CS2110 Spring 2018 Important Announcements 2 A4 is out now and due two weeks from today. Have fun, and start early! Data Structures 3 There are different ways of storing data, called data

More information

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree 0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree. Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of

More information

Lecture 6: Analysis of Algorithms (CS )

Lecture 6: Analysis of Algorithms (CS ) Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black

More information

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331 CSE2331/5331 Topic 6: Binary Search Tree Data structure Operations Set Operations Maximum Extract-Max Insert Increase-key We can use priority queue (implemented by heap) Search Delete Successor Predecessor

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees Design and Analysis of Algorithms Lecture- 9: Binary Search Trees Dr. Chung- Wen Albert Tsao 1 Binary Search Trees Data structures that can support dynamic set operations. Search, Minimum, Maximum, Predecessor,

More information

Trees. Tree Structure Binary Tree Tree Traversals

Trees. Tree Structure Binary Tree Tree Traversals Trees Tree Structure Binary Tree Tree Traversals The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The

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

TREES Lecture 12 CS2110 Fall 2016

TREES Lecture 12 CS2110 Fall 2016 TREES Lecture 12 CS2110 Fall 2016 Prelim 1 tonight! 2 5:30 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal 5:30 prelim (not the quiet room) and whose last names

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

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

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

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

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

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

! 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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises 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

More information

» Access elements of a container sequentially without exposing the underlying representation

» Access elements of a container sequentially without exposing the underlying representation Iterator Pattern Behavioural Intent» Access elements of a container sequentially without exposing the underlying representation Iterator-1 Motivation Be able to process all the elements in a container

More information

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

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1! //16 Prelim 1 tonight! :3 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal :3 prelim (not the quiet room) and whose last names begin with A through Da MUST go

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

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this

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

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

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 19 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Change making algorithm Greedy algorithm implementation Divide and conquer recursive

More information

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 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

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 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this

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

Spring 2018 Mentoring 8: March 14, Binary Trees

Spring 2018 Mentoring 8: March 14, Binary Trees CSM 6B Binary Trees Spring 08 Mentoring 8: March 4, 08 Binary Trees. Define a procedure, height, which takes in a Node and outputs the height of the tree. Recall that the height of a leaf node is 0. private

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

CSC148-Section:L0301

CSC148-Section:L0301 Slides adapted from Professor Danny Heap course material winter17 CSC148-Section:L0301 Week#8-Friday Instructed by AbdulAziz Al-Helali a.alhelali@mail.utoronto.ca Office hours: Wednesday 11-1, BA2230.

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

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

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

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

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

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

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

Binary Search Tree Binary Search tree is a binary tree in which each internal node x stores an element such that the element stored in the left subtree of x are less than or equal to x and elements stored

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

Data Structures And Algorithms

Data Structures And Algorithms Data Structures And Algorithms Binary Trees Eng. Anis Nazer First Semester 2017-2018 Definitions Linked lists, arrays, queues, stacks are linear structures not suitable to represent hierarchical data,

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

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree. Binary Search Trees Search trees are data structures that support many dynamic set operations including Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. Thus, a search tree can be

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

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 trees (BST) Binary search trees (BST)

Binary search trees (BST) Binary search trees (BST) Tree A tree is a structure that represents a parent-child relation on a set of object. An element of a tree is called a node or vertex. The root of a tree is the unique node that does not have a parent

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

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

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)

More information

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice Name Section Number CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice All Sections Bob Wilson OPEN BOOK / OPEN NOTES: You will have all 90 minutes until the start of the next class period.

More information

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

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational

More information

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1 Lecture 34 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture33/*.* In order to compile these files, also need bintree.h from last class. Project 7 posted. Due next week Friday, but

More information

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

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

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

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

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

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

Data Structures and Algorithms Winter term 2016

Data Structures and Algorithms Winter term 2016 Page 0 German University in Cairo December 26, 2016 Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Wael Abouelsaadat Data Structures and Algorithms Winter term 2016 Final Exam Bar Code

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

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39 Binary Search Tree 1.0 Generated by Doxygen 1.7.1 Mon Jun 6 2011 16:12:39 Contents 1 Binary Search Tree Program 1 1.1 Introduction.......................................... 1 2 Data Structure Index 3

More information

BBM 201 Data structures

BBM 201 Data structures BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

More information

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2 Revised 21-Mar-05 About This Lecture 2 Trees In this lecture we study a non-linear container called a Tree and a special kind of Tree called a Binary Tree. CMPUT 115 - Lecture 18 Department of Computing

More information

The Binary Search Tree ADT

The Binary Search Tree ADT The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such that the data in any internal node is Greater than the data in

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

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York CSC212 Data Structure Lecture 13 Trees and Tree Traversals Instructor: Prof. George Wolberg Department of Computer Science City College of New York Motivation Linear structures arrays dynamic arrays linked

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

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees Computer Science 10 Data Structures Siena College Fall 018 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree

More information

Traversing Trees with Iterators

Traversing Trees with Iterators Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees 4 1.1 begin()..................................... 6 1.2 operator++................................... 7 2 Iterators using Parent Pointers 11

More information

Traversing Trees with Iterators

Traversing Trees with Iterators Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees 3 1.1 begin()................................................................ 5 1.2 operator++..............................................................

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