Subversion Repositories programming

Rev

Rev 28 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28 Rev 29
Line 7... Line 7...
7
class BinaryTree {
7
class BinaryTree {
8
    private Object root;
8
    private Object root;
9
    private BinaryTree left, right;
9
    private BinaryTree left, right;
10
 
10
 
11
    //constructors ----------------------------------------------------------
11
    //constructors ----------------------------------------------------------
-
 
12
    
-
 
13
    // constructor to create a singleton tree
-
 
14
    // Precondition:  root is a non-null Object
-
 
15
    // Postcondition: returns a BinaryTree with the Object given as the root
-
 
16
    //                data, and null left and right subtrees
12
    public BinaryTree( Object root ) { }
17
    public BinaryTree( Object root ) { 
-
 
18
        this.root = root;
-
 
19
        this.left = null;
-
 
20
        this.right = null;
-
 
21
    }
-
 
22
    
-
 
23
    // constructor to create a BinaryTree with the given Object as the root
-
 
24
    // data, and the given BinaryTrees as the left and right subtrees
-
 
25
    // Precondition:  root is a non-null Object (right and left CAN be null
-
 
26
    // Postcondition: returns a BinaryTree with the given root data and
-
 
27
    //                the given left and right subtrees
13
    public BinaryTree( Object root, BinaryTree left, BinaryTree right ) { }
28
    public BinaryTree( Object root, BinaryTree left, BinaryTree right ) {
-
 
29
        this.root = root;
-
 
30
        this.left = left;
-
 
31
        this.right = right;
-
 
32
    }
-
 
33
 
-
 
34
    // copy constructor, creates a tree which has the same structure and
-
 
35
    // whose nodes reference the same objects as the _that_ tree
14
    public BinaryTree( BinaryTree that ) { }
36
    public BinaryTree( BinaryTree that ) { 
-
 
37
        System.out.println("Don't use me yet");
-
 
38
    }
15
 
39
 
16
    //getter methods --------------------------------------------------------
40
    //getter methods --------------------------------------------------------
-
 
41
 
-
 
42
    // method which returns the root data
-
 
43
    // Precondition:  none
-
 
44
    // Postcondition: return the root data
17
    public Object getRoot() { }
45
    public Object getRoot() { return root; }
-
 
46
 
-
 
47
    // method which will return a reference to the left subtree
-
 
48
    // Precondition:  none
-
 
49
    // Postcondition: returns a reference to the left subtree
18
    public BinaryTree getLeft() { }
50
    public BinaryTree getLeft() { return left; }
-
 
51
 
-
 
52
    // method which will return a reference to the right subtree
-
 
53
    // Precondition:  none
-
 
54
    // Postcondition: returns a reference to the right subtree
19
    public BinaryTree getRight() { }
55
    public BinaryTree getRight() { return right; }
20
 
56
 
21
    //setter methods --------------------------------------------------------
57
    //setter methods --------------------------------------------------------
-
 
58
 
-
 
59
    // method which updates the root data
-
 
60
    // Precondition:  root is non-null
-
 
61
    // Postcondition: sets this.root to the new data, returns the old data
22
    public Object setRoot( Object Root ) { }
62
    public Object setRoot( Object root ) { 
-
 
63
        Object temp = this.root;
-
 
64
        this.root = root;
-
 
65
        return temp;
-
 
66
    }
-
 
67
    
-
 
68
    // method which updates the left subtree
-
 
69
    // Precondition:  none ( left CAN be null )
-
 
70
    // Postcondition: sets this.left to the new subtree,
-
 
71
    //                returns a reference to the old left subtree
23
    public BinaryTree setLeft( BinaryTree left ) { }
72
    public BinaryTree setLeft( BinaryTree left ) { 
-
 
73
        BinaryTree temp = this.left;
-
 
74
        this.left = left;
-
 
75
        return temp;
-
 
76
    }
-
 
77
 
-
 
78
    // method which update the right subtree
-
 
79
    // Precondition:  none ( right CAN be null )
-
 
80
    // Postcondition: sets this.right to the new subtree,
-
 
81
    //                returns a reference to the old right subtree
24
    public BinaryTree setRight( BinaryTree right ) { }
82
    public BinaryTree setRight( BinaryTree right ) {
-
 
83
        BinaryTree temp = this.right;
-
 
84
        this.right = right;
-
 
85
        return temp;
-
 
86
    }
25
 
87
 
26
    //toString method -------------------------------------------------------
88
    //toString method -------------------------------------------------------
27
    public String toString() { }
-
 
28
 
89
 
-
 
90
    // returns a String representation of the BinaryTree
-
 
91
    public String toString() { 
-
 
92
        String sLeft = "";
-
 
93
        String sRight = "";
-
 
94
        String answer = new String();
-
 
95
        
-
 
96
        //check left tree
-
 
97
        if( !(left == null) ) { sLeft = left.toString(); }
-
 
98
        
-
 
99
        //check right tree
-
 
100
        if( !(right == null) ) { sRight = right.toString(); }
-
 
101
 
-
 
102
        //answer = "(" + sLeft + "," + root.toString() + "," + sRight + ")";
-
 
103
        
-
 
104
        answer = "(";
-
 
105
        if( !sLeft.equals("") ) { answer += sLeft + ","; }
-
 
106
        answer += root.toString();
-
 
107
        if( !sRight.equals("") ) { answer += "," + sRight; }
-
 
108
        answer += ")";
-
 
109
        
-
 
110
        return answer;
-
 
111
    }
-
 
112
    /*
29
    //misc methods ----------------------------------------------------------
113
    //misc methods ----------------------------------------------------------
30
    public boolean isLeaf() { }
114
    public boolean isLeaf() { }
31
    public int size() { }
115
    public int size() { }
32
    public int height() { }
116
    public int height() { }
33
    public boolean contains( Object object ) { }
117
    public boolean contains( Object object ) { }
Line 45... Line 129...
45
    //printing methods ------------------------------------------------------
129
    //printing methods ------------------------------------------------------
46
    public static void preOrderPrint( BinaryTree tree ) { }
130
    public static void preOrderPrint( BinaryTree tree ) { }
47
    public static void postOrderPrint( BinaryTree tree ) { }
131
    public static void postOrderPrint( BinaryTree tree ) { }
48
    public static void levelOrderPrint( BinaryTree tree ) { }
132
    public static void levelOrderPrint( BinaryTree tree ) { }
49
    public static void inOrderPrint( BinaryTree tree ) { }
133
    public static void inOrderPrint( BinaryTree tree ) { }
-
 
134
    */
50
}
135
}
51
 
136
 
52
/*
137
/*
53
      BufferedReader kb = new BufferedReader(
138
      BufferedReader kb = new BufferedReader(
54
                              new InputStreamReader(System.in));
139
                              new InputStreamReader(System.in));