140 likes | 335 Views
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.
E N D
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. • The major advantage of using this type of structure is that the search algorithms are particularly efficient.
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.
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)
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
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
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
Sorting F453 Computing
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.
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
Quick Sort – In dance http://www.c0t0d0s0.org/archives/7410-Dance-the-quicksort.html
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
Quick Sort vs Bubble Sort http://www.youtube.com/watch?v=vxENKlcs2Tw