Binary TreesCS-2303, C-Term 20101 Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.

Slides:



Advertisements
Similar presentations
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Introduction to C Programming CE Lecture 23 Binary Trees.
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 ),
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
David Stotts Computer Science Department UNC Chapel Hill.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Non Linear Data Structure
Trees Chapter 15.
COMP 53 – Week Fourteen Trees.
Binary Search Tree (BST)
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
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.
Programming Assignment #4 Binary Trees in C++
Find in a linked list? first last 7  4  3  8 NULL
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Chapter 20: Binary Trees.
1 Lecture 13 CS2013.
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Binary TreesCS-2303, C-Term Definitions Linked List A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Singly- or doubly-linked Stack, queue, circular list Tree A data structure in which each element is dynamically allocated and in which each element has more than one potential successor Defines a partial order

Binary TreesCS-2303, C-Term Binary Tree A linked list but with two links per item struct treeItem { type payload; treeItem *left; treeItem *right; }; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Binary TreesCS-2303, C-Term Binary Tree (continued) Binary tree needs a root struct treeItem { type payload; treeItem *left; treeItem *right; }; struct treeItem *root; Binary trees often drawn with root at top! Unlike ordinary trees in the forest More like the root systems of a tree

Binary TreesCS-2303, C-Term Definitions (continued) Binary Tree A tree in which each element has two potential successors Subtree The set of nodes that are successors to a specific node, either directly or indirectly Root of a tree The node of the tree that is not the successor to any other node, all other nodes are (directly or indirectly) successors to it See Deitel & Deitel, §12.7 K & R, §6.5

Binary TreesCS-2303, C-Term Binary Tree struct treeItem { type payload; treeItem *left; treeItem *right; }; struct treeItem *root; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Binary TreesCS-2303, C-Term Purpose of a Tree (Potentially) a very large data structure Capable of storing very many items In an orderly way Need to find items by value I.e., need to search through the data structure to see if it contains an item with the value we want Need to add new items If value is not already in the tree, add a new item … …so that it can be easily found in future Why not use a linked list?

Binary TreesCS-2303, C-Term Searching and Adding to a Binary Tree Look recursively down sequence of branches until either –Desired node is found; or –Null branch is encountered Replace with ptr to new item Decide which branch to follow based on payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Binary TreesCS-2303, C-Term Example — Searching a Tree typedef struct _treeItem { char *word;// part of payload int count;// part of payload _treeItem *left, *right; } treeItem; treeItem *findItem(treeItem *p, char *w) { if (p == NULL) return NULL;// item not found int c = strcmp(w, p->word); if (c == 0) return p; else if (c left, w); else return findItem(p->right, w); }

Binary TreesCS-2303, C-Term Why do this? Example — Adding an Item treeItem *addItem(treeItem *p, char *w) { if (p == NULL){ p = malloc(sizeof(treeItem)); char *c = malloc(strlen(w)+1); p->word = strcpy(c, w); p->count = 1; p->left = p->right = NULL; return p; }; int c = strcmp(w, p->word); if (c == 0) p->count++; else if (c left = addItem(p->left, w); else p->right = addItem(p->right, w); return p; }

Binary TreesCS-2303, C-Term Binary Tree Question:– how many calls to addItem for a tree with 10 6 nodes? –Assume balanced –I.e., approx same number of nodes on each subtree leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Binary TreesCS-2303, C-Term Answer Approximately 20 calls to addItem Note:– –2 10 = 1024  10 3 –Therefore 10 6  2 20 –Therefore it takes approximately 20 two-way branches to cover 10 6 items! How many comparisons would it take to search a linked list of 10 6 items?

Binary TreesCS-2303, C-Term Observation Problems like this occur in real life all the time Need to maintain a lot of data Usually random Need to search through it quickly Need to add (or delete) items dynamically Need to sort “on the fly” I.e., as you are adding and/or deleting items

Binary TreesCS-2303, C-Term Questions?

Binary TreesCS-2303, C-Term Binary Trees (continued) Binary tree does not need to be “balanced” i.e., with approximate same # of nodes hanging from right or left However, it often helps with performance Multiply-branched trees Like binary trees, but with more than two links per node

Binary TreesCS-2303, C-Term Binary Trees (continued) Binary tree does not need to be “balanced” i.e., with approximate same # of nodes hanging from right or left However, it helps with performance Time to reach a leaf node is O(log 2 n), where n is number of nodes in tree Multiply-branched trees Like binary trees, but with more than two links per node “Big-O” notation:– means “order of”

Binary TreesCS-2303, C-Term Order of Traversing Binary Trees In-order Traverse left sub-tree (in-order) Visit node itself Traverse right sub-tree (in-order) Pre-order Visit node first Traverse left sub-tree Traverse right sub-tree Post-order Traverse left sub-tree Traverse right sub-tree Visit node last

Binary TreesCS-2303, C-Term Question Suppose we wish to print out the strings stored in the tree of the previous example in alphabetical order? What traversal order of the tree should we use?

Binary TreesCS-2303, C-Term Another Example of Binary Tree x = (a.real*b.imag - b.real*a.imag) / sqrt(a.real*b.real – a.imag*b.imag) = x/ sqrt - **.. arealbimag.. brealaimag - …

Binary TreesCS-2303, C-Term Question What kind of traversal order is required for this expression? In-order? Pre-order? Post-order?

Binary TreesCS-2303, C-Term Binary Trees in Compilers Used to represent the structure of the compiled program Optimizations Common sub-expression detection Code simplification Loop unrolling Parallelization Reductions in strength – e.g., substituting additions for multiplications, etc. Many others Note: Deitel & Deitel, Ch 12 exercises, contain a series on building a compiler

Binary TreesCS-2303, C-Term Questions?

Binary TreesCS-2303, C-Term “Big O” notation New Topic

Binary TreesCS-2303, C-Term Linked Lists Again Linear data structure Easy to grow and shrink Easy to add and delete items Time to search for an item – O(n) I.e., proportional to n, the number of items in the list

Binary TreesCS-2303, C-Term Binary Trees Again Non-linear data structure Easy to grow and shrink Easy to add and delete items Time to search for an item – O(log n) I.e., proportional to log of number of items in the list

Binary TreesCS-2303, C-Term Definition — Big-O “Of the order of …” A characterization of the number of operations in an algorithm in terms of a mathematical function of the number of data items involved O(n) means that the number of operations to complete the algorithm is proportional to n E.g., searching a list with n items requires, on average, n/2 comparisons with payloads

Binary TreesCS-2303, C-Term Big-O (continued) O(n): proportional to n – i.e., linear O(n 2 ): proportional to n 2 – i.e., quadratic O(k n ) – proportional to k n – i.e., exponential … O(log n) – proportional to log n – i.e., sublinear O(n log n) Worse than O(n), better than O(n 2 ) O(1) – independent of n; i.e., constant

Binary TreesCS-2303, C-Term Anecdote & Questions:– In the design of electronic adders, what is the order of the carry-propagation? What is the order of floating point divide? What is the order of floating point square root? What program have we studied in this course that is O(2 n )? i.e., exponential?

Binary TreesCS-2303, C-Term Questions on Big-O?