CO4301 – Advanced Games Development Week 4 Binary Search Trees

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
CS 171: Introduction to Computer Science II
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Fundamentals of Python: From First Programs Through Data Structures
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Binary Search Trees Chapter 7 Objectives
Sorting With Priority Queue In-place Extra O(N) space
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Trees Chapter 15.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Heaps.
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Trees IT12112 Lecture 05.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Priority Queues and Heaps
Data Structures & Algorithms Priority Queues & HeapSort
Priority Queues.
B-Tree Insertions, Intro to Heaps
Priority Queues.
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
CS Data Structure: Heaps.
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSC 143 Java Trees.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Heaps and priority queues
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Binary Trees, Binary Search Trees
EE 312 Software Design and Implementation I
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CO4301 – Advanced Games Development Week 4 Binary Search Trees Gareth Bellaby

Introduction to Binary Search Trees

Binary Search Remember that we did Binary Search. Binary Search did a reasonably efficient search on an ordered list. A reasonable progression to the algorithm is to consider what would happen if we built the binary property of the search into the ordered list. In effect we will create a binary tree. Order the list as you add new items to it.

Binary Search Remember that we did Binary Search. Binary Search did a reasonably efficient search on an ordered list. A reasonable progression to the algorithm is to consider what would happen if we built the binary property of the search into the ordered list. In effect we will create a binary tree. Order the list as you add new items to it.

Binary Search We will start with an explicit tree, i.e. nodes and arcs Search algorithm: 1. Initialise a pointer ptr to the node containing the root. Set found to false. 2. While ptr is pointing at a node, and found is false do: If the item being sought is less than the value in the node pointed to by ptr Set ptr to the left child If the item being sought is greater than the value in the node pointed to by ptr Else Set found to true

Efficiency Binary tree works by splitting the list into two In effect it eliminates one sub-tree from consideration on each pass If the tree is lopsided then it is less efficient The least efficient tree would be one in which all of the nodes were on one side

Traversal Consider a recursive traversal of the tree. N: visit a node L: visit the left subtree of a node R: visit the right subtree of a node LNR: Inorder traversal NLR: Preorder traversal LRN: Postorder traversal

Arithmetic expression Inorder traversal produces the infix expression Preorder traversal produces the prefix expression Postorder traversal produces the postfix expression

Uses Tree traversal Ordered arrays BSP trees Heaps etc, etc

Binary Search Trees

Binary Search Tree Binary Tree BST is an ordered binary tree Priority queue implemented as a static array

Complete A binary tree is said to be complete if, except for the last level, all the levels of the tree are completely filled, i.e. no gaps in the tree All nodes in the last level are as far left as possible, no gaps to the left of a node in the last level

Complete

Not Complete

Array Representation Can be represented very simply as an array Store them in the array in order they would be visted if the tree were traversed [ 20, 15, 10, 5, 10, 9, 1 ]

Array Representation For every i, 2 <= i <= n, the parent of the ith node is i/2. For every i, 1 <= i <= n/2, the left child of the ith node is 2i. For every i, 1 <= i <= (n - 1)/2, the right child of the ith node is 2i + 1. Subtract 1 if the heap is stored using 0 based index, e.g. C++

Heap

Heap Heap is a complete binary tree… With the added characteristic that the child nodes of each node have an inferior value than the node itself Alternatively, that the child nodes have a greater value than the node itself In other words, they display priority. A phrase for this characteristic is "heap property“ Heap is an efficient implemention of a priority queue.

Heap: max-tree Max-tree. Child nodes of each node have an inferior value than the node itself [ 15, 12, 8, 10, 2, 4, 7 ]

Heap: min-tree Min-tree. Child nodes of each node have an inferior value than the node itself [ 2, 10, 5, 15, 11, 12, 15 ]

Heapify Heapify: Meaning to rectify problems with a heap Check each sub-tree (set of 3 nodes), and if it doesn't display the heap property then simply swap over child and root nodes

Insert Add a new item onto the end of the array Percolate upwards, using swap until the heap property is obtained, i.e. the parent is larger than or equal to the percolating element, (or vice versa depending on how priority is being determined) Some people use the phrase “sift” rather than percolate.

Heap Sort

Heapsort Heapsort is a sorting algorithm that repeatedly finds the largest remaining element and puts it in place. It is efficient O(n log n) Doesn’t have the pathologic case that quicksort does.

Efficiency algorithm best case average case worst case extra memory insertion sort O( n ) O( n2 ) O( 1 ) quicksort O( n log n) mergesort heapsort Heapsort is better for space complexity than mergesort, but matches its worst case Insertion is best for arrays which are almost sorted (heapsort scrambles the array at start and so looses any advantage) Quicksort is typically fastest in the average case, but works badly with almost sorted arrays

Heap Sort overview Represent as an array Create a heap of unsorted data Heapify into a max-heap Root must therefore be the max value in the tree In place algorithm: Until size > 0 Swap root with the last node. Decrement size by 1 Heapify

Heapify operation Examine a single subtree and if it doesn’t display the heap property then simply swap over child and root nodes. For example, max heapify: Root node must be bigger than both child nodes If not, swap the biggest child node with the root node If a change has been made then the tree may now be incorrect. Percolate (sift) up (or down dependent on the sort order)

Heap Sort operation Use the maths operations specified above when you need to find the parent or children Nodes are stored in an array Pattern is such that if you take the lowest root node, then each subsequent smallest element in the array will be the next root node in sequence left to right, and then upwards (until root hit) Or alternatively if you start with the root node then each subsequent higher element in the array will be the next node going left to right, and then downwards (until leaf hit)

Heap Sort Obviously replace all of the references to max and largest with min and smallest if you want to sort descending. There are a number of different ways to specify the algorithm. The heapify operation can be performed recursively or iteratively Plenty of references online or in books