集册 Java实例教程 将节点类声明为二叉搜索树的节点

将节点类声明为二叉搜索树的节点

欢马劈雪     最近更新时间:2020-01-02 10:19:05

447
将节点类声明为二叉搜索树的节点
//时 代      J a v a   公   众 号 - nowjava.com 提 供

     


import java.util.Scanner;


/* Declare a node class as a node of a binary search tree */

class Node {

  /* Instance variables */

  /* Left and right nodes of the binary tree */

  Node left, right;

  /* Data for the node */

  int data;


  /* Constructor for the node */

  Node(int newData) {

    /* Initialize the node with the new data */

    data = newData;

    /* Start with both the left and right nodes empty (null) */

    left = null;

    right = null;/**时 代 J a v a 公 众 号 - nowjava.com**/

  }

}


public class Main {


  /* Get the max height of the binary tree, starting at root */

  public static int getHeight(Node root) {

    // Write your code here

    if (root == null) {

      return -1;

    }

    return 1 + Math.max(getHeight(root.left), getHeight(root.right));

  }


  /* Insert a Node onto the binary tree */

  public static Node insert(Node root, int data) {

    /* Is the tree empty? */

    if (root == null) {

      /* Return the new Node */

      return new Node(data);

    } else {

      /* Find the current node */

      Node current;

      /* Is this data less than the current nodes data? */

      if (data <= root.data) {

        /* Try to insert the data on the left node */

        current = insert(root.left, data);

        root.left = current;

      } else {

        /* Try to insert the data on the right node */

        current = insert(root.right, data);

        root.right = current;

      }

      /* return the current node after inserting the data */

      return root;

    }

  }


  public static void main(String[] args) {

    /* Instantiate a Scanner to read standard input */

    Scanner stdIn = new Scanner(System.in);


    /* Read the number of data values */

    int dataValues = stdIn.nextInt();


    /* Start with an empty binary tree */

    Node root = null;


    
展开阅读全文