Trees. CSE 373 Data Structures

Similar documents
Why Do We Need Trees?

Binary Trees, Binary Search Trees

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

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

Binary Trees

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

Programming II (CS300)

CE 221 Data Structures and Algorithms

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

Trees. Truong Tuan Anh CSE-HCMUT

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Trees. Trees. CSE 2011 Winter 2007

CSE 214 Computer Science II Introduction to Tree

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

Chapter 20: Binary Trees

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

TREES. Trees - Introduction

Trees. Tree Structure Binary Tree Tree Traversals

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

Data Structures Question Bank Multiple Choice

Garbage Collection: recycling unused memory

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

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

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

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

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

Algorithms and Data Structures

BBM 201 Data structures

CSC148 Week 6. Larry Zhang

CSCI2100B Data Structures Trees

CSI33 Data Structures

CMSC351 - Fall 2014, Homework #2

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

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

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

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

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

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

Topic 14. The BinaryTree ADT

(2,4) Trees. 2/22/2006 (2,4) Trees 1

DATA STRUCTURES USING C

Friday Four Square! 4:15PM, Outside Gates

March 20/2003 Jayakanth Srinivasan,

Trees, Binary Trees, and Binary Search Trees

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

Recursive Data Structures and Grammars

Trees. A tree is a directed graph with the property

Programming II (CS300)

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

Data Structures and Algorithms for Engineers

Data Structures and Algorithms

tree nonlinear Examples

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Analysis of Algorithms

Trees. Eric McCreath

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Cpt S 122 Data Structures. Data Structures Trees

COMP : Trees. COMP20012 Trees 219

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

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Trees and Binary Trees

Trees Algorhyme by Radia Perlman

Data Structures and Algorithms

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

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

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Binary Trees Fall 2018 Margaret Reid-Miller

CSI33 Data Structures

Course Review for Finals. Cpt S 223 Fall 2008

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

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

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

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

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

Tree traversals and binary trees

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

LECTURE 11 TREE TRAVERSALS

INF2220: algorithms and data structures Series 1

Data Structure - Binary Tree 1 -

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

CS301 - Data Structures Glossary By

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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

Course Review. Cpt S 223 Fall 2009

COMP26120: Algorithms and Imperative Programming. Lecture 1 Trees

Lecture 26. Introduction to Trees. Trees

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

CS24 Week 8 Lecture 1

Transcription:

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 or folders Moves in a game Hierarchies in organizations Can build a tree to support fast searching Trees 3

Tree Jargon root nodes and edges (aka vertices and arcs) leaves parent, children, siblings ancestors, descendants subtrees A B C D E F path, path length height, depth Trees 4

Definition and Tree Trivia A tree is a set of nodes,i.e., either it s an empty set of nodes, or it has one node called the root from which zero or more trees (subtrees) descend A tree with N nodes always has N-1 edges (prove it by induction) A node has a single parent Two nodes in a tree have at most one path between them Trees 5

More definitions Leaf (aka external) node: node without children Internal node: a node that is not a leaf Siblings: two nodes with the same parent Trees 6

More Tree Jargon Length of a path = number of edges Depth of a node N = length of path from root to N Height of node N = length of longest path from N to a leaf Depth of tree = depth of deepest node Height of tree = height of root depth=0, height = 2 Trees 7 A B C D E depth = 2, height=0 F depth=1, height =0

Paths Can a non-zero path from node N reach node N again? No. Trees can never have cycles (loops) Does depth (height) of nodes in a nonzero path increase or decrease? Depth always increases in a non-zero path Height always decreases in a non-zero path Trees 8

More jargon.. If there is a path from node u to node v, u is an ancestor of v Yes but path in which direction? Better to say: Recursive definition: u is an ancestor of v if u= v or u is an ancestor of the parent of v Similar definition for descendent Trees 9

ADT Tree The usual (size(), isempty()... Accessor methods root(); error if the tree is empty parent(v);error if v is the root children(v);returns an iterable collection (i.e.,ordered list) of children Queries (isroot() etc ) How about iterators (or positions?) Trees 10

Implementation of Trees (1) One possible pointer-based implementation tree nodes with value and a pointer to each child but how many pointers should we allocate space for? OK if we use a pointer to a collection of children But how should the collection be implemented? (doubly linked list?) Should there be a parent link or not? Trees 11

Implementation of Trees (2) A more flexible pointer-based implementation 1 st Child / Next Sibling List Representation Each node has 2 pointers: one to its first child and one to next sibling Can handle arbitrary number of children Having a parent link is an orthogonal decision Trees 12

Arbitrary Branching A B C D A B C D Nodes of same depth E F Data E F FirstChild Sibling Trees 13

Binary Trees Every node has at most two children Most popular tree in computer science Given N nodes, what is the minimum depth of a binary tree? At depth d, you can have N = 2 d to N = 2 d+1-1 nodes d d+ 1 2 N 2 1 implies dmin = log2n Trees 14

Minimum depth vs node count At depth d, you can have N = 2 d to 2 d+1-1 nodes minimum depth d is O(log N) T(n) = Θ(f(n)) means T(n) = O(f(n)) and f(n) = O(T(n)), i.e. T(n) and f(n) have the same growth rate, so d = Θ(n) d=2 N=2 2 to 2 3-1 (i.e, 4 to 7 nodes) 1 2 3 4 5 6 7 Trees 15

Maximum depth vs node count What is the maximum depth of a binary tree? Degenerate case: Tree is a linked list! Maximum depth = N-1 Goal: Would like to keep depth at around log N to get better performance than linked list for operations like Find Trees 16

A degenerate tree 1 2 3 4 A linked list with high overhead and few redeeming characteristics 5 6 7 Trees 17

Traversing Binary Trees The definitions of the traversals are recursive definitions. For example: Visit the root Visit the left subtree (i.e., visit the tree whose root is the left child) and do this recursively Visit the right subtree (i.e., visit the tree whose root is the right child) and do this recursively Traversal definitions can be extended to general (non-binary) trees Trees 18

Traversing Binary Trees Preorder: Node, then Children (starting with the left) recursively + * + A B C D * + D Inorder: Left child recursively, Node, Right child recursively A + B * C + D A B Postorder: Children recursively, then Node A B + C * D + + C Trees 19