Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BinTree {
- private int root;
- private BinTree left;
- private BinTree right;
- private boolean empty = false;
- public BinTree(int rt, BinTree l, BinTree r) {
- root = rt;
- left = l;
- right = r;
- }
- public BinTree() {
- empty = true;
- }
- public BinTree(double p) {
- if (Math.random() < p) {
- empty = true;
- } else {
- root = (int)(Math.random()*100);
- left = new BinTree(p*1.1);
- right = new BinTree(p*1.1);
- }
- }
- public boolean isEmpty() {
- return empty;
- }
- public int size() {
- if(isEmpty() == true) {
- return 0;
- } else {
- return left.size() + right.size() + 1;
- }
- }
- public int height() {
- if(isEmpty()) {
- return -1;
- } else {
- return 1 + Math.max(left.height(), right.height());
- }
- }
- public int leaves() {
- if (isEmpty()){
- return 0;
- }else if(height()==0){
- return 1;
- }else{
- return left.leaves() + right.leaves();
- }
- }
- public boolean search(int n) {
- if (isEmpty()){
- return false;
- }else{
- return root == n||left.search(n)||right.search(n);
- }
- }
- public void print() {
- if (!isEmpty()){
- left.print();
- right.print();
- System.out.println(root);
- }
- }
- public boolean perfect(){
- if(isEmpty()){
- return true;
- }else{
- return left.height() == right.height() &&
- left.perfect() && right.perfect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement