Trees and beyond Tutorial #3 CPSC 261. Trees are just an example The next two weeks in labs we are playing with trees – Trees are interesting – Trees.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
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.
Starting at Binary Trees
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary 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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 Trees. Objectives To understand the concept of trees and the standard terminology used to describe them. To appreciate the recursive nature of a tree.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 15.
12 C Data Structures.
UNIT-3 LINKED LIST.
Trees.
External Methods Chapter 15 (continued)
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees Definitions Implementation Traversals K-ary Trees
Review & Lab assignments
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
Presentation transcript:

Trees and beyond Tutorial #3 CPSC 261

Trees are just an example The next two weeks in labs we are playing with trees – Trees are interesting – Trees are not the only interesting data structure – They are a good, first, simple data structure that uses pointers – We could use lists, graphs, etc. – Trees fit the bill perfectly

What is a tree? A directed, acyclic graph, where every node (except the root) has exactly one incoming edge

Diagrammatically

A binary tree No node has more than 2 children

A “complete” binary tree Every node has either 2 or 0 children All leaves are at the same “level”

Node ordering At their most general, trees assume nothing about the order of their nodes Enforcing order provides value – Search trees – Binary search trees – Heaps – B-Trees

Thoughts about trees They grow upside down (the root is at the top) They are used for many purposes – A look-up structure – Sorting – Some of Java’s collection classes are trees TreeSet TreeMap

Trees in C In C, we use structures like this to represent trees struct tree { sometype value; long nchildren; struct tree *children[MAXC]; }

Trees in C Or in the case of binary trees struct tree { sometype value; struct tree *left, *right; } Why no nchildren ?

Traversing Trees in C – pre-order Visiting all the nodes of a tree void visit(struct tree *t) { if (t == NULL) return; // do something with t->value visit(t->left); visit(t->right); }

Traversing Trees in C – in-order Visiting all the nodes of a tree void visit(struct tree *t) { if (t == NULL) return; visit(t->left); // do something with t->value visit(t->right); }

Traversing Trees in C – post-order Visiting all the nodes of a tree void visit(struct tree *t) { if (t == NULL) return; visit(t->left); visit(t->right); // do something with t->value }

Questions about visits Will visit always terminate? – Why? Will it visit every node? – Why? Will it visit any node more than once? – Why?

Making trees What kind of tree does this make? struct tree *makeTree(long levels) { if (levels <= 0) return NULL; struct tree *t = malloc (sizeof *t); t->value = random() % 1000; t->right = makeTree(levels - 1); t->left = makeTree(levels - 1); return t; }

Storing trees in files Can you meaningfully store a value in a file? – Yes, if it is char, int, long, char * Can you meaningfully store a pointer in a file? – Yes, but... – What does it mean when you read it back? long *p; readPtr(infile, swap, &p); printf(“*p is %d\n”, *p); – Stack, heap are (likely) at a different address now than when the pointer was written to the file – *(long *) random() = 23;

Swizzling “the conversion of references based on name or position to direct pointer references” [Wikipedia] – Performed when reading complex objects Unswizzling is the opposite – Performed when writing complex objects

Here’s a tree, draw it Each blue box represents 8 bytes Draw the tree with circles, values and arrows root 37 left (0x1248) right (0x1260) 0x1230 0x1238 0x left (0x1278) right (0x0) 0x1248 0x1250 0x left (0x0) right (0x1290) 0x1260 0x1268 0x left (0x0) right (0x0) 0x1278 0x1280 0x left (0x0) right (0x0) 0x1290 0x1298 0x12a0 0x1230

Circles and arrows

Don’t make assumptions Tree nodes won’t be consecutive in memory “Higher” nodes won’t have lower addresses root 37 left (0x1248) right (0x1260) 0x1230 0x1238 0x left (0x1278) right (0x0) 0x1248 0x1250 0x left (0x0) right (0x1290) 0x1260 0x1268 0x left (0x0) right (0x0) 0x1278 0x1280 0x left (0x0) right (0x0) 0x1290 0x1298 0x12a0 0x1230

A file representation Nodes have ids Pointers have been converted to ids 37 left (2) right (3) 25 left (4) right (0) 52 left (0) right (5) 13 left (0) right (0) 99 left (0) right (0)

Swizzling, how to convert? 37 left (2) right (3) 25 left (4) right (0) 52 left (0) right (5) 13 left (0) right (0) 99 left (0) right (0) left (0x1248) right (0x1260) 0x1230 0x1238 0x left (0x1278) right (0x0) 0x1248 0x1250 0x left (0x0) right (0x1290) 0x1260 0x1268 0x left (0x0) right (0x0) 0x1278 0x1280 0x left (0x0) right (0x0) 0x1290 0x1298 0x12a0

Basic idea - reading Repeat Read an id (x) Allocate space for the node, call its address p Read the fields of *p Remember the mapping x -> p Until there is no more data in the file When reading data, you may need to swap bytes

After reading 1 -> 0xa > 0xa > 0xa > 0xa > 0xa390 root 37 left (2) right (3) 0xa330 0xa338 0xa left (4) right (0) 0xa348 0xa350 0xa left (0) right (5) 0xa360 0xa368 0xa left (0) right (0) 0xa378 0xa380 0xa left (0) right (0) 0xa390 0xa398 0xa3a0 1 1

Basic idea - swizzling For every entry in the table ( x -> p ) if p->left is not 0 new = lookup p->left in table p->left = new if p->right is not 0 new = lookup p->right in table p->right = new root = p from the first entry in the table

After swizzling 1 -> 0xa > 0xa > 0xa > 0xa > 0xa390 root 37 left (0xa348) right (0xa360) 0xa330 0xa338 0xa left (0xa378) right (0x0) 0xa348 0xa350 0xa left (0x0) right (0xa390) 0xa360 0xa368 0xa left (0x0) right (0x0) 0xa378 0xa380 0xa left (0x0) right (0x0) 0xa390 0xa398 0xa3a0 0xa330

Necessary type evil new = lookup p->left in table p->left has type struct tree * table lookup expects an id (a long ) C casts convert between things – When the source and destination types of a cast are the same size (both 64 bits) and both “integer- like” the cast doesn’t modify any of the bits – (long)p->left