1 / 14

Searches

Searches. F453 Computing. Binary Trees. Not this kind of tree!. Binary Trees. 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.

jetta
Download Presentation

Searches

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Searches F453 Computing

  2. Binary Trees Not this kind of tree!

  3. Binary Trees • 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.

  4. Recursion • 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.

  5. Binary Search • In an ordinary binary tree, you have three iterative procedures that can be used: 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. Search • The binary tree holds data in the logical order that it was inserted into the tree. • So… if we were looking for Fred. 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. Andy Neil Tina Fred

  7. Insertion • 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’ 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. Neil Andy Evie Tina Fred

  8. Rules of Deletion • 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

  9. Sorting F453 Computing

  10. Insertion Sort • 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.

  11. Quick Sort • Quick Sort is robust and efficient, using the ‘divide and conquer’ method • quicksort( void *a, int low, int high ) { • intpivot; • /* 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.html

  12. Quick Sort – In dance http://www.c0t0d0s0.org/archives/7410-Dance-the-quicksort.html

  13. Bubble Sort • 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<n;i++) { /* From the first element to the end of the unsorted section */ for(j=1;j<(n-i);j++) { /* If adjacent items are out of order, swap them */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } } } • Excellent resource: http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html

  14. Quick Sort vs Bubble Sort http://www.youtube.com/watch?v=vxENKlcs2Tw

More Related