Question: that is 2 code of java package com.mycompany.assignment 3 ; class PlayerNode { private int playerID; private String playerName; private int stamina; private PlayerNode right;
that is code of java
package com.mycompany.assignment;
class PlayerNode
private int playerID;
private String playerName;
private int stamina;
private PlayerNode right;
private PlayerNode left;
PlayerNodeint playerID, String playerName, int stamina
this.playerID playerID;
this.playerName playerName;
this.stamina stamina;
this.right null;
this.left null;
public int getPlayerID
return playerID;
public String getPlayerName
return playerName;
public int getStamina
return stamina;
public PlayerNode getRight
return right;
public PlayerNode getLeft
return left;
public void setPlayerIDint playerID
this.playerID playerID;
public void setPlayerNameString playerName
this.playerName playerName;
public void setStaminaint stamina
this.stamina stamina;
public void setRightPlayerNode right
this.right right;
public void setLeftPlayerNode left
this.left left;
and
package com.mycompany.assignment;
public class GameTree
private PlayerNode root;
public GameTree
this.root null;
Method to add a player to the game tree
public void addPlayerint playerID, String playerName, int stamina
root insertroot playerID, playerName, stamina;
Method to insert a player recursively
private PlayerNode insertPlayerNode node, int playerID, String playerName, int stamina
if node null
return new PlayerNodeplayerID playerName, stamina;
if playerID node.getPlayerID
node.setLeftinsertnodegetLeft playerID, playerName, stamina;
else if playerID node.getPlayerID
node.setRightinsertnodegetRight playerID, playerName, stamina;
return node;
Method to update player's stamina
public void updatePlayerStaminaint playerID, int stamina
updateroot playerID, stamina;
Method to update player's stamina recursively
private void updatePlayerNode node, int playerID, int stamina
if node null
return;
if playerID node.getPlayerID
updatenodegetLeft playerID, stamina;
else if playerID node.getPlayerID
updatenodegetRight playerID, stamina;
else
node.setStaminastamina;
Method to delete a player from the game tree
public void deletePlayerint playerID
root deleteroot playerID;
Method to delete a player recursively
private PlayerNode deletePlayerNode node, int playerID
if node null
return null;
if playerID node.getPlayerID
node.setLeftdeletenodegetLeft playerID;
else if playerID node.getPlayerID
node.setRightdeletenodegetRight playerID;
else
Case : No child or one child
if nodegetLeft null
return node.getRight;
else if nodegetRight null
return node.getLeft;
Case : Two children
node.setPlayerIDminValuenodegetRight;
node.setRightdeletenodegetRight node.getPlayerID;
return node;
Method to find the minimum value in a subtree
private int minValuePlayerNode node
int minValue node.getPlayerID;
while nodegetLeft null
minValue node.getLeftgetPlayerID;
node node.getLeft;
return minValue;
Method to display all players' information
public void displayAllPlayersInfo
displayInOrderroot;
Method to display players' information in order recursively
private void displayInOrderPlayerNode node
if node null
displayInOrdernodegetLeft;
System.out.printfdttsttdn node.getPlayerID node.getPlayerName node.getStamina;
displayInOrdernodegetRight;
Method to display player information by ID
public void displayPlayerByIDint playerID
PlayerNode player searchroot playerID;
if player null
System.out.printfPlayer with ID
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
