l Tree: set of nodes and directed edges l Parent: source node of directed edge l Child: terminal node of directed edge

Similar documents
! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

l A set is a collection of objects of the same l {6,9,11,-5} and {11,9,6,-5} are equivalent. l There is no first element, and no successor of 9.

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8.

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8. CS 5301 Spring 2017

l A set is a collection of objects of the same l {6,9,11,-5} and {11,9,6,-5} are equivalent. l There is no first element, and no successor of 9.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Searching, Sorting & Analysis

Functions. 6.1 Modular Programming. 6.2 Defining and Calling Functions. Gaddis: 6.1-5,7-10,13,15-16 and 7.7

Binary Trees, Binary Search Trees

Chapter 20: Binary Trees

Trees. (Trees) Data Structures and Programming Spring / 28

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Data Structures in Java

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Lecture outline Graphics and Interaction Scan Converting Polygons and Lines. Inside or outside a polygon? Scan conversion.

Binary Search Trees. See Section 11.1 of the text.

CMSC 341 Lecture 10 Binary Search Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

Binary Trees. Examples:

Register Allocation. Consider the following assignment statement: x = (a*b)+((c*d)+(e*f)); In posfix notation: ab*cd*ef*++x

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-4,6

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Programming II (CS300)

Trees, Binary Trees, and Binary Search Trees

CS 350 : Data Structures Binary Search Trees

CSI33 Data Structures

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-3,5

Straight-line code (or IPO: Input-Process-Output) If/else & switch. Relational Expressions. Decisions. Sections 4.1-6, , 4.

CS350: Data Structures Binary Search Trees

CS 151. Binary Search Trees. Wednesday, October 10, 12

Trees. A tree is a directed graph with the property

Programming Abstractions

Name CPTR246 Spring '17 (100 total points) Exam 3

CE 221 Data Structures and Algorithms

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Programming II (CS300)

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

A METHOD FOR GRIDLESS ROUTING OF PRINTED CIRCUIT BOARDS. A. C. Finch, K. J. Mackenzie, G. J. Balsdon, G. Symonds

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11

l A program is a set of instructions that the l It must be translated l Variable: portion of memory that stores a value char

COMP : Trees. COMP20012 Trees 219

Binary Trees

index.pdf March 17,

CMSC 341. Binary Search Trees CMSC 341 BST

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

understood as processors that match AST patterns of the source language and translate them into patterns in the target language.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Illustrated

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Why Do We Need Trees?

Trees, Part 1: Unbalanced Trees

Programming II (CS300)

9. Heap : Priority Queue

Shape Analysis with Structural Invariant Checkers

CMSC 341 Leftist Heaps

Chapter 5. Binary Trees

CSE 214 Computer Science II Heaps and Priority Queues

The Binary Search Tree ADT

CSCI2100B Data Structures Trees

Section I B COMPUTER SCIENCE SOLUTION

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Space-Time Trade-offs.

A Comparison of a Second-Order versus a Fourth- Order Laplacian Operator in the Multigrid Algorithm

CMSC 341 Lecture 15 Leftist Heaps

Binary Trees. Height 1

PL/SQL, Embedded SQL. Lecture #14 Autumn, Fall, 2001, LRX

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CMSC 341 Lecture 15 Leftist Heaps

TREES. Trees - Introduction

CS350: Data Structures Tree Traversal

INF2220: algorithms and data structures Series 1

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

As Michi Henning and Steve Vinoski showed 1, calling a remote

Summer Final Exam Review Session August 5, 2009

Succinct Indices for Path Minimum, with Applications to Path Reporting

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Professor: Alvin Chao

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Data Structures And Algorithms

Outerjoins, Constraints, Triggers

Data Structures Brett Bernstein

Data Structures and Algorithms

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Lecture Notes on Binary Search Trees

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Advanced Memory Management

CS 5114: Theory of Algorithms. Sorting. Insertion Sort. Exchange Sorting. Clifford A. Shaffer. Spring 2010

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Avaya Extension to Cellular User Guide Avaya Aura TM Communication Manager Release 5.2.1

