使用带有关联名称的mm/dd/yyyy日期的Java二进制搜索树
我和BST之间有个问题,似乎无法解决。 我要在我的项目中使用DOB输出get、min、max、floor、天花、rank、迭代器。 由于整数太大,我目前挂断了不接受的日期使用带有关联名称的mm/dd/yyyy日期的Java二进制搜索树,java,date,binary-search-tree,dob,Java,Date,Binary Search Tree,Dob,我和BST之间有个问题,似乎无法解决。 我要在我的项目中使用DOB输出get、min、max、floor、天花、rank、迭代器。 由于整数太大,我目前挂断了不接受的日期 public class StringBinaryTreeSample { node root; public void addNode(int key, String name) { // Create a new Node and initialize it node newNode = new n
public class StringBinaryTreeSample {
node root;
public void addNode(int key, String name) {
// Create a new Node and initialize it
node newNode = new node(key, name);
// If there is no root this becomes root
if (root == null) {
root = newNode;
} else {
// Set root as the Node we will start
// with as we traverse the tree
node focusNode = root;
// Future parent for our new Node
node parent;
while (true) {
// root is the top parent so we start
// there
parent = focusNode;
// Check if the new node should go on
// the left side of the parent node
if (key <= focusNode.key) {
// Switch focus to the left child
focusNode = focusNode.leftChild;
// If the left child has no children
if (focusNode == null) {
// then place the new node on the left of it
parent.leftChild = newNode;
return; // All Done
}
} else { // If we get here put the node on the right
focusNode = focusNode.rightChild;
// If the right child has no children
if (focusNode == null) {
// then place the new node on the right of it
parent.rightChild = newNode;
return; // All Done
}
}
}
}
}
// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth
public void inOrderTraverseTree(node focusNode) {
if (focusNode != null) {
// Traverse the left node
inOrderTraverseTree(focusNode.leftChild);
// Visit the currently focused on node
System.out.println(focusNode);
// Traverse the right node
inOrderTraverseTree(focusNode.rightChild);
}
}
public node findNode(int key) {
// Start at the top of the tree
node focusNode = root;
// While we haven't found the Node
// keep looking
while (focusNode.key != key) {
// If we should search to the left
if (key == focusNode.key) {
// Shift the focus Node to the left child
focusNode = focusNode.leftChild;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.rightChild;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
public static void main(String[] args) {
StringBinaryTreeSample theTree = new StringBinaryTreeSample();
theTree.addNode("8/15/1998", "Mujib");
theTree.addNode("5/13/2005", "Zia");
theTree.addNode("1/13/1952", "Freedom");
theTree.addNode("2/12/1990", "Victory");
theTree.addNode("3/2/1985", "Molly");
theTree.addNode("5/1/2010", "Asad");
theTree.addNode("11/23/1983", "Genny");
theTree.addNode("6/14/1979", "Independent");
// Different ways to traverse binary trees
// theTree.inOrderTraverseTree(theTree.root);
// theTree.preorderTraverseTree(theTree.root);
// theTree.postOrderTraverseTree(theTree.root);
// Find the node with key 75
theTree.inOrderTraverseTree(theTree.root);
System.out.println("11/23/1983");
System.out.println(theTree.findNode(1));
}
public String toString() {
return name + " has the DOB " + key;
/*
* return name + " has the key " + key + "\nLeft Child: " + leftChild +
* "\nRight Child: " + rightChild + "\n";
*/
}
public class node implements Comparable {
int key;
String name;
node leftChild;
node rightChild;
node(int key, String name) {
this.key = key;
this.name = name;
}
private int handleDOB(final node that){
final String [] thisDOB = key.split("/");
final String [] thatDOB = that.key.split("/");
final int thisMonth = Integer.valueOf(thisDOB[0]);
final int thisDay = Integer.valueOf(thisDOB[1]);
final int thisYear = Integer.valueOf(thisDOB[2]);
final int thatMonth = Integer.valueOf(thatDOB[0]);
final int thatDay = Integer.valueOf(thatDOB[1]);
final int thatYear = Integer.valueOf(thatDOB[2]);
if (thisYear < thatYear) {
return -1;
}
if (thisYear > thatYear) {
return +1;
}
if (thisMonth < thatMonth) {
return -1;
}
if (thisMonth > thatMonth) {
return +1;
}
if (thisDay < thatDay) {
return -1;
}
if (thisDay > thatDay) {
return +1;
}
return 0;
}
}
}
公共类StringBinaryTreeSample{
节根;
public void addNode(int键,字符串名称){
//创建一个新节点并初始化它
node newNode=新节点(键、名称);
//如果没有根,则成为根
if(root==null){
根=新节点;
}否则{
//将root设置为我们将启动的节点
//当我们穿过这棵树的时候
节点focusNode=root;
//新节点的未来父节点
节点父节点;
while(true){
//root是顶级父级,因此我们开始
//那里
父节点=焦点节点;
//检查新节点是否应继续运行
//父节点的左侧
如果(关键年份){
返回+1;
}
如果(本月<当月){
返回-1;
}
如果(本月>当月){
返回+1;
}
如果(今天<那天){
返回-1;
}
如果(今天>那天){
返回+1;
}
返回0;
}
}
}
我看到的第一个明显问题是:
theTree.addNode(08/15/1975, "Mujib");
第一个参数不是Java中的日期…您正在执行一个结果为0的除法,因为它是从其原始浮点值0.00027转换为int的,请使用日期对象或整数时间戳(可能更难看,但更实用?)。这不是Java中的日期:
08/15/1975
它是一个数值表达式,尝试将08
除以15
,然后除以1975
。如果它确实有效,则整数除法将产生0
但是出现“整数太大”错误是因为在int
文本前面加上0
表示Java有一个,并且只有数字0-7才是八进制数的合法数字
目前还不清楚如何将日期隐式转换为int
您可能希望切换到使用
日历
对象。您可以解析“08/15/1975”这样的字符串使用SimpleDataFormat
将转换为日历中的。如果您使用的是Java 8,那么您可以选择使用LocalDate
您能更准确地解释您的问题并只提供特定的代码吗?请花时间和精力正确格式化您的代码。您在问题上投入的精力是多少n是你可能期望某人在回答这个问题时付出的努力,在这种情况下,这不会有多大。03/26/1971
是一个数学表达式;“03/26/1971”
将是一个字符串
表示日期。啊,好吧,我对格式表示歉意。老师的要求和示例非常模糊,所以我在互联网上寻找这样做的提示。我没有意识到这是一个除法问题,这是有意义的。我将查看日历,simpledateform在,看看我能想出什么。谢谢你们。我已经研究了int-convert-to-string,但没有找到任何示例代码,也没有找到任何关于如何应用它的信息。我也尝试了char,但不确定如何将其应用于int。基本上,我必须对其进行自定义,以便从一个文本文件中读取类似于的未排序数据“01/01/2001(MM/dd/yyyy,John,以及类似的输出数据的方式01/01/2001(MM/dd/yyyy),John(使用应用于数据的函数)。关于如何让搜索树读取DOB,我几乎被难住了,非常感谢您的任何帮助(如果有可用的话,示例代码将有助于分析,谢谢。