Garbage Collection: recycling unused memory

Similar documents
TREES. Trees - Introduction

Binary Trees, Binary Search Trees

Binary Trees

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

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

CE 221 Data Structures and Algorithms

Trees. CSE 373 Data Structures

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

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

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

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

Trees. Tree Structure Binary Tree Tree Traversals

[ DATA STRUCTURES ] Fig. (1) : A Tree

Data Structures and Algorithms for Engineers

CS301 - Data Structures Glossary By

Trees, Binary Trees, and Binary Search Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Data Structure. IBPS SO (IT- Officer) Exam 2017

Trees. A tree is a directed graph with the property

March 20/2003 Jayakanth Srinivasan,

COMP : Trees. COMP20012 Trees 219

CSE 230 Intermediate Programming in C and C++ Binary Tree

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Successor/Predecessor Rules in Binary Trees

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

Tree Structures. A hierarchical data structure whose point of entry is the root node

Topic 14. The BinaryTree ADT

INF2220: algorithms and data structures Series 1

Binary Search Trees 1

Stacks, Queues and Hierarchical Collections

Data Structures Question Bank Multiple Choice

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

A set of nodes (or vertices) with a single starting point

CS24 Week 8 Lecture 1

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

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

Binary Trees and Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Algorithms. Deleting from Red-Black Trees B-Trees

Trees. Trees. CSE 2011 Winter 2007

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Cpt S 122 Data Structures. Data Structures Trees

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Lecture 26. Introduction to Trees. Trees

Algorithms. AVL Tree

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

Analysis of Algorithms

CSC148 Week 6. Larry Zhang

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

CSI33 Data Structures

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

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

Why Do We Need Trees?

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

CS 206 Introduction to Computer Science II

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees Fall 2018 Margaret Reid-Miller


CMSC 341. Binary Search Trees CMSC 341 BST

Lecture 6: Analysis of Algorithms (CS )

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

tree nonlinear Examples

CMPT 225. Binary Search Trees

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

TREES Lecture 10 CS2110 Spring2014

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

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

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

Motivation for B-Trees

Associate Professor Dr. Raed Ibraheem Hamed

CSCI2100B Data Structures Trees

Tree traversals and binary trees

Chapter 20: Binary Trees

INF2220: algorithms and data structures Series 1

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Algorithms and Data Structures

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CMPE 160: Introduction to Object Oriented Programming

Binary Trees. Examples:

BBM 201 Data structures

Lecture: Analysis of Algorithms (CS )

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

EE 368. Weeks 5 (Notes)

Binary Trees and Huffman Encoding Binary Search Trees

Transcription:

Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1

Backtracking finding a path through a maze is an example of a technique called backtracking if we have several choices, and we are not sure which one will work, we try one of them, and look at the result if the result is not favorable, we try the next choice this is done recursively, because we have more choices once we have made the first choice this can be useful in some games, such as chess or go however, exhaustively searching all possible moves is impractical instead, the search goes up to a point, estimates how good the play is at that point, and reports that in a two-player game, have to consider my best move in response to my opponent's best move -- my opponent's best move is the one that gives the lowest score, my best move is the one that gives the highest score 2

Garbage Collection: recycling unused memory garbage collection means finding the parts of memory that are not reachable by the program: start with the system stack and all the global variables follow each of the pointers, marking each location as visited follow each reference in each of the objects visited at the end, any location that has not been visited, is free and can be reused we need to mark each of the memory areas as being in use or not and also whether we ve visited it before, otherwise circular structures will be hard to garbage collect 3

Garbage Collection: recycling unused memory garbage collection means finding the parts of memory that are not reachable by the program: start with the system stack and all the global variables follow each of the pointers, marking each location as visited follow each reference in each of the objects visited at the end, any location that has not been visited, is free and can be reused we need to mark each of the memory areas as being in use or not and also whether we ve visited it before, otherwise circular structures will be hard to garbage collect 4

trees imagine having to store, in an organized fashion, all the descendants of a given person as in this family tree: image by Derrick Coetzee, in entry family tree in Wikipedia 5

Trees in Computer Science trees can hold any kind of data, even numbers: image by Derrick Coetzee, in entry Tree (data structure) in Wikipedia a node in a tree is similar to a node in a linked list, except: a linked list node has a reference to zero or one link nodes, whereas a tree node has a reference to zero or more other tree nodes later we will consider how trees are implemented 6

Tree Properties a tree has one root node, from which all other nodes can be reached by following links each node in a tree, except the root node, has exactly one parent each node in a tree can have zero or more children the other children of a node's parent are the node's siblings nodes are in a hierarchical relationship: node X is an ancestor of another node Y, or node X is a descendant of another node Y, or node X is on a different branch than node Y nodes without children are leaf nodes nodes with children and a parent are interior nodes 7

Tree Properties Exercise in-class exercise (everyone together): identify the root node image by Derrick Coetzee, in entry Tree (data structure) in Wikipedia interior nodes leaf nodes parent of node 6 children of node 6 ancestors of node 9 descendants of node 7 8

