CSI33 Data Structures

Similar documents
CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

RBT Operations. The basic algorithm for inserting a node into an RBT is:

DATA STRUCTURE TREES UNIT II

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

mywbut.com Two agent games : alpha beta pruning

CSE 100: RED-BLACK TREES

Topic 23 Red Black Trees

Lecture 14 Instruction Selection: Tree-pattern matching

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

Self-Adjusting Binary Search Trees. Andrei Pârvu

PRIORITY QUEUES AND HEAPS. Slides of Ken Birman, Cornell University

PRIORITY QUEUES AND HEAPS

The Theory Behind the z/architecture Sort Assist Instructions

and 6.855J. Network Simplex Animations

CSI33 Data Structures

A Memory Efficient Anti-Collision Protocol to Identify Memoryless RFID Tags

MITOCW 6. AVL Trees, AVL Sort

Instruction Selection via Tree-Pattern Matching Comp 412

Algorithms for Data Structures: Search for Games. Phillip Smith 27/11/13

Lecture5: Lossless Compression Techniques

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

A Note on Downup Permutations and Increasing Trees DAVID CALLAN. Department of Statistics. Medical Science Center University Ave

UMBC 671 Midterm Exam 19 October 2009

I am not claiming this report is perfect, or that it is the only way to do a high-quality project. It is simply an example of high-quality work.

Stacks. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

An O(1) Time Algorithm for Generating Multiset Permutations

CHAPTER 5 PAPR REDUCTION USING HUFFMAN AND ADAPTIVE HUFFMAN CODES

5 AVL trees: deletion

Solutions to Assignment-2 MOOC-Information Theory

INF September 25, The deadline is postponed to Tuesday, October 3

Module 3 Greedy Strategy

Random Binary Search Trees. EECS 214, Fall 2017

AIMA 3.5. Smarter Search. David Cline

Do You Understand Evolutionary Trees? By T. Ryan Gregory

GENETIC PROGRAMMING. In artificial intelligence, genetic programming (GP) is an evolutionary algorithmbased

Universiteit Leiden Computer Science

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

COMP 2804 solutions Assignment 4

Checkpoint Questions Due Monday, October 7 at 2:15 PM Remaining Questions Due Friday, October 11 at 2:15 PM

Module 3 Greedy Strategy

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

Games and Adversarial Search II

Adversary Search. Ref: Chapter 5

Parallel Randomized Best-First Search

Pin-Permutations and Structure in Permutation Classes

M14/4/COMSC/HP1/ENG/TZ0/XX. Computer science. Paper 1. Friday 16 May 2014 (afternoon) 2 hours 10 minutes INSTRUCTIONS TO CANDIDATES

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

Backtracking. Chapter Introduction

Game Theory and Randomized Algorithms

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

Intermediate Information Structures

Chess Puzzle Mate in N-Moves Solver with Branch and Bound Algorithm

Problem A. Worst Locations

Past questions from the last 6 years of exams for programming 101 with answers.

Adversarial Search 1

CS188: Section Handout 1, Uninformed Search SOLUTIONS

CSE 332: Data Structures and Parallelism Games, Minimax, and Alpha-Beta Pruning. Playing Games. X s Turn. O s Turn. X s Turn.

Game-playing AIs: Games and Adversarial Search FINAL SET (w/ pruning study examples) AIMA

Artificial Intelligence Lecture 3

Problem A. Vera and Outfits

HEAPABLE SEQUENCES AND SUBSEQUENCES

Foundations of Artificial Intelligence

The power behind an intelligent system is knowledge.

A Level Computer Science H446/02 Algorithms and programming. Practice paper - Set 1. Time allowed: 2 hours 30 minutes

Informatica Universiteit van Amsterdam. Performance optimization of Rush Hour board generation. Jelle van Dijk. June 8, Bachelor Informatica

Informatics 2D: Tutorial 1 (Solutions)

