Download presentation
Presentation is loading. Please wait.
Published byHelle Møller Modified over 5 years ago
1
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Gareth Bellaby
2
Introduction to Binary Search Trees
3
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.
4
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.
5
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
6
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
7
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
8
Arithmetic expression
Inorder traversal produces the infix expression Preorder traversal produces the prefix expression Postorder traversal produces the postfix expression
9
Uses Tree traversal Ordered arrays BSP trees Heaps etc, etc
10
Binary Search Trees
11
Binary Search Tree Binary Tree BST is an ordered binary tree
Priority queue implemented as a static array
12
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
13
Complete
14
Not Complete
15
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 ]
16
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++
17
Heap
18
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.
19
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 ]
20
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 ]
21
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
22
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.
23
Heap Sort
24
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.
25
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
26
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
27
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)
28
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)
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.