Transcription:

Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fa 2016 Ji Seaman 1 Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except root) has exacty exacty one edge coming into it. - Every node can have any number of edges going out of it (zero or more). Parent: source node of directed edge Chid: termina node of directed edge Binary Tree: a tree in which no node can have more than two chidren. 2 Tree Traversas: exampes Binary Trees: impementation Structure with a data vaue, and a pointer to the eft subtree and another to the right subtree. Preorder: print node vaue, process eft tree, then right + + a * b c * + * d e f g Postorder: process eft tree, then right, then print node vaue a b c * + d e * f + g * + Inorder: process eft tree, print node vaue, then process right tree struct TreeNode { int vaue; // the data TreeNode *eft; // eft subtree TreeNode *right; // right subtree ; Like a inked ist, but two next pointers. There is aso a specia pointer to the root node of the tree (ike head for a ist). TreeNode *root; a + b * c + d * e + f * g 3 4

Binary Search Trees Binary Search Trees: operations A specia kind of binary tree, used for efficient searching, insertion, and deetion. Binary Search Tree property: For every node X in the tree: - A the vaues in the eft subtree are smaer than the vaue at X. - A the vaues in the right subtree are arger than the vaue at X. Not a binary trees are binary search trees An inorder traversa of a BST shows the vaues in insert(x) remove(x) (or deete) isempty() (returns boo) - if the root is NULL find(x) findmin() (or search, returns boo) (returns <type>) - Smaest eement is found by aways taking the eft branch. findmax() (returns <type>) - Largest eement is found by aways taking the right branch. sorted order 5 6 BST: find(x) BST: find(x) Defined iterativey: boo IntBinaryTree::searchNode(int num) { TreeNode *p = root; Agorithm: if we are searching for 15 we are done. If we are searching for a key < 15, then we shoud search in the eft subtree. If we are searching for a key > 15, then we shoud search in the right subtree. 7 whie (p) { if (p->vaue == num) return true; ese if (num < p->vaue) p = p->eft; ese p = p->right; return fase; Can aso be defined recursivey 8

BST: insert(x) Agorithm is simiar to find(x) If x is found, do nothing (no dupicates in tree) If x is not found, add a new node with x in pace of the ast empty subtree that was searched. Inserting 13: 9 Recursive function BST: insert(x) root is passed by reference to this function void IntBinaryTree::insert(TreeNode *&nodeptr, int num) { if (nodeptr == NULL) { // Create a new node and store num in it, // making nodeptr point to it nodeptr = new TreeNode; nodeptr->vaue = num; nodeptr->eft = nodeptr->right = NULL; ese if (num < nodeptr->vaue) insert(nodeptr->eft, num); // Search the eft branch ese if (num > nodeptr->vaue) insert(nodeptr->right, num); // Search the right branch // ese nodept->vaue == num, do nothing, no dupicates 10 BST: remove(x) BST: remove(x) Agorithm is starts with finding(x) Case 1: Node is a eaf If x is not found, do nothing If x is found, remove node carefuy. - Must remain a binary search tree (smaers on eft, biggers on right). - The agorithm is described here in the ecture, the code is in the book (and on cass website in BinaryTree.zip) - Can be removed without vioating BST property Case 2: Node has one chid - Make parent pointer bypass the Node and point to that chid Does not matter if the chid is the eft or right chid of deeted node 11 12

BST: remove(x) Case 3: Node has 2 chidren - Find minimum node in right subtree --cannot have eft subtree, or it s not the minimum - Move origina node s eft subtree to be the eft subtree of this node. - Make origina node s parent pointer bypass the origina node and point to right subtree find minimum of 2 s right subtree (3) and move 2 s eft subtree (1) to be it s new eft. Remove 2 (make 6 point to 5). Binary heap data structure A binary heap is a specia kind of binary tree - has a restricted structure (must be compete) - has an ordering property (parent vaue is smaer than chid vaues) - NOT a Binary Search Tree! Used in the foowing appications - Priority queue impementation: supports enqueue and deetemin operations in O(og N) - Heap sort: another O(N og N) sorting agorithm. 13 14 Binary Heap: structure property Compete binary tree: a tree that is competey fied - every eve except the ast is competey fied. - the bottom eve is fied eft to right (the eaves are as far eft as possibe). Compete Binary Trees A compete binary tree can be easiy stored in an array - pace the root in position 1 (for convenience) 15 16

Compete Binary Trees Properties In the array representation: - put root at ocation 1 - use an int variabe (size) to store number of nodes - for a node at position i: - eft chid at position 2i (if 2i <= size, ese i is eaf) - right chid at position 2i+1 (if 2i+1 <= size, ese i is eaf) - parent is in position foor(i/2) (or use integer division) There is a heap impementation on the cass website in Heap.zip 17 Binary Heap: ordering property In a heap, if X is a parent of Y, vaue(x) is ess than or equa to vaue(y). - the minimum vaue of the heap is aways at the root. 18 Heap: insert(x) Heap: insert(x) First: add a node to tree. - must be paced at next avaiabe ocation, size+1, in order to maintain a compete tree. Next: maintain the ordering property: - if x doesn t have a parent: done - if x is greater than its parent: done - ese swap with parent, repeat Caed percoate up or reheap up preserves ordering property 19 20

Heap: deetemin() Heap: deetemin() Minimum is at the root, removing it eaves a hoe. - The ast eement in the tree must be reocated. First: move ast eement up to the root Next: maintain the ordering property, start with root: - if no chidren, do nothing. - if one chid, swap with parent if it s smaer than the parent. - if both chidren are greater than the parent: done - otherwise, swap the smaer of the two chidren with the parent, and repeat on that chid. Caed percoate down or reheap down preserves ordering property 21 22