3540.Pseudo code.1

advertisement
Emanuel Walker
Project 4 Pseudo code for Tree Classs
1. Tree ( )
Set nodeNumber to zero
2. Find (String key)
Initialize current node to be the root node
Set the integer nodesVisited to one
While current abbreviation does not equal key
If key is less than current abbreviation
Add one to nodesVisited
Set current to current’s leftChild
else
Add 1 to nodesVisited
Set current to current’s rightChild
end if
If current is null
Print message stating key not found and the number of nodes visited
end if
end while
Print message stating key found and number of nodes visited, when loop is exited
Catch NullPointerException
3. Insert (State object)
Initialize newNode to new Node(object)
If root is null
Setr root to newNode
else
Set current to root
initialize parent
While (true)
Set parent to current
if newNode abbreviation is less than current abbreviation
Set current to current’s leftChild
If current is null
Set parent’s leftChild to newNode
Return and exit while loop
end if
else
Set current to current’s rightChild
If current is null
Set parent.rightChild to newNode
return
end if
Emanuel Walker
End if
end while
Call nodeNum(0,root)
4. getSuccessor(Node delNode)
Set successorParent to delNode;
Set successor to delNode
Set current to delNode.rightChild;
while current isn’t null
Set successorParent to successor
Set successor to current
Set current to current’s leftChild
end while
if successor is not the same as delNode’s rightChild
Set successorParent’s leftChild to successor’s rightChild
Set successor’s rightChild to delNode’s rightChild
end if
return successor
5. delete(String abbrevKey)
Set current to root
Set parent to root
Set isLeftChild to true
While current’s abbreviation is not equal to abbrevKey
Set parent to current
If abbrevKey is less than current’s abbreviation
Set isLeftChild to true
Set current to current’s leftChild
else
Set isLeftChild to false
Set current to current’s rightChild
End if
If current is null
Print message stating abbrevKey is not found in binary tree
end if
end while
If current’s leftChild is null and current’s rightChild is null
if current is root
Set root to null
else if isLeftChild is true
Set parent’s leftChild to null
else
Set parent’s rightChild to null
end if
else if current’s rightChild is null
If current is root
Set root to current’s leftChild
else if isLeftChild is true
Emanuel Walker
Set parent’s leftChild to current’s leftChild
else
Set parent’s rightChild to current’s leftChild
else if current’s leftChild is null
If current is root
Set root to current’s rightChild
else if isLeftChild is true
Pass current’s rightChild to parent’s leftChild
else
Set parent’s rightChild to current’s rightChild
end if
else
Set Node successor to getSuccessor(current)
If current is the root
Set root to successor
else if isLeftChild is true
Set parent’s leftChild to successor
else
Set parent’s rightChild to successor
Set successor’s leftChild to current’s leftChild
end if
end else
Print message indicating that the node was deleted from the tree
Call nodeNum(0, root) method
catch (NullPointerException ex)
7. printIterativePreOrder(int stackCapacity)
Set node to root
Set stack to new NodeStack(stackCapacity)
Reset Visited (root)
while (true)
if node is not null
if nod has not been visited
Call displayNode() to print the node
Set the nodes visited to true
end if
Push the node onto the top of the stack
node = node.leftChild;
end if
else
if stack is Empty( )
Return and exit while loop
Set node to the node on the top of the stack by calling the pop( ) method
Set node to node’s rightChild
end else
end while
6. printIterativeRNL(int stackCapacity)
Emanuel Walker
Set node to root
Set NodeStack stack to new NodeStack(stackCapacity)
while (true)
if node is not null
Push node onto stack
Set node to rightChild
end if
Else
if stack is Empty
Return and exit while loop
End if
Set node to the node on top of the stack by popping the stack
Print node
Set node equal to leftChild
end else
end while
7. printRecursivePreOrder(Node localRoot)
if localRoot is not null
Call display( ) method to print local root
Call printRecursivePreOrder(localRoot’s leftChild)
Call printRecursivePreOrder(localRoot’s rightChild)
end if
8. resetVisited(Node localRoot)
If localRoot is not null
Set localRoot’s visited to false
Call resetVisited with(localRoot’s leftChild) as a parameter
Call resetVisited with (localRoot.rightChild) as a parameter
end if
9. printRecursiveRNL(Node localRoot)
If localRoot is not null
Call printRecursiveRNL with parameters (localRoot’s rightChild)
Call localRoot’s displayNode() method
Call printRecursiveRNL with parameters (localRoot.leftChild)
end if
10. printRecursiveInOrder(Node localRoot)
If localRoot is not null
Call printRecursiveInOrder with parameters (localRoot’s leftChild)
Call localRoot’s displayNode() method to print localRoot’s attributes
Call printRecursiveInOrder with parameters, (localRoot.rightChild)
end if
11. nodeNum(int nodeID, Node rootNode)
Emanuel Walker
If the rootNode’s abbreviation is equal to root’s abbreviation
Set rootNode’s nodeNum to nodeID
If rootNode’s leftChild is not null
Set the rootNode’s leftChild’s nodeNum to (two times nodeID) plus one
Call nodeNum method with parameters(nodeNum of rootNode‘s leftChild and rootNode’s
leftChild
end if
If the rootNode’s rightChild is not null
Set nodeNum of rootNode’s rightChild to (two times nodeID) plus 2
Call nodeNum with the following parameters (rootNode.rightChild.nodeNum,
rootNode.rightChild)
end if
12. getHeader()
Set formatString to "%-9s %2$10s %3$17s%n" in order to format a 3 column header.
Print out the Node#,"State Name, and Population of the node
Download