当前位置: 首页 > 知识库问答 >
问题:

二叉树只在左侧添加节点。如何使其在根节点的左侧和右侧都工作?

昝浩阔
2023-03-14

我最近开始学习数据结构和算法。我正在创建一个二叉树,它只在树的左侧添加节点。我如何创建这样一种方式,即它应该在根节点的两侧添加节点,并如下所示:

         2               
        / \       
       /   \      
      /     \     
     /       \    
     7       5       
    / \     / \   
   /   \   /   \  
   2   6   3   6   

以下是我编写的代码:

public class BinaryTreeOperations {

    BinaryTreeNode root;

    //let's start with an empty binary tree
    public BinaryTreeOperations(){
        root = null;
    }

    //let's initialize our tree with a root node
    public BinaryTreeOperations(BinaryTreeNode rootNode){
        this.root = rootNode;
    }


    public void insert(int data)
    {
        root = insertNode(root, data);
    }




    private BinaryTreeNode insertNode(BinaryTreeNode node, int data){


        //To check if the root is null then create a root node with no children
        if (node == null) {

            System.out.println("inserting root node"+ data+"\n");
            node = new BinaryTreeNode(data);
        }
        else {
            if(node.getRightChild() == null){
                System.out.println("inserting right child :"+data+"\n");
                node.rightChild=insertNode(node.rightChild, data);
            }
            else {
                System.out.println("inserting left child :"+data+"\n");
                node.leftChild = insertNode(node.leftChild, data);
            }
        }
        return node;
    }

    public int countNodes() {
        return countNodes(root);
    }

    private int countNodes(BinaryTreeNode r) {
        if (r == null)
            return 0;
        else
        {
            int count = 1;
            count += countNodes(r.getLeftChild());
            count += countNodes(r.getRightChild());
            return count;
        }
    }
}

主要类别:

public class BinaryTreeMain {
    public static void main(String args[]){

        BinaryTreeOperations binaryTreeOperations = new BinaryTreeOperations();
        binaryTreeOperations.insert(12);
        binaryTreeOperations.insert(17);
        binaryTreeOperations.insert(11);
        binaryTreeOperations.insert(21);
        binaryTreeOperations.insert(27);

        System.out.println("Total number of nodes :" + binaryTreeOperations.countNodes());

    }
}

共有1个答案

岳承悦
2023-03-14

例如,您可以在每个节点上存储额外的信息,比如一个布尔值,它给出了插入的方向,您可以在插入后对其进行切换。它只需要比您已经做的多几行。

 类似资料:
  • 我正在将以下数据作为赋值的一部分读取到二叉树(不是严格的二叉查找树)中: 它们被读取到python、和中的三个列表中,其中第一行具有整数是节点数。接下来的n行是键,左,右。其中左是父级左子级的键是,同样右子级的键是,因此例如第一行是4的键是根,意味着2是4的左子级,意味着5是4的右子级,以此类推,-1代表左右意味着这个键是叶子: 此示例的树结构 问题是正在添加根的左右子节点,但没有添加其中的任何子

  • 问题内容: 任何方法都可以在左右两侧(水平?)获得箱形阴影,而没有任何骇客或图像。我在用: 但它给周围的阴影。 我周围没有边界。 问题答案: 注意: 我建议您在下面查看@Hamish的答案;它不涉及此处描述的解决方案中不完善的“掩盖”。 您可以使用多个框阴影来接近;每一面一个 编辑 在顶部和底部的顶部再添加2个阴影,以遮盖流血的阴影。

  • 问题内容: 以任何方式在左侧和右侧(水平?)上获得盒子阴影,而没有骇客或图像。我在用: 但它给周围的阴影。 我周围没有边界。 问题答案: 注意: 我建议在下面查看@Hamish的答案;它不涉及此处所述解决方案中不完善的“掩盖”。 您可以使用多个框阴影来接近;每一侧一个 编辑 在顶部和底部的顶部再添加2个阴影,以掩盖流血的阴影。

  • 我遇到了这个问题:下面的方法必须返回左侧子节点中的值,或者-1(如果它不存在)。 现在,参数是一个int值,它指示父节点的值。另一个问题是...树不是有序的,只有正整数值。所以,我可以在根中有一个0的值,在左边的子项中有3的值,在右边的子项中有1的值,以此类推...我想不出该怎么解决这件事。我不能将像LinkedList或Stack这样的ADT用于任何目的。二叉树类有一个字段根,类型为node:

  • 我的问题是:如何确定已拆分的旋转矩形几何体的和是拆分该几何体的任意的“左”和“右”边? 对于这个问题,"左"和"右"被定义为从节点到节点"行走"时,按顺序的拆分器的左边和右边。 我创建了此函数,用于将任意几何体(非集合)拆分为两个面——“左”和“右”: 上面的想法在此处链接的笔记本中进行了说明(与上面的链接相同): http://nbviewer.jupyter.org/urls/dl.dropb

  • 问题内容: 这个问题源于-其他解包概述,据我所知,它存在于Python 3.5中(并且没有反向移植到)。具体来说,在“ 缺点 ”部分中,注意以下几点: 虽然原因是一, ,原因是一个。这样做的原因可能会使不熟悉该结构的人感到困惑。 对于第一种情况,确实确实存在: 在第二种情况下,创建了: 不熟悉这个概念,我很困惑。谁能解释这种行为?加星标的表情是否会根据其所在的侧面而有所不同? 问题答案: 当还考虑