The Eighth Annual Student Programming Contest. of the CCSC Southeastern Region. Saturday, November 3, :00 A.M. 12:00 P.M.

Section Marks Agents / 8. Search / 10. Games / 13. Logic / 15. Total / 46

Link State Routing. Brad Karp UCL Computer Science. CS 3035/GZ01 3 rd December 2013

Intelligent Agents & Search Problem Formulation. AIMA, Chapters 2,

V. Adamchik Data Structures. Game Trees. Lecture 1. Apr. 05, Plan: 1. Introduction. 2. Game of NIM. 3. Minimax

From a Ball Game to Incompleteness

MITOCW ocw lec11

TTIC 31230, Fundamentals of Deep Learning David McAllester, April AlphaZero

ARTIFICIAL INTELLIGENCE (CS 370D)

Stack permutations and an order relation for binary trees

EECS 583 Class 7 Classic Code Optimization cont d

Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

Fall 2018 #8 DFS-Strong-Comps. A. Cut 'em all! 1 second, 256 megabytes

Simple Search Algorithms

Binary trees. Application: AVL trees and the golden ratio. The golden ratio. φ 1=1/φ 1. φ =1+

Heapable Sequences and Subseqeuences

CS 473G: Combinatorial Algorithms, Fall 2005 Homework 0. I understand the Homework Instructions and FAQ.

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

Coding for Efficiency

Göttlers Proof of the Collatz Conjecture

depth parallel time width hardware number of gates computational work sequential time Theorem: For all, CRAM AC AC ThC NC L NL sac AC ThC NC sac

COS 226 Algorithms and Data Structures Fall Midterm Exam

COS 226 Algorithms and Data Structures Fall Midterm Exam

16.410/413 Principles of Autonomy and Decision Making

Examination paper for TDT4120 Algorithms and Data Structures

Lecture 14. Questions? Friday, February 10 CS 430 Artificial Intelligence - Lecture 14 1

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

MITOCW R11. Principles of Algorithm Design

CS : Data Structures

Midterm Examination. CSCI 561: Artificial Intelligence

CMPUT 396 Tic-Tac-Toe Game

Transcription:

Department of Mathematics and Computer Science Bronx Community College

Outline Chapter 7: Trees 1 Chapter 7: Trees

Uses Of Trees Chapter 7: Trees Taxonomies animal vertebrate invertebrate fish mammal reptile insect

Uses Of Trees Chapter 7: Trees Hierarchies Board Of Directors CEO COO Administrative Staff... Division Executives Managers...

Uses Of Trees Chapter 7: Trees Efficient Containers of Sequential Data Each node represents a single data item of a collection organized, for efficient access, into a tree: Binary Search Trees Heaps Priority Queues B-Trees, Quad Trees, etc.

Definitions Chapter 7: Trees Definitions I A tree consists of nodes connected by edges. A node can have zero or more child nodes. A child is connected to its parent node by a single edge. Children with the same parent are called siblings. A tree with no nodes or edges is an empty tree. A nonempty tree will have one special node with no parent called the root node. A node with no children is called a leaf.

Definitions Chapter 7: Trees Definitions II All nodes are connected to the root by a path of edges. The depth of a node is the length of its path to the root. The root has depth zero. A level of a tree is a set of nodes which have the same depth. The descendants of a node are nodes whose paths include that node. The ancestors of a node are nodes on its path to the root.

Binary Trees Chapter 7: Trees Binary Trees A tree whose nodes have at most two children is a binary tree. 2 7 6 5 8 4 3

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: how many nodes are in a full binary tree of height h?

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: how many nodes are in a full binary tree of height h? 2 h 1

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: how many leaves are in the full binary tree of height h?

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: how many leaves are in the full binary tree of height h? 2 h 1

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: If there are n nodes in a full binary tree, how many leaves are there?

Binary Trees Chapter 7: Trees Full Binary Trees A full binary tree is one whose levels have every position filled. 2 7 6 3 5 8 4 1 9 2 8 7 3 6 5 Question: If there are n nodes in a full binary tree, how many leaves are there? n 2

