zoukankan      html  css  js  c++  java
  • construct binary tree 构造一棵二叉树

    参考:https://www.geeksforgeeks.org/binary-tree-set-1-introduction/

     

    /* Class containing left and right child of current
    node and key value*/
    class Node
    {
        int key;
        Node left, right;
    
        public Node(int item)
        {
            key = item;
            left = right = null;
        }
    }
    
    // A Java program to introduce Binary Tree
    class BinaryTree
    {
        // Root of Binary Tree
        Node root;
    
        // Constructors
      //注意这里要加constructor
    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 */     //就用这种很不合理的left.left.left定义法 tree.root.left.left = new Node(4); /* 4 becomes left child of 2 1 / 2 3 / / 4 null null null / null null */ } }

     

  • 相关阅读:
    mysql逻辑架构
    delete与truncate的区别
    mycat
    mycat
    MyCat数据库中间件
    mysql主从复制
    docker学习笔记之快速安装
    linux学习笔记之CentOS7系统快速安装
    Redis学习笔记
    双绞线的种类与型号
  • 原文地址:https://www.cnblogs.com/immiao0319/p/14872994.html
Copyright © 2011-2022 走看看