英文:
Binary tree implementation with OOP in Java
问题
目前我正在使用Java中的面向对象编程处理二叉树,但在面向对象编程的一些方面我感到困惑。现在我正在尝试的目标是手动将节点插入我的树中(例如,在主程序中):root.left(2); root.right(5); root.left.left(10),等等。所有这些都是因为我想在树中尝试基本操作,以这种方式添加元素会更容易查看。
我正在使用3个类:Main、BinaryTree 和 Node。
在Node类中,只有getter和setter方法以及3个私有变量:int integer、Node leftChild、Node rightChild。
我的问题是,我不太理解在哪里添加节点,因为BinaryTree类具有来自Node的对象,而Main类具有来自BinaryTree的对象。如果我尝试从Main对象中添加元素,会出现错误。我有点困惑。
英文:
At the moment im working with binary trees in Java with OOP, but Im confused in some aspects of the OOP. Right now what I am trying to achieve is insert nodes into my tree (manually, ex: (in main) root.left(2); root.right(5), root.left.left(10),etc). All this because I want to try basic operations in a tree and adding elements in this way would make it easier to see.
I am working with 3 classes : Main, BinaryTree and Node
in Node theres only getters and setters and 3 private variables : int integer, Node leftChild, Node rightChild;
My problem is that I dont quite understand where to add nodes because BinaryTree has an object from Nodes and Main has an object from BinaryTree. And if I try to add elements from my object in Main it gives me an error. Im kind of confused.
答案1
得分: 1
将每个节点视为独立的树。
因此,在插入操作中,通过从根节点开始迭代整棵树,直到找到要插入的节点的父节点。
这可能有助于更好地解释它:(https://www.geeksforgeeks.org/insertion-in-a-binary-tree-in-level-order/)
英文:
think of each node as a separate tree.
So in the insert operation by iterating over the tree starting from the root until you find the parent of the node you are inserting.
This might help to explain it better: (https://www.geeksforgeeks.org/insertion-in-a-binary-tree-in-level-order/)
答案2
得分: 1
这是链接:
https://www.geeksforgeeks.org/binary-tree-set-1-introduction/
JAVA实现的逻辑:
class Node
{
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
class BinaryTree
{
// 二叉树的根节点
Node root;
// 构造函数
BinaryTree(int key)
{
root = new Node(key);
}
BinaryTree()
{
root = null;
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*创建根节点*/
tree.root = new Node(1);
/* 在上述语句后,树如下所示
1
/ \
null null */
tree.root.left = new Node(2);
tree.root.right = new Node(3);
/* 2和3成为1的左右子节点
1
/ \
2 3
/ \ / \
null null null null */
tree.root.left.left = new Node(4);
/* 4成为2的左子节点
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}
英文:
Here is the link:
https://www.geeksforgeeks.org/binary-tree-set-1-introduction/
JAVA Implementation of the logic:
class Node
{
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
class BinaryTree
{
// Root of Binary Tree
Node root;
// Constructors
BinaryTree(int key)
{
root = new Node(key);
}
BinaryTree()
{
root = null;
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*create root*/
tree.root = new Node(1);
/* following is the tree after above statement
1
/ \
null null */
tree.root.left = new Node(2);
tree.root.right = new Node(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
null null null null */
tree.root.left.left = new Node(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论