TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Binary Trees

Programming II (CS300)

Programming II (CS300)

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

Binary Trees, Binary Search Trees

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

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

CSC148 Week 6. Larry Zhang

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

CS24 Week 8 Lecture 1

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

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

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Trees. CSE 373 Data Structures

Principles of Computer Science

Why Do We Need Trees?

Garbage Collection: recycling unused memory

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

CE 221 Data Structures and Algorithms

Data Structures and Algorithms

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Programming Abstractions

CS 106X Lecture 18: Trees

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

Trees. Eric McCreath

Trees. Trees. CSE 2011 Winter 2007

ITI Introduction to Computing II

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

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

ITI Introduction to Computing II

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Stacks, Queues and Hierarchical Collections

CSE 214 Computer Science II Introduction to Tree

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

EE 368. Weeks 5 (Notes)

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Topic 14. The BinaryTree ADT

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

TREES. Trees - Introduction

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Data Structure. IBPS SO (IT- Officer) Exam 2017

Trees. Carlos Moreno uwaterloo.ca EIT

CS301 - Data Structures Glossary By

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

Binary Trees Fall 2018 Margaret Reid-Miller

COMP : Trees. COMP20012 Trees 219

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

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

Trees. Tree Structure Binary Tree Tree Traversals

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

Module 6: Binary Trees

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

Trees. A tree is a directed graph with the property

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

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

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

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

Trees, Binary Trees, and Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

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

CS 206 Introduction to Computer Science II

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

Binary Trees. Height 1

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

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

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

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Data Structures and Algorithms

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

TREES Lecture 10 CS2110 Spring2014

Data Structures and Algorithms

Trees Algorhyme by Radia Perlman

Data Structures Question Bank Multiple Choice

tree nonlinear Examples

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

Binary Trees Due Sunday March 16, 2014

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

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

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

BBM 201 Data structures

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

Programming II (CS300)

CS302 - Data Structures using C++

B-Trees and External Memory

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSE 214 Computer Science II Heaps and Priority Queues

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

Data Structures and Algorithms

Chapter 5. Binary Trees

3. Fundamental Data Structures

Trees. Truong Tuan Anh CSE-HCMUT

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

Transcription:

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1

administrivia 2

-assignment 7 due Thursday at midnight -asking for regrades through assignment 5 and midterm must be complete by Friday 3

assignment 5 scores number of students score 4

last time 5

-a queue is a FIRST-IN, FIRST-OUT data structure -FIFO -insert on the back, remove from the front -operations: -enqueue adds an item to the back of the queue -dequeue removes and returns the item at the front terminology avoids confusion with a stack! -like a stack, all operations are O(1) 6

as an array -keep track of front and back indices -front and back advance through the array -enqueueing advances back -dequeueing advance front 14 8 7 front back -what happens when back reaches the end of the array? 7

as a linked list -remember, inserting and deleting to the head and tail of a linked list is automatically O(1) -front is analogous to head -back is analogous to tail -no messy wrap-around, or growth issues 14 front (head) 8 7 10 back (tail) -which linked list operations are analogous to enqueue and dequeue? 8

summary -linked lists and wrap-around arrays are both O(1) for queue implementations -BUT, arrays are much more complicated to code -both queues and stacks require very little code on top of a good linked list implementation 9

priority queues 10

using a linked list -always add items in correct, sorted spot enqueue(10) 5 8 10 11 front (head) back (tail) -dequeue will return smallest item O(1) -what is the cost of enqueue? -we will study a more advanced priority queue later 11

today 12

-trees -terminology -binary trees -traversing a tree -EXAMPLE: expression trees -DOT format 13

trees 14

-trees are a linked data structure with a hierarchical formation -recall that a linked list has a reference to a next (and sometimes previous) node a b c -trees can have multiple links, called branches there are multiple directions you can take at any given node b a c 15

-trees have a hierarchical structure -meaning, any node is a subtree of some larger tree -except the very top node! -in CS, trees are usually represented with the root at the top -trees are recursive in nature -any given node is itself a tree -a tree consists of: -a data element - and more subtrees 16

