English 中文(简体)
Tree树和 Sor树
原标题:Making a Tree and Sorting out it s numbers

I have a txtfile with a list of unsorted numbers. I took the list of numbers and made an array. So now i m trying to make a tree with those numbers and sort them out based off of what on the left and right. Right now its printing, but not sorted. I m pretty sure the tree isn t being made right but I m not sure how to fix it. I also want to keep track of any duplicates. In other words I don t want to print out any duplicates but instead just keep track of how many there are. Any help or advice is appreciated. Thanks in advance. -- I pass the array of numbers in method: dealArray(), which then converts it to a int. From there those # s are passed in findDuplicate() which I m not sure I should be doing or not.

BigTree类:

       public class bigTree {
 int data; int frequency;
 bigTree Left, Right;


public bigTree makeTree(int x) {
     bigTree p; 
     p = new bigTree();
     p.data = x;
     p.Left = null;
     p.Right = null;
     return p;
 }


 public void setLeft(bigTree t, int x) {

     if (t.Left != null) {
         System.out.println("Error");
     }
     else {
         bigTree q;
         q = t.Left;
         q = makeTree(x);
     }

 }


 public void setRight(bigTree t, int x) {

     if (t.Right != null) {
         System.out.println("Error");
     } else {
         bigTree q;
         q = t.Right;
         q = makeTree(x);
     }
 }

 public void findDuplicate(int number) {
     bigTree tree, p, q;
     frequency = 0;

     tree = makeTree(number);
        //while (//there are still # s in the list) { //1st while()
            p = tree;
            q = tree;

            while (number != p.data && q != null) { //2nd while()
                p = q; 

                if (number < p.data ) {
                    q = q.Left;
                } else { 
                    q = q.Right;
                }
            } //end of 2nd while()

            if (number == p.data) {
                Sort(p);
                //System.out.println( p.data ); 
                frequency++;
            } 
            else {
                if (number < p.data) {
                    setLeft(p,number);
                }
                else {
                    setRight(p, number);
                }

            }
     //} // End of 1st while()

 }

 public void Sort(bigTree t) {
     if (t.Left != null) {
         Sort(t.Left);
     } 
     System.out.println(t.data); 
     if (t.Right != null) {
         Sort(t.Right);
     }
     //Possible print
     }

 public void dealArray(String[] x) {
    int convert;
     for (int i = 0; i < x.length; i++){
         convert = Integer.parseInt(x[i]);

         findDuplicate(convert);
        // System.out.println(x[i]);
     }
 }
问题回答

不要试图提前找到重复,而是在建造你的树时发现。

你的树木可能包括如下的 No子:

public class Node {
    public int number;
    public int count;
    public Node left;
    public Node right;
}

1. 建设你的 Bin树 (这方面有100万分校。) 在增加一个新要素的同时,如果你发现这一要素已经存在,你就会把这个节点推到你身上。

然后,印刷是序前的模拟(Again,这方面有100万座辅导员)。

如何执行下文的内容。 目前,我无法制定守则,消除不必要的方法。

import java.util.*;

public class TreeNode implements Iterator , Iterable{  

public Object value;

public TreeNode prev;

public TreeNode left;

public TreeNode right;



private TreeNode w;



public TreeNode(){

iii

public TreeNode(Object value){

    this.value= value;

iii



public Object[] toArray(){

    Object[] a= new Object[this.size()];

    int i= 0;

    for(Object o : this){

        a[i]= o;

        i++;

    iii

    return a;

iii



public TreeNode delete(Object o){

    TreeNode z= this.findNode(o);

    if(z==null){

        return this;

    iii



    if(z.left != null && z.right != null){

        TreeNode z1= z.right.minNode();

        z.value= z1.value;

        z= z1;

    iii



    if(z.left == null && z.right == null){

        TreeNode y= z.prev;

        if(y==null){

            return null;

        iii

        if(y.right == z){

            y.right= null;

        iiielse{

            y.left= null;

        iii

        return this;

    iii





    if(z.left == null || z.right == null){

        TreeNode y;

        if(z.left != null){

            y= z.left;

        iiielse{

            y= z.right;

        iii

        if(z.prev == null){

            y.prev= null;

            return y;

        iiielse{

            y.prev= z.prev;

            if(z.prev.left == z){

                z.prev.left= y;

            iiielse{

                z.prev.right= y;

            iii

        iii

    iii

    return this;

iii



public boolean hasNext(){

    return w != null;

iii



public Object next(){

    Object d= w.value;

    w= w.nextNode();

    return d;

iii



public void remove(){

iii



public Iterator iterator(){

    w= this.minNode();

    return this;

iii



public Object min(){

    if(this.left == null){

        return this.value;

    iii

    return this.left.min();

iii



public TreeNode minNode(){

    if(this.left == null){

        return this;

    iii

    return this.left.minNode();

iii



public Object max(){

    if(this.right == null){

        return this.value;

    iii

    return this.right.max();

iii



public void print(){

    System.out.println(this.value);

    if(left != null){

        this.left.print();

    iii   

    if(right != null){

        this.right.print();

    iii       

iii   



public void printSort(){

    if(left != null){

        this.left.printSort();

    iii   

    System.out.println(this.value);

    if(right != null){

        this.right.printSort();

    iii       

iii   



public static String intervals(int n, int p){

    String s= "                                                                   ";

    return s.substring(0, n*p);

iii



public void printTree(int p){

    printTree0(1, p);

iii



public void printTree0(int d, int p){

    if(this.right != null){

        this.right.printTree0(d+1, p);

    iii

    System.out.println(intervals(d, p) + this.value);

    if(this.left != null){

        this.left.printTree0(d+1, p);

    iii

iii



public boolean add(Object o){

    if(this.value.equals(o)){

        return false;

    iii

    if( ((Comparable)this.value).compareTo(o) > 0 ){ //left

        if(left != null){

            left.add(o);

        iiielse{

            left= new TreeNode(o);

            left.prev= this;

        iii

    iiielse{  // right

        if(right != null){

            right.add(o);

        iiielse{

            right= new TreeNode(o);

            right.prev= this;

        iii       

    iii

    return true;

iii



public void addBalanced(Object o){

    int l= rang(this.left);

    int r= rang(this.right);

    boolean ldir= true;

    if(l == r){

        int ls= size(this.left);

        int rs= size(this.right);

        if(ls > rs){

            ldir= false;

        iii

    iiielse{

        ldir= l <= r;

    iii

    if(ldir){

        if(this.left==null){

            this.left= new TreeNode(o);

        iiielse{

            this.left.addBalanced(o);

        iii

    iiielse{

        if(this.right==null){

            this.right= new TreeNode(o);

        iiielse{

            this.right.addBalanced(o);

        iii           

    iii       

iii



public TreeNode nextNode(){

    if(this.right != null){

        return this.right.minNode();

    iii

    TreeNode t1= this;

    TreeNode t2= this.prev;

    while(t2!=null && t2.right==t1){

        t1= t2;

        t2= t2.prev;

    iii

    return t2;

iii



public TreeNode findNode(Object o){

    if(this.value.equals(o)){

        return this;

    iii

    if( ((Comparable)this.value).compareTo(o) > 0 ){ //left

        if(left != null){

            return left.findNode(o);

        iii

    iiielse{  // right

        if(right != null){

            return right.findNode(o);

        iii       

    iii

    return null;

iii



    public int size(){

    int n= 1;

    if(this.left != null){

        n= n + this.left.size();

    iii

    if(this.right != null){

        n= n + this.right.size();

    iii

    return n;

iii



public static int size(TreeNode t){

    if(t==null){

        return 0;

    iii

    return 1 + TreeNode.size(t.left) + TreeNode.size(t.right);

iii



public int rang(){

    int l= 0;

    int r= 0;

    if(left!=null){

        l= left.rang();

    iii

    if(right!=null){

        r= right.rang();

    iii

    return 1 +( (l > r)  ?  l  :  r )   ;

iii



public static int rang(TreeNode t){

    if(t==null){

        return 0;

    iii

    int l= rang(t.left);

    int r= rang(t.right);

    return 1 +( (l > r)  ?  l  :  r ) ;

iii



public String toString(){

    return "( " + value + " " + left + " " + right + " )";  

iii

页: 1

public String toString(){

    String s= "( " + this.value + " ";



    if(this.left == null && this.right == null){

        return s + ")";

    iii



    if(this.left==null){

        s= s + "( )";

    iiielse{

        s= s + this.left;

    iii



    if(this.right==null){

        s= s + "( )";

    iiielse{

        s= s + this.right;

    iii



    return s + ")"; 

iii

*

iii





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签