Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
28 irasnyd 1
// Written by Ira Snyder
2
// Due Date: 11-15-2004
3
// Project #3
4
 
5
import java.io.*;
6
import java.util.*;
7
class BinaryTree {
8
    private Object root;
9
    private BinaryTree left, right;
10
 
11
    //constructors ----------------------------------------------------------
29 irasnyd 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
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
28
    public BinaryTree( Object root, BinaryTree left, BinaryTree right ) {
29
        this.root = root;
30
        this.left = left;
31
        this.right = right;
32
    }
28 irasnyd 33
 
29 irasnyd 34
    // copy constructor, creates a tree which has the same structure and
35
    // whose nodes reference the same objects as the _that_ tree
36
    public BinaryTree( BinaryTree that ) { 
37
        System.out.println("Don't use me yet");
38
    }
39
 
28 irasnyd 40
    //getter methods --------------------------------------------------------
41
 
29 irasnyd 42
    // method which returns the root data
43
    // Precondition:  none
44
    // Postcondition: return the root data
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
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
55
    public BinaryTree getRight() { return right; }
56
 
28 irasnyd 57
    //setter methods --------------------------------------------------------
58
 
29 irasnyd 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
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
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
82
    public BinaryTree setRight( BinaryTree right ) {
83
        BinaryTree temp = this.right;
84
        this.right = right;
85
        return temp;
86
    }
87
 
28 irasnyd 88
    //toString method -------------------------------------------------------
89
 
29 irasnyd 90
    // returns a String representation of the BinaryTree
30 irasnyd 91
    // Precondition:  none
92
    // Postcondition: returns a string representation of the BinaryTree
29 irasnyd 93
    public String toString() { 
94
        String sLeft = "";
95
        String sRight = "";
96
        String answer = new String();
97
 
30 irasnyd 98
        //get the left tree's string representation (if it exists)
29 irasnyd 99
        if( !(left == null) ) { sLeft = left.toString(); }
100
 
30 irasnyd 101
        //get the right tree's string representation (if it exists)
29 irasnyd 102
        if( !(right == null) ) { sRight = right.toString(); }
103
 
30 irasnyd 104
        //assemble the string to return
29 irasnyd 105
        answer = "(";
106
        if( !sLeft.equals("") ) { answer += sLeft + ","; }
107
        answer += root.toString();
108
        if( !sRight.equals("") ) { answer += "," + sRight; }
109
        answer += ")";
110
 
30 irasnyd 111
        //return the assembled string
29 irasnyd 112
        return answer;
113
    }
30 irasnyd 114
 
28 irasnyd 115
    //misc methods ----------------------------------------------------------
30 irasnyd 116
    public boolean isLeaf() { 
117
        if( left == right == null ) { return true; }
118
        return false;
119
    }
28 irasnyd 120
    public int size() { }
121
    public int height() { }
122
    public boolean contains( Object object ) { }
123
    public int numLeaves() { }
124
    public int count( Object x ) { }
125
    public boolean isFull() { }
126
    public boolean isBalanced() { }
127
    public int pathLength() { }
128
    public BinaryTree reverse() { }
129
    public int level( Object x ) { }
130
    public boolean isDisjointFrom( BinaryTree that ) { }
131
    public boolean isValid() { }
132
    public boolean equals( Object object ) { }
133
 
134
    //printing methods ------------------------------------------------------
135
    public static void preOrderPrint( BinaryTree tree ) { }
136
    public static void postOrderPrint( BinaryTree tree ) { }
137
    public static void levelOrderPrint( BinaryTree tree ) { }
138
    public static void inOrderPrint( BinaryTree tree ) { }
29 irasnyd 139
    */
28 irasnyd 140
}
141
 
142
/*
143
      BufferedReader kb = new BufferedReader(
144
                              new InputStreamReader(System.in));
145
 
146
 
147
      BufferedReader br = new BufferedReader(
148
                              new InputStreamReader(
149
                                  new FileInputStream(filename)));
150
 
151
      PrintStream ps = new PrintStream(
152
                           new FileOutputStream(
153
                               new File(filename)));
154
 
155
*/
156