! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! 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: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

CMSC 341 Lecture 10 Binary Search Trees

Data Structures in Java

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

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

Programming II (CS300)

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

CE 221 Data Structures and Algorithms

CS350: Data Structures Heaps and Priority Queues

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

Binary Search Trees. See Section 11.1 of the text.

COMP : Trees. COMP20012 Trees 219

Binary Heaps. CSE 373 Data Structures Lecture 11

CS 350 : Data Structures Binary Search Trees

Trees. A tree is a directed graph with the property

Recall: Properties of B-Trees

CS350: Data Structures Binary Search Trees

Trees, Binary Trees, and Binary Search Trees

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

CMSC 341. Binary Search Trees CMSC 341 BST

Binary Trees. Examples:

Data Structures and Algorithms

Programming II (CS300)

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

Binary Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

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

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

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

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CSE 214 Computer Science II Heaps and Priority Queues

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

TREES. Trees - Introduction

CSI33 Data Structures

CMSC 341 Lecture 15 Leftist Heaps

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

Priority Queues Heaps Heapsort

CMSC 341 Leftist Heaps

Why Do We Need Trees?

CMSC 341 Lecture 15 Leftist Heaps

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

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

Algorithms and Data Structures

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

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

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

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

The Binary Search Tree ADT

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary Search Trees Treesort

9. Heap : Priority Queue

Data Structures and Algorithms for Engineers

Priority Queues Heaps Heapsort

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

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

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

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

INF2220: algorithms and data structures Series 1

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

CSCI2100B Data Structures Trees

Programming II (CS300)

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

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

Figure 1: A complete binary tree.

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

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

Chapter 5. Binary Trees

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

Cpt S 122 Data Structures. Data Structures Trees

Data Structures and Algorithms

Course Review. Cpt S 223 Fall 2009

CSCI2100B Data Structures Heaps

Priority Queues (Heaps)

CompSci 201 Tree Traversals & Heaps

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

Trees, Part 1: Unbalanced Trees

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

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

CS350: Data Structures Tree Traversal

Exam Data structures DAT036/DAT037/DIT960

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

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

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

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

CS102 Binary Search Trees

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

TREES Lecture 12 CS2110 Spring 2018

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Priority Queues (Heaps)

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

Binary Trees. Height 1

Friday Four Square! 4:15PM, Outside Gates

Balanced Search Trees. CS 3110 Fall 2010

Transcription:

Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 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! Structure with a data value, and a pointer to the left subtree and another to the right subtree.! 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 a + b * c + d * e + f * g!3 struct TreeNode { int value; // 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;!4

Binary Search Trees! 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 sorted order!5 Binary Search Trees: operations! insert(x)! remove(x) (or delete)! isempty() (returns bool) - if the root is NULL! find(x) (or search, 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.!6 BST: find(x) BST: find(x)! Defined iteratively: bool IntBinaryTree::searchNode(int num) { TreeNode *p = root; 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 while (p) { if (p->value == num) return true; else if (num < p->value) p = p->left; else p = p->right; return false;! Can also be defined recursively!8

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:!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->value = num; nodeptr->left = nodeptr->right = NULL; else if (num < nodeptr->value) insert(nodeptr->left, num); // Search the left branch else if (num > nodeptr->value) insert(nodeptr->right, num); // Search the right branch // else nodept->value == num, do nothing, no duplicates!10 BST: remove(x)! Algorithm starts with finding(x)! If x is not found, do nothing! If x is found, remove node carefully. - Must remain a binary search tree (smallers on left, biggers on right). - The algorithm is described here in the lecture, the code is in the book (and on class website in BinaryTree.zip) in the makedeletion(treenode *&nodeptr) function. BST: remove(x)! Case 1: Node has no right child (or no children) - Make parent pointer bypass the Node and point to the left child! Case 2: Node has no left child - Make parent pointer bypass the Node and point to the right child!11 Does not matter if the child is the left or right child of deleted node!12

BST: remove(x)! Case 3: Node has 2 children - Find minimum node in right subtree this node 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 original node s parent pointer bypass the original node and point to right subtree 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). 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 - Priority queue implementation: supports enqueue and deletemin operations in O(log N) - Heap sort: another O(N log N) sorting algorithm.!13!14 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). Complete Binary Trees! A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)!15!16

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)! There is a heap implementation on the class website in Heap.zip!17 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.!18 Heap: insert(x) Heap: insert(x)! First: add a node to tree. - must be placed at next available location, size+1, in order to maintain a complete tree.! Next: maintain the ordering property: - if x doesn t have a parent: done - 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: deletemin() 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 no children, do nothing. - if one child, swap with parent if it s smaller than the parent. - if both children are greater than the parent: done - otherwise, swap the smaller of the two children with the parent, and repeat on that child.! Called percolate down or reheap down! preserves ordering property!21!22