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

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

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

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

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

Chapter 20: Binary Trees

Binary Trees, Binary Search Trees

Data Structures in Java

CE 221 Data Structures and Algorithms

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

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

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

Programming II (CS300)

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

CMSC 341 Lecture 10 Binary Search Trees

Data Structures and Algorithms

CS350: Data Structures Heaps and Priority Queues

COMP : Trees. COMP20012 Trees 219

Trees, Binary Trees, and Binary Search Trees

Data Structures and Algorithms for Engineers

Trees. A tree is a directed graph with the property

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

Binary Search Trees. See Section 11.1 of the text.

Binary Trees. Examples:

CS 350 : Data Structures Binary Search Trees

CSE 214 Computer Science II Heaps and Priority Queues

CMSC 341. Binary Search Trees CMSC 341 BST

Programming II (CS300)

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

CS350: Data Structures Binary Search Trees

Binary Heaps. CSE 373 Data Structures Lecture 11

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

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

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

TREES. Trees - Introduction

Recall: Properties of B-Trees

Programming II (CS300)

Binary Trees

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

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Priority Queues (Heaps)

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

9. Heap : Priority Queue

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CSCI2100B Data Structures Heaps

CMSC 341 Lecture 14: Priority Queues, Heaps

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

Figure 1: A complete binary tree.

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSI33 Data Structures

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

Priority Queues (Heaps)

Why Do We Need Trees?

CMSC 341 Leftist Heaps

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

INF2220: algorithms and data structures Series 1

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Algorithms and Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Exam Data structures DAT036/DAT037/DIT960

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

CMSC 341 Lecture 15 Leftist Heaps

Heaps, Heap Sort, and Priority Queues.

Chapter 5. Binary Trees

Data Structures and Algorithms

Priority Queues Heaps Heapsort

Priority Queues Heaps Heapsort

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

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

CMSC 341 Lecture 15 Leftist Heaps

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

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CSCI2100B Data Structures Trees

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Friday Four Square! 4:15PM, Outside Gates

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

TREES Lecture 12 CS2110 Spring 2018

The Binary Search Tree ADT

Cpt S 122 Data Structures. Data Structures Trees

Data Structures and Algorithms

CompSci 201 Tree Traversals & Heaps

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS102 Binary Search Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Priority Queues. Binary Heaps

Programming Abstractions

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

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CS350: Data Structures Tree Traversal

Administration CSE 326: Data Structures

Binary Search Trees Treesort

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Transcription:

Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill 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 exactly exactly 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! Child: terminal node of directed edge! Binary Tree: a tree in which no node can have more than two children. 2 Tree Traversals: examples Binary Trees: implementation! Preorder: print node value, process left tree, then right + + a * b c * + * d e f g! Postorder: process left tree, then right, then print node value a b c * + d e * f + g * +! Inorder: process left tree, print node value, then process right tree! Structure with a data value, and a pointer to the left subtree and another to the right subtree. struct TreeNode { <type> data; // the data TreeNode *left; // left subtree TreeNode *right; // right subtree ;! Like a linked list, but two next pointers.! There is also a special pointer to the root node of the tree (like head for a list). TreeNode *root; a + b * c + d * e + f * g 3 4

Binary Search Trees Binary Search Trees: operations! A special kind of binary tree, used for efficient searching, insertion, and deletion.! Binary Search Tree property: For every node X in the tree: - All the values in the left subtree are smaller than the value at X. - All the values in the right subtree are larger than the value at X.! Not all binary trees are binary search trees! An inorder traversal of a BST shows the values in! insert(x)! remove(x) (or delete)! isempty() (returns bool) - if the root is NULL! find(x) (returns bool)! findmin() (returns <type>) - Smallest element is found by always taking the left branch.! findmax() (returns <type>) - Largest element is found by always taking the right branch. sorted order 5 6 BST: find(x) BST: find(x)! Pseudocode! Recursive bool find (<type> x, TreeNode t) { Recursive Algorithm:! if we are searching for 15 we are done.! If we are searching for a key < 15, then we should search in the left subtree.! If we are searching for a key > 15, then we should search in the right subtree. 7 if (isempty(t)) return false Base case if (x < value(t)) return find (x, left(t)) if (x > value(t)) return find (x, right(t)) return true // x == value(t) 8

BST: insert(x) BST: insert(x)! Algorithm is similar to find(x)! If x is found, do nothing (no duplicates in tree)! If x is not found, add a new node with x in place of the last empty subtree that was searched. Inserting 13:! Pseudocode! Recursive void insert (<type> x, TreeNode t) { if (isempty(t)) make t s parent point to new TreeNode(x) else if (x < value(t)) insert (x, left(t)) 9 else if (x > value(t)) insert (x, right(t)) //else x == value(t), do nothing, no duplicates 10 Linked List example:! Append x to the end of a singly linked list: - Pass the node pointer by reference - Recursive BST: remove(x)! Algorithm is starts with finding(x)! If x is not found, do nothing! If x is found, remove node carefully. void List::append (double x) { append(x, head); Public function - Must remain a binary search tree (smallers on left, biggers on right). void List::append (double x, Node *& p) { if (p == NULL) { p = new Node(); p->data = x; p->next = NULL; else append (x, p->next); Private recursive function 11 12

! Case 1: Node is a leaf BST: remove(x) - Can be removed without violating BST property! Case 2: Node has one child - Make parent pointer bypass the Node and point to that child BST: remove(x)! Case 3: Node has 2 children - Find minimum node in right subtree --cannot have left subtree, or it s not the minimum - Move original node s left subtree to be the left subtree of this node. - Make parent pointer bypass the original node and point to right subtree Does not matter if the child is the left or right child of deleted node 13 find minimum of 2 s right subtree (3) and move 2 s left subtree (1) to be it s new left. Remove 2 (make 6 point to 5). 14 Binary heap data structure! A binary heap is a special kind of binary tree - has a restricted structure (must be complete) - has an ordering property (parent value is smaller than child values) - NOT a Binary Search Tree!! Used in the following applications Binary Heap: structure property! Complete binary tree: a tree that is completely filled - every level except the last is completely filled. - the bottom level is filled left to right (the leaves are as far left as possible). - Priority queue implementation: supports enqueue and deletemin operations in O(log N) - Heap sort: another O(N log N) sorting algorithm. 15 16

Complete Binary Trees! A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience) Complete Binary Trees Properties! In the array representation: - put root at location 1 - use an int variable (size) to store number of nodes - for a node at position i: - left child at position 2i (if 2i <= size, else i is leaf) - right child at position 2i+1 (if 2i+1 <= size, else i is leaf) - parent is in position floor(i/2) (or use integer division) 17 18 Binary Heap: ordering property! In a heap, if X is a parent of Y, value(x) is less than or equal to value(y). - the minimum value of the heap is always at the root.! First: add a node to tree. Heap: insert(x) - must be placed at next available location, size+1, in order to maintain a complete tree.! Next: maintain the ordering property: - if x is greater than its parent: done - else swap with parent, repeat! Called percolate up or reheap up! preserves ordering property 19 20

Heap: insert(x) Heap: deletemin()! Minimum is at the root, removing it leaves a hole. - The last element in the tree must be relocated.! First: move last element up to the root! Next: maintain the ordering property, start with root: - if both children are greater than the parent: done - otherwise, swap the smaller of the two children with the parent, repeat! Called percolate down or reheap down! preserves ordering property 21! O(log n) 22 Heap: deletemin() Sample Problem Tree Height: Write a member function for the IntBinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. For example, the tree shown below has three levels. 7 3 18 1 13 23 24