![]() |
|
#1
|
|||
|
|||
Algorithm Help Please!Hey guys got a few questions, ive done the answers but dont know if there correct, so if anyone could add anything or let me know what i can do better much appreciated
Question Develop a non-recursive algorithm to find the largest node in a binary search tree. (No mark will be given to any deficient algorithm.) Your algorithm starts as follows: Algorithm findLargest (rootPtr <node pointer>) Purpose: to find the largest node in a binary search tree Pre: rootPtr is a pointer to the root of the binary search tree; rootPtr is null if the tree is empty. Return: address of the largest node if the tree is not empty; null if the tree is empty. Note: Suppose p is a pointer that is not null, then you can use p->left and p->right to access the subtrees. Answer BEGIN findLargest (rootPtr, p, largestPtr) IF (rootPtr not null) SET rootPtr to p LOOP (p --> right not null) largestPtr = p p = p --> right END LOOP ENDIF RETURN largestPtr END findLargest Question 2 Algorithm searchBST (root <node pointer>, target <key type>) Purpose: This algorithm searches for a node in a binary search tree Pre: root is a pointer to the root of a binary search tree; target is the key of the node to be searched for Return: pointer to the target node if target is found, NULL if not found Begin Set ____________ //0.5 marks while (______________)//1 mark if (target < searchPtr->data.key) ___________________ //0.5 marks else ___________________ //0.5 marks endif endwhile return __________ //0.5 marks End Answer 2 BEGIN searchBST SET searchPtr to root as node pointer WHILE (searchPtr not null AND target not found) IF (target < searchPtr->data.key) SET searchPtr to searchPtr -> left ELSE SET searchPtr to searchPtr -> right ENDIF ENDWHILE RETURN searchPtr.node or null if target not found END searchBST The last question not sure how to do exeactly so if someone could help please Last question Construct the Huffman Code for the following characters. (No mark will be given if any character is coded incorrectly.) Character Frequency A 5% B 10% C 10% D 15% E 30% F 2% G 3% H 20% I 1% J 4% Thank you very much |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| balloon park puzzle algorithm | husny | Computer Software Forum - Games | 0 | 14-Mar-2006 16:29 |
| Problem with sorting algorithm | MM4o | C++ Forum | 3 | 24-Feb-2006 09:40 |
| Time of computation for an algorithm? | cpit | C++ Forum | 1 | 24-Jan-2006 19:35 |
| help.... SLR * algorithm | tay | C Programming Language | 4 | 10-Sep-2004 11:48 |
| algorithm for display complex graph | ctai010 | C++ Forum | 0 | 20-Mar-2004 05:24 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The