-there is a strict parent-to-child relationship among nodes -links only go from parent to child -not from child to parent -not from sibling to sibling -every node has exactly one parent, except for the root, which has none a -there is exactly one path from the root to any other node b c d 17

terminology 18

-root node: the single node in a tree that has no parents -parent: a node s parent has a direct reference to it -nodes have AT MOST one parent -child: a node B is a child of node A if A has a direct reference to B a -sibling: two nodes are siblings if they have the same parent b c d 19

-leaf node: a node with no children -inner node: a node with at least one child -depth: the number of ancestors a node has -ie. how many steps to the root -children are exactly one level deeper than their parents -a root node has depth 0 -height: the depth of a tree s deepest leaf node 20

root parent nodes children Leaf nodes 21

a b c d e subtree rooted at node c (leaf nodes are trees too!) f g h i subtree rooted at node d 22

example 23

The root is. v4 The height is. The parent of v3 is. The depth of v3 is. v2 v6 The children of v6 are. v1 v3 v5 v7 v8 The ancestors of v1 are. The descendants of v6 are. v9 The leaves are. Every node other than is the root of a subtree. 24

binary trees 25

-binary trees are a special case of a tree in which a node can have AT MOST two children -these nodes are designated left and right -in this class we will mostly concentrate on binary trees what should the implementation of a binary tree look like? what about a binary tree node? 26

-each node has two reference variables -one for each of the two children -if there is no child, the reference is set to null d d f c f c a b a b g g 27

class BinaryNode<E> { E data; BinaryNode left; BinaryNode right; } -what are the values of left and right for a leaf node? -this is the just the Node class! -the BinaryTree class would contain what? 28

traversing a tree 29

-traversing a linked list is simple -there is only one way to go! b a c -how do we traverse a binary tree if we want to visit every node? -eg. we want to print out the data at every node -how do we decide which direction to take at each node? d f e 30

depth-first traversal -to visit every node, go both directions at each node -trees are recursive in nature a -start at root, recursively traverse the left subtree, then the right subtree b c -if the subtree is null, stop (return) d e f 31

public static void DFT(BinaryNode N) { if(n == null) return; System.out.println(N.data); DFT(N.left); a } DFT(N.right); b c what does this print out? d e f 32

traversal orders -pre-order: use the node before traversing its children -in-order: traverse left child, use node, traverse right child -post-order: use node after traversing both children 33

-pre-order: use N // eg. print N DFT(N.left); DFT(N.right); a -in-order: DFT(N.left); use N // eg. print N DFT(N.right); d b e c -post-order: DFT(N.left); DFT(N.right); use N // eg. print N f note: Nodes are still traversed in the same order, but used (printed) in a different order 34

EXAMPLE: expression trees 35

+ (3-(15/11))+(7*2 4 ) - * 3 / 7 ^ 15 11 2 4 how can we traverse this tree to evaluate the expression? 36

public static double evaluate(node n) { if(n.isleaf()) return n.value; double leftval = evaluate(n.left); double rightval = evaluate(n.right); } switch(n.operator){ case + : return leftval + rightval; case - : return leftval - rightval; } 37

public static double evaluate(node n) { if(n.isleaf()) return n.value; double leftval = evaluate(n.left); double rightval = evaluate(n.right); switch(n.operator){ case + : return leftval + rightval; case - : return leftval - rightval; } } Node class has these fields and method! 38

DOT format 39

-DOT is a tool for tree (and graph) visualization -it is part of the GraphViz software -http://www.graphviz.org -installed on the CADE machines -DOT is also a file format for trees (and graphs) -we can (and will!) write Java code to read them as input to construct a tree, as well as output them from an existing tree for debugging purposes 40

(simplified) DOT format -the DOT language as many features for specifying the layout of a tree (and graph) -the simplest format looks like this: graph mygraph{ a -- b a -- c c -- g c -- j } 41

DOT tool -the CADE Linux machines have the command-line DOT tool installed dot -Tgif input.dot -o output.gif - -Tgif means create a.gif file as the result - -o means specify the name of the output file 42

next time 43

-reading -chapters 8 and 19 in book -chapter 6 -http://opendatastructures.org/ods-java/ -homework -assignment 7 due Thursday 44