CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Trees.
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees CSE 373 Data Structures.
Trees.
CSE 326: Data Structures Extra Slides on Nested Lists
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002

Questions... 1.What is a call stack? 2.Could you write a compiler that did not use one? 3.What data structure does a printer queue use?

Sparse Matrices  Sparse matrices –what does this remind us of? –how could we represent it?

Lists of Lists  LISP –programming used in AI, math, functional programing –lists (of lists)  (add (sqrt 16) d) addd  sqrt16 

Other Data Structures for Lists  Doubly Linked List - when useful?  Circular List - when useful? cdef

Why Do We Need Trees?  Lists, Stacks, and Queues represent linear sequences  Data often contain hierarchical relationships that cannot be expressed as a linear ordering –File directories or folders on your computer –Moves in a game –Employee hierarchies in organizations and companies –Family trees –Classification hierarchies (e.g. phylum, family, genus, species)

Tree Jargon  Basic terminology: nodes and edges root subtrees parent children, siblings leaves path ancestors descendants path length A B CD E F Note: Arrows denote directed edges Trees always contain directed edges but arrows are often omitted.

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 and height of tree = ? A B CD E F depth=0, height = 2 depth = 2, height=0

Definition and Tree Trivia Recursive Definition of a Tree: A tree is a set of nodes that is a. an empty set of nodes, or b. has one node called the root from which zero or more trees (subtrees) descend.  A tree with N nodes always has ___ edges  Two nodes in a tree have at most how many paths between them?  Can a non-zero path from node N reach node N again?  Does depth of nodes in a non-zero path increase or decrease?

Definition and Tree Trivia Recursive Definition of a Tree: A tree is a set of nodes that is a. an empty set of nodes, or b. has one node called the root from which zero or more trees (subtrees) descend.  A tree with N nodes always has N-1 edges  Two nodes in a tree have at most one path between them  Can a non-zero path from node N reach node N again? –No! Trees can never have cycles.  Does depth of nodes in a non-zero path increase or decrease? –Depth always increases in a non-zero path

Implementation of Trees  Obvious Pointer-Based Implementation: Node with value and pointers to children –Problem: Do not usually know number of children for a node in advance. How many pointers should we allocate space for?  Better Implementation: 1 st Child/Next Sibling Representation –Each node has 2 pointers: one to its first child and one to next sibling –Can handle arbitrary number of children –Exercise: Draw the representation for this tree… A B CD E F

Example Arithmetic Expression: A + (B * (C / D) ) How would you express this as a tree? Application: Arithmetic Expression Trees

Example Arithmetic Expression: A + (B * (C / D) ) Tree for the above expression: Application: Arithmetic Expression Trees + A * B / C D Used in most compilers No parenthesis need – use tree structure Can speed up calculations e.g. replace / node with C/D if C and D are known Calculate by traversing tree (how?)

Traversing Trees  Preorder: Root, then Children –+ A * B / C D  Postorder: Children, then Root –A B C D / * +  Inorder: Left child, Root, Right child –A + B * C / D + A * B / C D

void print_preorder ( TreeNode * T) { TreeNode * P; if ( T == NULL ) return; else { print_element(T-> Element); P = T -> FirstChild; while (P != NULL) { print_preorder ( P ); P = P -> NextSibling; } } Example Code for Recursive Preorder What is the running time for a tree with N nodes?

void Stack_Preorder (TreeNode * T, Stack S) { if (T == NULL) return; else push(T,S); while (!isempty(S)) { T = pop(S); print_element(T -> Element); if (T -> Right != NULL) push(T -> Right, S); if (T -> Left != NULL) push(T -> Left, S); } Preorder Traversal with a Stack What is the running time for a tree with N nodes?

Alternative: Nested List Implementation of a Tree data a dbc next a  b  c  d 

How To Represent? a i d h b f c g

a i d h b f c g a  bc  d  f  g   hi 

void print_preorder ( Node * T) { Node * P; if ( T == NULL ) return; print_element(T-> data); P = T -> next; while (P != NULL) { if (type(P->data)!= (Node*)) signal error; print_preorder ( P->data ); P = P->next; } Recursive Preorder for Nested List Implementation

Determining Type of a Node class node { public: enum Tag { I, P }; private: union { int i; node * p; }; Tag tag; void check(Tag t){ if (tag!=t) error();} public: Tag get_tag() { return tag; } int & ival() { check(I); return i; } node * & pval() { check(P); return p; }

Creating and Setting Nodes class node {... public: // Creating a new node node(int ii) { i=ii; tag=I; } node(node * pp) { p=pp; tag=P; } // Changing the value in a node void set(int ii) { i=ii; tag=I; } void set(node * pp) { p=pp; tag=P; } };

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?  What is the maximum depth of a binary tree?

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 2 d+1 -1 nodes (a full tree) –So, minimum depth d is: log N  d  log(N+1)-1 or  (log N)  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.

Coming Up  Read Chapter 4  Analysis of Binary Search Tree Operations  AVL, Splay, and Balanced Trees