二叉树在Java中的面向对象实现

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

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 
         */
    } 
} 

huangapple
  • 本文由 发表于 2020年8月8日 06:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63310041.html
匿名

发表评论

匿名网友

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

确定