CSCE 2100: Computing Foundations 1 The Tree Data Model

Slides:



Advertisements
Similar presentations
1 A B C
Advertisements

Simplifications of Context-Free Grammars
Mathematical Preliminaries
Variations of the Turing Machine
PDAs Accept Context-Free Languages
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Priority search queues: Loser trees
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
On-line Construction of Suffix Trees Chairman : Prof. R.C.T. Lee Speaker : C. S. Wu ( ) June 10, 2004 Dept. of CSIE National Chi Nan University.
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
Programming Language Concepts
The 5S numbers game..
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 10: Applications of Arrays and the class vector
Abstract Data Types and Algorithms
Data Structures Using C++
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
: 3 00.
5 minutes.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
Converting a Fraction to %
CSE Lecture 17 – Balanced trees
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

CSCE 2100: Computing Foundations 1 The Tree Data Model Tamara Schneider Summer 2013

Trees in Computer Science Data model to represent hierarchical or nested structures family trees charts arithmetic expressions Certain tree types allow for faster search, insertion and deletion of elements

Tree Terminology [1] n1 n4 n3 n2 n6 n5 n1 is called the root node n2 and n3 are children of n1 n4, n5, n6 are children of n2 n4, n5, and n6 are siblings n1 is a parent of n2 and n3 n3, n4, n5, and n6 are leaves, since they do not have any children All other nodes are interior nodes n1 n4 n3 n2 n6 n5

Tree Terminology [2] n1 n4 n3 n2 n6 n5 n2, n3, n4, n5, and n6 are descendants of n1 n1 and n2 are ancestors of n5 n2 is the root of a sub-tree T

Conditions for a tree It has a root. All nodes have a unique parent. Following parents from any node in the tree, we eventually reach the root.

Inductive Definition of Trees Basis: A single node n is a tree. Induction: For a new node r and existing trees T1, T2, ... , Tk, designate r as the root node and make all Ti its children. Subtrees are often drawn as triangles. We do not know the size of each of the sub-trees. They can be as small as a single node. T1 T3 T2 Tk ... r

Height and Depth of a tree The height of the tree is the length of the longest path between any node and the root. n1 n2 n3 1 The depth (level) of a node is the length of the path to the root. n4 n5 n6 2 The height of the tree is 3. n7 n8 The depth or level of node n5 is 2. The depth of the root node is always 0. 3 3

Expression Trees Describe arithmetic expressions Inductively defined A tree can be as little as containing a single operand, e.g. a variable or integer (basis) Trees can be inductively generated by applying the unary operator “-” to it or combing two trees via binary operators (+, - , * , / )

Expression Trees - Example

Tree Data Structures In C we can define a structure, similarly to linked lists. use malloc to allocate memory for each node nodes “float” in memory and are reached via pointers In C++ we can also use classes to represent the individual nodes (and we will for this class) Trees can also be represented by arrays of pointers

Trees as “Array of Pointers” using Classes A tree contains the functions that are executed on a tree It has a pointer to the “access point” of the tree, the root node.

Trees as “Array of Pointers” using Classes Each node contains data Each node contains an array of pointers to its children Each child is represented by a the root of a tree (sub-tree) Since CTree is a friend class, it can access the private data of CNode

Constructor of CNode

Functions of CTree Insert Search Remove creates a new node. navigates through the tree to place the node in the corresponding spot. Search navigates through the tree to the spot where the search value is suspected. returns true is the search was successful, false otherwise. Remove executes a search on the tree. deletes the element if it exists.

Trie (Prefix Tree) - Abstraction Nodes contain “data” and a flag indicating whether a valid word ends at the node abstract representation of a trie he, hers, his, she

TrieNode

TrieNode Constructor

Trie (Prefix Tree) - Data Structure What value for MAX_CHILDREN do you expect? Are arrays of pointers a space efficient choice?

Leftmost-Child-Right-Sibling Abstraction Use a linked list instead of an array. A parent only points to the first of its children children of n1 children of n2 child of n4

Leftmost-Child-Right-Sibling Data Structure

Recursion on Trees Recursion is “natural” on trees, since trees are recursively defined.

Order of Recursion 1, 13, 15 2, 4, 6, 12 14 3 5 7, 9, 11 8 10

Tree Traversal: Preorder List a node the first time it is visited n1, n2, n4, n5, n6, n7, n8, n3 For expression trees: results in prefix ex-pressions, e.g. (a + b) * c (infix) *+abc (prefix) n1 n2 n3 n4 n5 n6 n7 n8

Tree Traversal: Postorder List a node the last time it is visited. n4, n5, n7, n8, n6, n2, n3, n1 For expression trees: results in prefix expressions, e.g. (a + b) * c (infix) ab+c* (postfix) n1 n2 n3 n4 n5 n6 n7 n8

Binary Trees Binary trees can have at most 2 children. Examples: We distinguish between the left and the right child. The distinction between them is important!

All binary Trees With 3 Nodes

Binary Tree Traversal: Inorder List a node after its left child has been listed and before its right child has been listed n4, n2, n6, n5, n7, n1, n3 For expression trees: results in infix expressions n1 n2 n3 n4 n5 n6 n7

Evaluating Expression Trees [1] + left right For interior nodes, m_chOperator contains an arithmetic operator (+,-,*,/) For leaf nodes, m_chOperator contains the character i for integer, and m_nData contains a value

Evaluating Expression Trees [2]

