你将如何实现二叉树?

时间:2011-06-20 14:50:11

标签: java binary-tree

您将如何在下面的代码中实现二叉树?我需要它以id的数字顺序打印出下面的内容。

import java.util.Scanner;

class clubmember {

    public static void main(String[] args) {

        int id;
        String fname, lname;
        Scanner input = new Scanner(System.in);

        System.out.println("ID>");
        id = input.nextInt();

        System.out.println("Fname >");
        fname = input.next();

        System.out.println("lname >");
        lname = input.next();


        Person object1 = new Person(id, fname, lname);

        System.out.println(object1);


    }
}




public class Person {
    private final int id;
    private final String firstName;
    private final String lastName;


    public Person(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return String.valueOf(id) + ": " + firstName + " " + lastName;
    }
}

我实际上被要求以这棵树为例......任何人都可以帮忙吗?

public class BinaryTreeTest {

  public static void main(String[] args) {
    new BinaryTreeTest().run();
  }

  // Node Class
  static class Node {
    Node left;

    Node right;

    int value;

    public Node(int value) {
      this.value = value;
    }
  }

  public void run() {
    // build simple tree 

    Node root = new Node(5);
    System.out.println("Binary Tree Example");
    System.out.println("Building tree with root value of ID " + root.value);
    insert(root, 1);
    insert(root, 8);
    insert(root, 6);
    insert(root, 3);
    insert(root, 9);
    System.out.println("Traversing tree in order");
    printInOrder(root);
    System.out.println("Traversing tree front-to-back from location 7");
    printFrontToBack(root, 7);
  }

  public void insert(Node node, int value) {
    if (value < node.value) {
      if (node.left != null) {
        insert(node.left, value);
      } else {
        System.out.println("  Inserted " + value + " to left of "
            + node.value);
        node.left = new Node(value);
      }
    } else if (value > node.value) {
      if (node.right != null) {
        insert(node.right, value);
      } else {
        System.out.println("  Inserted " + value + " to right of "
            + node.value);
        node.right = new Node(value);
      }
    }
  }

  public void printInOrder(Node node) {
    if (node != null) {
      printInOrder(node.left);
      System.out.println("  Traversed " + node.value);
      printInOrder(node.right);
    }
  }

  /**
   * uses in-order traversal when the origin is less than the node's value
   * 
   * uses reverse-order traversal when the origin is greater than the node's
   * order
   */
  public void printFrontToBack(Node node, int idNumber) {
    if (node == null)
      return;
    if (node.value > idNumber) {
      // print in order
      printFrontToBack(node.left, idNumber);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.right, idNumber);
    } else if (node.value < idNumber) {
      // print reverse order
      printFrontToBack(node.right, idNumber);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.left, idNumber);
    } else {
      // order doesn't matter
      printFrontToBack(node.left, idNumber);
      printFrontToBack(node.right, idNumber);
    }
  }

}

2 个答案:

答案 0 :(得分:1)

如果您希望按ID排序对象,可以使用TreeMap

public static void main(String[] args) {
    Map<Integer, Person> persons = new TreeMap<Integer, Person>();
    persons.put(433, new Person(433, "John", "Smith");
    persons.put(211, new Person(211, "Peter", "Johnson");
    persons.put(556, new Person(556, "Laura", "Walters");

    // This loop will print out ordering by id
    for (Integer key: persons.keySet())
        System.out.println(persons.get(key));
}

答案 1 :(得分:1)

您也可以使Person实现Comparable,然后添加compareTo(Person p)方法。 然后,您可以将所有生成的Person对象添加到ArrayList,并使用Collections.sort()对其进行排序。