common code “abstracted out” same code written several times: don’t!

Slides:



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

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Data Structures Using C++1 Chapter 11 Binary Trees.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
CPS 100, Spring Trees: no data structure lovelier?
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Introduction to C Programming CE Lecture 23 Binary Trees.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
Data Structures AVL Trees.
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.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
From doubly-linked lists to binary trees
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
Printing a search tree in order
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Data Structures Binary Trees 1.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Programming Abstractions
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
PFTWBH More examples of recursion and alternatives
UNIT III TREES.
Red Black Trees
What’s the Difference Here?
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Additional Tree Traversal Example #1
Tree.
Algorithms and Data Structures Recursion and Binary Tree
CMSC 341 Introduction to Trees.
Compsci 201 Trees, Tries, Tradeoffs
Binary Search Trees Why this is a useful data structure. Terminology
Quiz Oct
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Trees.
Data Structures and Algorithms
Trees CMSC 202, Version 5/02.
Binary Tree Traversal Methods
Binary Search Trees.
CMSC 341 K-D Trees.
CS-240 Dick Steflik D.J. Foreman
CMSC 341 K-D Trees.
Binary Trees, Binary Search Trees
Trees.
Heap code in C++ template <class eType>
CMSC 341 K-D Trees.
CMSC 341 K-D Trees.
Compsci 201 Binary Trees Stacks+Queues
Jordi Cortadella and Jordi Petit Department of Computer Science
Goals Design decisions Design Insertion
CMSC 341 K-D Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

common code “abstracted out” same code written several times: don’t! why templated nodes? common code “abstracted out” same code written several times: don’t! write a function, call it twice, rewrite code, why? struct STree struct ITree template <class T> { { struct Tree string info; int info; { STree * left; ITree * left; T info; STree * right; ITree * right; Tree * left; }; }; Tree * right; }; Tree<string> * t = new Tree<string>; int numNodes(Tree<string> * t); template <class T> int numNodes(Tree<T> * t); // hard to count?

Tree functions, vocabulary trees have roots, nodes, leaves, branches/edges the depth of a node is the number of edges on the path from the root to the node single node tree? empty tree? height of a node is depth of deepest leaf below what’s good about trees? when are trees used? what are practical examples? files/directories (almost) expression trees search structures

Tree traversals void Preorder(Tree * t) void Inorder(Tree * t) { { if (t != 0) Inorder(t->left); cout << t->info << endl; Inorder(t->right); } void Preorder(Tree * t) { if (t != 0) cout << t->info << endl; Preorder(t->left); Preorder(t->right); } “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard” void Postorder(Tree * t) { if (t != 0) Postorder(t->left); Postorder(t->right); cout << t->info << endl; }

Tree shapes “good” trees are roughly balanced “bad” trees are long, skewed, skinny what is complexity of inserting n nodes into a “good” tree? why? a bad tree? why?

sidebar: solving recurrence T(n) = 2T(n/2) + O(n) T() = 2T(/2) + T(0) = 1 what about 2n? 3n? T(n) = 2[2T(n/4) + n/2] + n = 4 T(n/4) + n + n = 4[2T(n/8) + n/4] + 2n = 8T(n/8) + 3n = ... eureka! = 2k T(n/2k) + kn let 2k = n k = log n, this yields 2log n T(n/2log n) + n(log n) n T(1) + n(log n) O(n log n)

sidebar: why observers? separate simulation from observation in general, separation yields more flexibility plug-in new observers later, or no observers each class should do one thing, not several avoid “kitchen-sink” classes make [it] as simple as possible, but no simpler inheritance facilitates re-use of an interface implement in different ways, use in same way