. . . Structural Induction Prove a statement S(T) for a tree T Basis: Prove the statement for a single node Induction: Assume the statement is true for subtrees T1 T2 ... Tk r r1 r2 rk T1 T2 Tk . . .

Structural Induction - Example [1] S(T): T::eval() returns the value of the arithmetic expression represented by T.

Structural Induction - Example [2] Basis: T consists of a single node. m_chOperator has the value ‘i’ and the value (stored in m_nData) is returned.

Structural Induction - Example [3] Induction: If T is not a leaf: v1 and v2 contain the values of the left and right subtrees (by inductive hypothesis). In the switch statement the corresponding operator is applied → correct value returned. ∎

Binary Search Trees Suitable for so-called dictionary operations: insert delete search Binary Search Tree property: All nodes in left subtree of a node x have labels less than the label of x, and all nodes in the right subtree of x have labels greater than the label of x.

Binary Search Tree - Example Is this a valid binary search tree in lexicographic order?

Search Search for element x Check root node If the root is null, return false If x == root->data, return true If x > root->data, search in the right subtree (recursively) If x < root->data, search in the left subtree (recursively)

Example: Search for 7 8 9 5 2 7 10 3

Recursive Search Implementation

Alternate Search Implementation

Insertion Insert element x Check root node If the root is null, create a new root node If x == root->data, then do nothing If x > root->data then insert x into the right subtree (recursively) If x < root->data then insert x into the left subtree (recursively)

Example: Insert 8, 5, 2, 7, 9, 3, 2, 10 8 9 5 2 7 10 3

Deletion Search for element x If x does not exist, there is nothing to delete If x is a leaf, simply delete leaf If x is an interior node Replace by largest element of left subtree OR replace by smallest element of right subtree Deletion is recursive! The node we use to replace the originally deleted node must be deleted recursively! What would happen if we replaced node by the smallest element of the left subtree?

Example: Delete 4 8 9 4 10 2 6 1 3 5 7

Priority Queues The elements of a priority queue have priorities. If an element with a high priority arrives, it passes all the elements with lower priorities. e.g. Scheduling algorithms in operating systems make use of priority queues. Priority queues are often implemented using heaps, a type of partially ordered tree (POT).

Heaps 4 5 2 8 7 3 A node must have a greater value than its children. 17 19 7 3 10 14 A node must have a greater value than its children. A heap is always complete: all levels except the last level are completely filled. Heaps are usually implemented via arrays.

Array Representation of Heaps 19 A[2] A[3] 17 14 A[4] A[5] A[6] A[7] 17 8 10 7 5 2 4 7 3 1 2 3 4 5 6 7 8 9 10 11 12 19 17 14 A[8] A[9] A[10] A[11] A[12] For a node A[i], we find its left child at A[2i] and A[2i+1]. Example: Children of the node A[5] are A[2*5] and A[2*5+1].

Priority Queue Operations: Insert [1] 4 5 2 8 17 19 7 3 10 14 15 Insert into the last level using the first available spot. If the last level is full, create a new level.

Priority Queue Operations: Insert [2] 4 5 2 8 17 19 7 3 15 14 10 Bubble Up: Compare with parent and exchange, if the parent is smaller.

Priority Queue Operations: Insert [2] 4 5 2 8 17 19 7 3 14 15 10 Bubble Up: Compare with parent and exchange, if the parent is smaller.

Priority Queue Operations: Deletemax [1] The element with the highest priority will be served first and therefore, will be removed first. 4 5 2 8 17 19 7 3 10 14

Priority Queue Operations: Deletemax [2] The element with the highest priority will be served first and therefore, will be removed first. 4 5 2 8 17 19 7 3 10 14 The root node must be replaced. We choose the rightmost node of the last level.

Priority Queue Operations: Deletemax [3] Bubble Down: Compare with parent and if one or both of the children are larger, then exchange it with the larger one of the children. 4 5 2 8 17 3 7 10 14

Priority Queue Operations: Deletemax [4] Bubble Down: Compare with parent and if one or both of the children are larger, then exchange it with the larger one of the children. 4 5 2 8 17 3 7 10 14

Priority Queue Operations: Deletemax [5] Bubble Down: Compare with parent and if one or both of the children are larger, then exchange it with the larger one of the children. 4 5 2 8 3 17 7 10 14 What if we swap it with the smaller one of the children?

Priority Queue Operations: Deletemax [6] Bubble Down: Compare with parent and if one or both of the children are larger, then exchange it with the larger one of the children. 4 3 2 8 5 17 7 10 14

Heap Sort Heapify the array: Insert elements one by one into an initially empty MaxHeap. Repeatedly call deletemax: We obtain the elements in a sorted order from largest to smallest. To obtain elements sorted from smallest to largest, we can use a MinHeap instead and repeatedly call deletemin.

HeapSort: Example [1] Sort 2, 1, 3, 4 Insert elements into heap (Heapify) 2 2 3 2 1 1 3 1 2 3 3 4 1 2 4 2 3 2 4 1 1

HeapSort: Example [2] Sort 2, 1, 3, 4 Deletemax 4 4 3 4 1 3 2 3 2 3 2

Summary Heaps Highest priority element in the root. Each node’s children are smaller than the node itself. We have seen “max-heaps”, where the greatest number is in the root. Analogously there are “min-heap”, where the smallest number is in the root. Insertion: Add to end and “bubble-up” Deletemax: Remove root element and “bubble-down” Heaps can be used for sorting (HeapSort)