CS 206 Introduction to Computer Science II

Similar documents
CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Programming II (CS300)

Binary Trees

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

Principles of Computer Science

Binary Trees, Binary Search Trees

Binary Trees Fall 2018 Margaret Reid-Miller

Chapter 20: Binary Trees

CE 221 Data Structures and Algorithms

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

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

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

Topic 14. The BinaryTree ADT

Programming II (CS300)

CSC148 Week 6. Larry Zhang

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

INF2220: algorithms and data structures Series 1

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

tree nonlinear Examples

TREES. Trees - Introduction

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

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

Trees. Truong Tuan Anh CSE-HCMUT

Garbage Collection: recycling unused memory

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

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

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Trees. Tree Structure Binary Tree Tree Traversals

Trees, Binary Trees, and Binary Search Trees

Associate Professor Dr. Raed Ibraheem Hamed

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

TREES Lecture 10 CS2110 Spring2014

Tree traversals and binary trees

IX. Binary Trees (Chapter 10)

Why Do We Need Trees?

Trees. Trees. CSE 2011 Winter 2007

Programming II (CS300)

INF2220: algorithms and data structures Series 1

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

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

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

March 20/2003 Jayakanth Srinivasan,

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

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

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

CS302 - Data Structures using C++

ITI Introduction to Computing II

Successor/Predecessor Rules in Binary Trees

Cpt S 122 Data Structures. Data Structures Trees


Trees. CSE 373 Data Structures

CSI33 Data Structures

CSE 214 Computer Science II Introduction to Tree

Trees: examples (Family trees)

ITI Introduction to Computing II

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

Algorithms and Data Structures

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

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

STUDENT LESSON AB30 Binary Search Trees

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

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

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

Chapter 5. Binary Trees

Data Structures and Algorithms

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Tree Data Structures CSC 221

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

BBM 201 Data structures

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

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

COMP : Trees. COMP20012 Trees 219

Binary Trees. Examples:

CS 206 Introduction to Computer Science II

Binary Trees. Height 1

Data Structure - Binary Tree 1 -

Stacks, Queues and Hierarchical Collections

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

Building Java Programs

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

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

CSC148H Week 8. Sadia Sharmin. July 12, /29

CS 151. Binary Trees. Friday, October 5, 12

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

Lecture 26. Introduction to Trees. Trees

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

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

Data Structures and Algorithms for Engineers

Binary trees. Binary trees. Binary trees

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

CS 206 Introduction to Computer Science II

Data Structures And Algorithms

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

TREES Lecture 12 CS2110 Spring 2018

Transcription:

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

Linked lists A dummy head and tail (nodes that always exist but do not hold actual data) can be useful to reduce the special cases of many operations Empty list will be when head.next == tail and when tail.prev == head head.prev and tail.next as usual, will always be null Special cases where head or tail would need to be updated are no longer special cases Example on board

Trees The next group of data structures that we will learn are trees. As we learn about them over the next several class periods, we will discuss what they can be used for, and why. Some things seem to be naturally modelled as trees (think biology classification of living things --- Kingdom, Phylum, Class, etc.) and some algorithms depend on the data to be in some kind of tree to be efficient.

Tree terminology A tree is a data structure consisting of a finite (possibly empty) set of nodes. If the tree is nonempty it has one root node. Each node in a tree can have 0 or more children. Each node in a tree has exactly one parent, except the root which has 0 parents. Starting at any node in the tree a path can be traced to the root by following the parent at each step. Picture on the board.

Tree terminology A binary tree is a data structure consisting of a finite (possibly empty) set of nodes. If the tree is nonempty it has one root node. Each node in a tree can have 0, 1 or 2 children. Each node in a tree has exactly one parent, except the root which has 0 parents. Starting at any node in the tree a path can be traced to the root by following the parent at each step. Picture on the board.

Tree terminology The root node in a tree is the one without a parent. The parent of a node n, in a tree, is the node that has n as one of its children. Leaf nodes are those that have 0 children. A subtree is a tree within a tree that has as its root any node in the original tree. The depth of a node is the number of steps away that node is from the root. The depth of the root is 0. The depth of the root's children are 1, etc. The depth of a tree is the maximum depth of any of its leaves.

Tree terminology A perfect binary tree is a binary tree where every leaf has the same depth and all nodes have 0 or 2 children. A complete binary tree is a binary tree where the leaves with the maximum depth are all as far left as possible (and any other leaves are only one depth less). In other words, every level of the tree except the deepest level, must contain as many nodes as possible. At the deepest level the nodes must be as far left as possible. Examples on the board.

Tree terminology Two nodes are siblings if they have the same parent.

Exercise How many nodes are there in a perfect binary tree of depth 8?

Exercise How many nodes are there in a perfect binary tree of depth 8? root level has 1 node (which is 2 0 ) next level has 2 nodes (which is 2 1 ) next level has 4 nodes (which is 2 2 ) next level has 8 nodes (which is 2 3 ) next level has 16 nodes (which is 2 4 ) next level has 32 nodes (which is 2 5 ) next level has 64 nodes (which is 2 6 ) next level has 128 nodes (which is 2 7 ) next level has 256 nodes (which is 2 8 ) These added up are 2 9 1 = 512 1 = 511.

Exercise How many nodes are there in a complete binary tree of depth 4?

Exercise How many nodes are there in a complete binary tree of depth 4? root level has 1 node (which is 2 0 ) next level has 2 nodes (which is 2 1 ) next level has 4 nodes (which is 2 2 ) next level has 8 nodes (which is 2 3 ) next level has any number from 1 to 16 nodes So, adding these is from 16 to 31 nodes. When a binary tree has 31 nodes is it guaranteed to be a perfect binary tree?

Is a binary tree of 12 nodes perfect? complete? Exercise

Is a binary tree of 12 nodes perfect? No complete? possibly Exercise

Application of binary trees The game 20 questions I think of something and the game consists of you asking me only yes/no questions (e.g. Is it bigger than a bread box?) Each node can contain a question (or answer) If it contains a question, If the answer to the question is yes then the left child node contains the next question to ask (or the answer to give) If the answer to the question is no then the right child node contains the next question to ask (or the answer to give)

Application of binary trees What could you say about the nodes that contain answers, not questions? Assuming a perfect binary tree of depth 20 to be used for the game, how many possible answers could your tree differentiate among?

Application of binary trees What could you say about the nodes that contain answers, not questions? They are leaves. Assuming a perfect binary tree of depth 20 to be used for the game, how many possible answers could your tree differentiate among? 2 20 = 1048576 (over 1 million) that's the number of leaves The number of questions in the tree are 2 20 1 = 1048575 The number of nodes in the tree are 2 21 1 = 2097151

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. Applet: http://www.cosc.canterbury.ac.nz/mukundan/dsal/btree.html

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

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?

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.