More Tree Definitions a subtree is a node with all its descendants the level of a node is the number of its ancestors (including itself), so e.g. the root is always at level 1: the children of the root are at level 2 their children are at level 3 the children of a node at level n are at level n+1 sometimes the word depth is used instead of level sometimes the level or depth does not include the node itself, in which case the root is at level 0 the height of a tree is the maximum depth of any node in the tree this may also be called the depth of the tree in a binary tree each node has at most two children likewise, in a ternary tree each node has at most three children 9

types of binary trees in a balanced binary tree the height of each subtree of every node differs by at most one (this is a recursive definition) there are also other definitions of balanced binary trees in an expression tree, each internal node contains an operator, and each leaf node is an operand (value) subtrees are evaluated first, yielding a value. Once all its children have been evaluated, we can evaluate a node in a Huffman tree, each internal node has two children. Each leaf node represents something to encode, such as a letter. The binary string to encode the letter is found by going down the tree from the root to the leaf. Each time we go left contributes a 0 bit to the code, and each time we go right contributes a 1 bit to the code. in a binary search tree each node has a value greater than every node in its left subtree, and less than every node in its right subtree in a full binary tree, each node has either 0 or two children (non-leaf nodes are full) a perfect binary tree of height n has 2 n - 1 nodes, of which 2 n - 1 are leaves a complete binary tree is a perfect binary tree up to the last level. All the leaves at the lowest level are as far to the left as possible 10

expression trees an expression tree is a tree where: every node with children is an operator every leaf node is an operand each operator operates on the values of its children 11

expression tree exercise do a pre-order, in-order, and post-order traversal of this tree 12

binary search trees in-class exercise: how do we know this is a binary search tree? image by Derrick Coetzee and Booyabazooka, in entry Binary search tree" in Wikipedia 13

binary search in binary search trees image by Derrick Coetzee and Booyabazooka, in entry Binary search tree" in Wikipedia binary search (for value x) is now much easier: if the value in the root is x, we are done otherwise, if x < the value in the root, search in the left subtree otherwise, x > the value in the root, so search in the right subtree if the subtree we need to search is empty, x is not in the tree note this is a recursive definition in-class exercise: what is the runtime of this algorithm? 14

binary tree traversals if we want to visit each node in a binary tree, we have a choice of how to do it: preorder traversal: visit the root node, then recursively visit the left subtree, then the right subtree inorder traversal: recursively visit the left subtree, then visit the root node, then recursively visit the right subtree postorder traversal: recursively visit the left subtree, then the right subtree, then the root node what does visit a node mean? could mean printing the value could mean saving the value in a data structure 15

binary tree traversal exercises do pre-order, inorder, and post-order traversals of these trees image by Derrick Coetzee, in entry " Tree (data structure)" in Wikipedia image by Derrick Coetzee and Booyabazooka, in entry " Binary search tree" in Wikipedia 16

binary search tree properties adding, finding (get), or removing a node are all O(depth), which, if the tree is balanced, is O(log n), with n the number of nodes in a sorted array, binary search is always O(log n), inserting is O(n) binary search trees are an efficient way to search data and to sort data, as long as the trees remain balanced an unbalanced binary tree might look like a linked list in-class exercise: what is the runtime of the get method when the tree is not balanced? data can be identified with a unique key in-class exercise: add everyone's name to a search tree (in groups of five-ten people) in-class exercise: is the resulting tree balanced? 17

binary search tree add operation if key is less than the key of the current node, recursively add to the left subtree if key is greater than the key of the current node, recursively add to the right subtree otherwise, add at this node: if there is no current node, create one and return it if there is a current node, replace its contents with the new contents this assumes: the method returns a new root for the new (sub)tree with the desired value inserted the caller of this method knows what to do with the new root in-class exercise: create a new binary tree using as keys the following letters, in the order given: "hello world". These keys each carry the value 1, 2, 3, 4, 5, 6, 7 8, 9, 10, 11 18

binary search tree remove operation find node with the given key if the node is a leaf node, delete it, return null if the node only has a left subtree, return the left subtree if the node only has a right subtree, return the right subtree if the node has both subtrees, must replace it with another node that fits in the slot 19

removing a node that has both subtrees the rightmost node in the left subtree can be put in place of the current node without altering the sorted property likewise, the leftmost node in the right subtree can be put in place of the current node that node might have a left (right) subtree, which can be used in its old position the rightmost node in the left subtree is the inorder predecessor of the current node the leftmost node in the right subtree is the inorder successor of the current node so either can be used 20

node removal exercises in-class exercise (everyone together): delete node 17 from this tree in-class exercise (individually): delete node 14 from the original tree in-class exercise (individually): delete node 31 from the original tree 21

BinaryNode class a singly-linked list node has (at most) one reference to another node a binary tree node has (at most) two references to other nodes for example, see BinaryNode.java both types of nodes also need a reference to the locally stored value (possibly including a key) data fields are item, left, right methods include constructors, accessor methods, mutator methods, tostring test program builds small tree, tests the methods 22

BinaryNode exercises in-class exercise: write a recursive static method to print in post-order a tree, given its root in-class exercise: build the following tree using the methods from class BinaryNode 23