F453 Computing Searches
Binary Trees Not this kind of tree!
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
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
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)
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
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
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: Rules of Deletion
F453 Computing Sorting
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
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: Quick Sort
Quick Sort – In dance Dance-the-quicksort.html
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: Bubble Sort
Quick Sort vs Bubble Sort Tw