CS 206 Introduction to Computer Science II

Similar documents
CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

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

Programming II (CS300)

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

Binary Trees Fall 2018 Margaret Reid-Miller

Principles of Computer Science

Binary Trees

Chapter 20: Binary Trees

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

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

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

Binary Trees, Binary Search Trees

Associate Professor Dr. Raed Ibraheem Hamed

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

CS 206 Introduction to Computer Science II

Trees. Tree Structure Binary Tree Tree Traversals

Data Structures and Algorithms for Engineers

TREES. Trees - Introduction

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

Topic 14. The BinaryTree ADT

Successor/Predecessor Rules in Binary Trees

IX. Binary Trees (Chapter 10)

Binary Search Trees. See Section 11.1 of the text.

Trees. Truong Tuan Anh CSE-HCMUT

INF2220: algorithms and data structures Series 1

Tree Travsersals and BST Iterators

Programming II (CS300)

CS350: Data Structures Tree Traversal

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

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

Trees, Binary Trees, and Binary Search Trees

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Programming II (CS300)

March 20/2003 Jayakanth Srinivasan,

Cpt S 122 Data Structures. Data Structures Trees

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

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

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

CSC148-Section:L0301

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

CSC148 Week 6. Larry Zhang

CSC148-Section:L0301

CS 206 Introduction to Computer Science II

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

Data Structures And Algorithms

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

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

tree nonlinear Examples

Binary Trees. Examples:

Tree traversals and binary trees

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

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

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

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

STUDENT LESSON AB30 Binary Search Trees

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

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

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

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

CE 221 Data Structures and Algorithms

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

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

Data Structures and Algorithms

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

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

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

Garbage Collection: recycling unused memory

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

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

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

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

CS 350 : Data Structures Binary Search Trees

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

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

The Binary Search Tree ADT

Data Structure Advanced

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

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

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

Friday Four Square! 4:15PM, Outside Gates

TREES Lecture 12 CS2110 Fall 2016

TREES Lecture 10 CS2110 Spring2014

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

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

CS350: Data Structures Binary Search Trees

Binary Trees: Practice Problems

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

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

Title Description Participants Textbook

Binary Search Tree (3A) Young Won Lim 6/6/18

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

CS 206 Introduction to Computer Science II

Transcription:

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 search trees

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; }

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

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?

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.

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.

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.

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.

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?

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.

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

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

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

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.

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?

binary tree representation This scheme 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.

binary tree representation Problem if the tree is not complete. We'll have holes in the array. Why is this a problem?

binary tree representation Problem if the tree is not complete. We'll have holes in the array. Why is this a problem? wasted space need some indicator value that it a node is not there (e.g. if the type was a class type, null would be a good choice)

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

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.

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 http://www.nist.gov/ (The National Institute of Standards and Technology)

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?

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

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?

Binary Search Trees Let's assume array implementation Consider algorithms for Search Insert