Advertisement
joharido

Untitled

Apr 3rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. class node
  2.     int value;
  3.     Node right;
  4.     Node left;
  5.     int counter;
  6. End class
  7.  
  8. /*This algorithm inserts the elements in the node array voteInformation into an AVL tree and finds the candidate number with the highest number of votes
  9. pre-condition: Every element given in the array has a candidate number as its value, and since it is a node, it has a counter variable initialized to 1 by default.
  10. post-condition: the candidate number of the person with the highest number of votes is returned. */
  11. Algorithm int electionWinner(Node voteInformation[])
  12.     AVLtree tree <- new AVLtree;
  13.     for every node, node in voteinformation up to the length of voteInformation
  14.         if (search(voteInformation[node]) = null)
  15.             tree.insert(voteInformation[node]);
  16.         end IF
  17.         else
  18.             (node.counter)++;
  19.         end ELSE
  20.     end FOR
  21.     findingMax(tree);
  22. END
  23.  
  24. /*This algorithm takes the given tree and finds the node with the biggest counter value
  25. pre-condition: the given tree contains nodes with counter variable.
  26. post-condition: the node with the biggest counter variable is returned.
  27. Algorithm int findingMax(Tree tree)
  28. if(root = null)
  29.     return null;
  30. end IF
  31. node max <- root
  32. for every node, node in the tree up to the size of the tree
  33.     if (node.counter > max.counter)
  34.         max = node;
  35.     end IF 
  36. end FOR
  37. return max
  38. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement