Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class node
- int value;
- Node right;
- Node left;
- int counter;
- End class
- /*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
- 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.
- post-condition: the candidate number of the person with the highest number of votes is returned. */
- Algorithm int electionWinner(Node voteInformation[])
- AVLtree tree <- new AVLtree;
- for every node, node in voteinformation up to the length of voteInformation
- if (search(voteInformation[node]) = null)
- tree.insert(voteInformation[node]);
- end IF
- else
- (node.counter)++;
- end ELSE
- end FOR
- findingMax(tree);
- END
- /*This algorithm takes the given tree and finds the node with the biggest counter value
- pre-condition: the given tree contains nodes with counter variable.
- post-condition: the node with the biggest counter variable is returned. */
- Algorithm int findingMax(Tree tree)
- if(root = null)
- return null;
- end IF
- node max <- root
- for every node, node in the tree up to the size of the tree
- if (node.counter > max.counter)
- max = node;
- end IF
- end FOR
- return max
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement