Download presentation
Presentation is loading. Please wait.
Published byAdelia Short Modified over 9 years ago
1
F453 Computing Searches
2
Binary Trees Not this kind of tree!
3
A binary tree is made up of: A Root Node Parent Nodes Child Nodes (called ‘left’ and ‘right’) These binary trees are used to define Binary Searches and Binary Heaps. The major advantage of using this type of structure is that the search algorithms are particularly efficient. Binary Trees
4
Look at the Towers of Hanoi as an example of recursion: Start with x number of discs & three pegs (A, B & C) Every disk with the same parity always moves through the same pattern If a disk has odd parity, it moves A->B->C If a disk has even parity, it moves A->C->B Then through a process of iteration, we move the smallest disk that we can that wasn’t shifted in the previous iteration. Recursion
5
In an ordinary binary tree, you have three iterative procedures that can be used: Binary Search Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted sequence Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)
6
The binary tree holds data in the logical order that it was inserted into the tree. So… if we were looking for Fred. Search Basil We start at the root If the root is not equal to the search: We compare the terms – look left for lesser values and right for higher values We iterate through the tree until the value is found. Fred Andy Neil Tina
7
The term insertion is used to add a new term to the binary tree. It begins in a similar way to searching. So… if we were going to add the term ‘Evie’ Insertion Basil We start at the root If the root is not equal to the term: We compare the terms – look left for lesser values and right for higher values We iterate through the tree until a node is found which relates to our new term. Fred Andy Neil Tina Evie
8
Deleting a leaf (a node with no children) - Just delete it –it has no effect on others Deleting a node with one child - Delete it and replace it with its child Deleting a node with two children The node is replaced with its in-order successor (left-most leaf) Confused? Try this animated version: http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html Rules of Deletion
9
F453 Computing Sorting
10
Think of a pack of cards. -Pick up a card from the pack -look at each card in turn -then place it in its correct place. Insertion Sort
11
Quick Sort is robust and efficient, using the ‘divide and conquer’ method quicksort( void *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) { pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } Excellent resource: http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.htmlhttp://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html Quick Sort
12
Quick Sort – In dance http://www.c0t0d0s0.org/archives/7410- Dance-the-quicksort.html
13
Bubble Sort is robust but not as memory efficient #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) /* Pre-condition: a contains n items to be sorted */ { int i, j; /* Make n passes through the array */ for(i=0;i a[j] ) SWAP(a[j-1],a[j]); } } } Excellent resource: http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html Bubble Sort
14
Quick Sort vs Bubble Sort http://www.youtube.com/watch?v=vxENKlcs2 Tw
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.