将特定对象添加到二叉搜索树中。

huangapple go评论38阅读模式
英文:

Add Specific Object to a Binary Search Tree

问题

我想将一个名为"player"的对象添加到基于二叉搜索树的数据结构中,根据玩家的得分高低,类似于记分牌的功能,但我不确定如何将玩家添加到二叉树中;我想知道如何编写这个方法,希望这个问题有意义。

这是我的BinaryTree类的一部分:

import java.util.Iterator;

public class BinaryTree implements Iterable<Player> {

    private BinNode root;

    public BinaryTree() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    // TODO: 根据玩家的游戏分数将给定的玩家添加到二叉树中
    public void add(final Player player) {

        if (isEmpty())
            root = new BinNode(player);
        // 这只是一个占位符

    } ...
}

这是玩家类:

public class Player implements Comparable<Player> {

    private String name;
    private int score;
    private static final int MIN_SCORE = 0;

    public Player(final String name, int score) {
        this.name = name;
        if (score < MIN_SCORE)
            this.score = 0;
        else
            this.score = score;
    }

    public Player(final String name) {
        this(name, MIN_SCORE);
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return name + ": " + score;
    }

    // TODO: 根据他们的分数比较玩家对象
    @Override
    public int compareTo(Player other) {

        return score - other.score; // 我认为这是正确方向的一步???
    }
}

这是我的Binary Node类:

public class BinNode {

    private Player player;
    private BinNode left, right;

    public BinNode() {
        player = null;
        left = right = null;
    }

    public BinNode(final Player player) {
        this.player = player;
        left = right = null;
    }

    public Player getPlayer() {
        return player;
    }

    public BinNode getLeft() {
        return left;
    }

    public BinNode getRight() {
        return right;
    }

    public void setPlayer(final Player data) {
        this.player = player;
    }

    public void setLeft(BinNode left) {
        this.left = left;
    }

    public void setRight(BinNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return player.toString();
    }
}

如果需要更多帮助,请随时提问。

英文:

I'm looking to add an object called "player" to a binary search tree based on how high the score of the player is, essentially like a scoreboard, but I'm not entirely certain how to add players to the binary tree; I'm curious how to go about writing the method, so I hope this question makes sense.

Here's a part of my BinaryTree class:

import java.util.Iterator;

public class BinaryTree implements Iterable&lt;Player&gt; {

private BinNode root;

public BinaryTree() {
    root = null;
}

public boolean isEmpty() {
    return root == null;
}

// TODO: add the given player to the binary tree based on the player&#39;s game score
public void add(final Player player) {

    if (isEmpty())
        root = new BinNode(player);
    // this is SUUUPER placeholder

} ...

Here's the player class, :

public class Player implements Comparable&lt;Player&gt; {

private String name;
private int    score;
private static final int MIN_SCORE = 0;

public Player(final String name, int score) {
    this.name = name;
    if (score &lt; MIN_SCORE)
        this.score = 0;
    else
        this.score = score;
}

public Player(final String name) {
    this(name, MIN_SCORE);
}

public String getName() {
    return name;
}

public int getScore() {
    return score;
}

@Override
public String toString() {
    return name + &quot;: &quot; + score;
}

// TODO: compare player objects based on their scores
@Override
public int compareTo(Player other) {

    return score - other.score;// I think this is a step in the right direction???
}

}

Here's my Binary Node class:

public class BinNode {

private Player player;
private BinNode left, right;

public BinNode() {
    player = null;
    left = right = null;
}

public BinNode(final Player player) {
    this.player = player;
    left = right = null;
}

public Player getPlayer() {
    return player;
}

public BinNode getLeft() {
    return left;
}

public BinNode getRight() {
    return right;
}

public void setPlayer(final Player data) {
    this.player = player;
}

public void setLeft(BinNode left) {
    this.left = left;
}

public void setRight(BinNode right) {
    this.right = right;
}

@Override
public String toString() {
    return player.toString();
}

}

答案1

得分: 0

我建议您不要在生产环境中使用自己的树,但如果您只是在学习,那么可以。要将新实体添加到您的树中,您必须找到一个合适的位置。在这段代码中,我们通过比较玩家的得分来寻找位置。

public void add(final Player player){
	if (isEmpty()){
		root = new BinNode(player);
	}
	else{
		BinNode node = root;
		while (true){
			if (player.getScore() <= node.getPlayer().getScore()){
				if (node.getLeft() == null){
					node.setLeft(new BinNode(player));
					return;
				}
				else{
					node = node.getLeft();
				}
			}
			else{
				if (node.getRight() == null){
					node.setRight(new BinNode(player));
					return;
				}
				else {
					node = node.getRight();
				}
			}
		}
	}
}
英文:

I recommend you not to use your own trees in production, but if you are just learn, then that's fine. To add a new entity to your tree, you have to find a place for it. In this code we search for it by comparing scores of players.

public void add(final Player player){
	if (isEmpty()){
		root = new BinNode(player);
    }
    else{
        BinNode node = root;
        while (true){
            if (player.getScore() &lt;= node.getPlayer().getScore()){
                if (node.getLeft() == null){
                    node.getLeft() = new BinNode(player);
                    return;
                }
                else{
                    node = node.getLeft();
                }
            }
            else{
                if (node.getRight() == null){
                    node.getRight() = new BinNode(player);
                    return;
                }
                else {
                    node = node.getRight();
                }
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年7月23日 02:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定