Binary Trees Chapter 7: Trees Complete Binary Trees A complete binary tree has every level filled except the bottom, which is filled from left to right. 2 7 6 3 5 8 4 1 9 2

Binary Tree Traversal Binary Trees are Recursive Data Structures A binary tree can be defined as either An empty binary tree (base case), or a node having two binary trees as attributes, a left subtree and a right subtree (recursive case). Using this recursive definition, recursive algorithms can be used to process the nodes of a binary tree.

Binary Tree Traversal Preorder def traverse(tree): if tree is not empty: process data at tree s root traverse(tree s left subtree) traverse(tree s right subtree)

Binary Tree Traversal Inorder def traverse(tree): if tree is not empty: traverse(tree s left subtree) process data at tree s root traverse(tree s right subtree)

Binary Tree Traversal Postorder def traverse(tree): if tree is not empty: traverse(tree s left subtree) traverse(tree s right subtree) process data at tree s root

Binary Tree Representation Of An Expression Abstract Syntax Tree + * * + 4 5 6 2 3

Binary Tree Representation Of An Expression Different Traversals Give The Different Notations + * * + 4 5 6 2 3 Printing in preorder gives Prefix Notation. Printing inorder gives Infix Notation. Printing in Postorder gives Postfix Notation.

Binary Tree Representation Of An Expression Different Traversals Give The Different Notations + * * + 4 5 6 2 3 Printing in preorder gives Prefix Notation. +*+234*56 Printing inorder gives Infix Notation. (2+3)*4+5*6 Printing in Postorder gives Postfix Notation. 23+4*56*+

Binary Tree Representation Of An Expression Evaluation + * * + 4 5 6 2 3 def evaluatetree(tree): if tree s root is an operand: return root data else: # root contains an operator leftvalue = evaluatetree(tree s left subtree) rightvalue = evaluatetree(tree s right subtree) result = operator(leftvalue, rightvalue) return result

Linked Representation Of A Binary Tree A TreeNode Class in Python An empty tree is represented by None. A nonempty tree is defined using a TreeNode as root. The TreeNode class is defined recursively.

Linked Representation Of A Binary Tree Recursive Definition Of A TreeNode Class class TreeNode: def init (self, data = None, left=none, right=none): self.item = data self.left = left # TreeNode or None self.right = right # TreeNode or None Python Examples Using The TreeNode Class TreeNode left item None right

Linked Representation Of A Binary Tree Example 1: Abstract View 2 1 3

Linked Representation Of A Binary Tree Example 1: Abstract View 2 1 3 Example 1: Python Implementation left = TreeNode(1) right = TreeNode(3) root = TreeNode(2, left, right)

Linked Representation Of A Binary Tree Example 1: Python Memory Model left item 2 right left item right left item right None 1 None None 3 None

Linked Representation Of A Binary Tree Example 2: Abstract View 2 7 6 5 8 4 3

Linked Representation Of A Binary Tree Example 2: Abstract View 2 7 6 5 8 4 3 Example 2: Python Implementation root = TreeNode(2, TreeNode(7, None, TreeNode(5, TreeNode(3))), TreeNode(6, TreeNode(8), TreeNode(4)))

Linked Representation Of A Binary Tree Example 2: Python Memory Model left item right 2 left item right left item right None 7 6 left item right left item right left item right 5 None None 8 None None 4 None left item right None 3 None

Array Representation of Binary Trees Abstract View - array representation 2 7 6 5 8 4 3 2 7 6 None 5 8 4 None None 3 0 1 2 3 4 5 6 7 8 9

Array Representation of Binary Trees Implementation 2 7 6 None 5 8 4 None None 3 0 1 2 3 4 5 6 7 8 9 The node at position i has: def left child(i): return 2 * i + 1 def right child(i): return 2 * i + 2 def parent(i): return (i - 1) // 2

Chapter 7: Trees See the handout.