Skip to main content
added 149 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 children or none and all the leaves of the tree are at the same depth).

My idea is to use recursion. I will check for any node if its left son has number of children that is equal to its right son's number of children. If so, I will return true, otherwise false;false.

The algorithm will look like this:

public class Utils {
    
    
    public boolean isFullCompleteTree(Tree<Integer> t) {
            TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
            return rootInfo.valid;
        }
    public TreeInfo isFullCompleteTree(Node<Integer> node) {
        boolean valid = true;
    
        if (node == null) {
            return new TreeInfo(true, 0);
        }
    
        TreeInfo rightInfo = isFullCompleteTree(node.goRight());
        TreeInfo leftInfo = isFullCompleteTree(node.goLeft());
    
        if ((!rightInfo.valid) || (!leftInfo.valid)) {
            valid = false;
        }
        if (rightInfo.numChildern != leftInfo.numChildern) {
            valid = false;
        }
        return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
                + 1);
    }
    }

    class TreeInfo {
       public boolean valid;
       public int numChildern;
    
       public TreeInfo(boolean valid, int numChildern) {
           this.valid = valid;
           this.numChildern = numChildern;
    }

}

} I I didn't include the tree implementation but it is pretty straightforward.

The idea of the algorithm is to check if in every node the number of right son's children is equal to the left son's children. If a tree is not full and complete, then in some node this rule will not apply.

Do you think that my alogrithmalgorithm is corretcorrect or am I missing something?

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 children or none and all the leaves of the tree are at the same depth).

My idea is to use recursion. I will check for any node if its left son has number of children that is equal to its right son's number of children. If so, I will return true, otherwise false;

The algorithm will look like this:

public class Utils {


public boolean isFullCompleteTree(Tree<Integer> t) {
        TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
        return rootInfo.valid;
    }
public TreeInfo isFullCompleteTree(Node<Integer> node) {
    boolean valid = true;

    if (node == null) {
        return new TreeInfo(true, 0);
    }

    TreeInfo rightInfo = isFullCompleteTree(node.goRight());
    TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

    if ((!rightInfo.valid) || (!leftInfo.valid)) {
        valid = false;
    }
    if (rightInfo.numChildern != leftInfo.numChildern) {
        valid = false;
    }
    return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
            + 1);
}
}

class TreeInfo {
   public boolean valid;
   public int numChildern;

   public TreeInfo(boolean valid, int numChildern) {
       this.valid = valid;
       this.numChildern = numChildern;
}

} I didn't include the tree implementation but it is pretty straightforward.

The idea of the algorithm is to check if in every node the number of right son's children is equal to the left son's children. If a tree is not full and complete, then in some node this rule will not apply.

Do you think that my alogrithm is corret or am I missing something?

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 children or none and all the leaves of the tree are at the same depth).

My idea is to use recursion. I will check for any node if its left son has number of children that is equal to its right son's number of children. If so, I will return true, otherwise false.

The algorithm will look like this:

public class Utils {
    
    
    public boolean isFullCompleteTree(Tree<Integer> t) {
            TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
            return rootInfo.valid;
        }
    public TreeInfo isFullCompleteTree(Node<Integer> node) {
        boolean valid = true;
    
        if (node == null) {
            return new TreeInfo(true, 0);
        }
    
        TreeInfo rightInfo = isFullCompleteTree(node.goRight());
        TreeInfo leftInfo = isFullCompleteTree(node.goLeft());
    
        if ((!rightInfo.valid) || (!leftInfo.valid)) {
            valid = false;
        }
        if (rightInfo.numChildern != leftInfo.numChildern) {
            valid = false;
        }
        return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
                + 1);
    }
    }

    class TreeInfo {
       public boolean valid;
       public int numChildern;
    
       public TreeInfo(boolean valid, int numChildern) {
           this.valid = valid;
           this.numChildern = numChildern;
    }

}

I didn't include the tree implementation but it is pretty straightforward.

The idea of the algorithm is to check if in every node the number of right son's children is equal to the left son's children. If a tree is not full and complete, then in some node this rule will not apply.

Do you think that my algorithm is correct or am I missing something?

Improved grammar
Source Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

algorithm Algorithm that checks if a tree is full and complete

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 childernchildren or none and all the leaves of the tree are at the same depth).

myMy idea is to use recursion. I will check for any node if it'sits left son has number of childernchildren that euqalsis equal to it'sits right son's number of childernchildren. If so -, I will return true, otherwise false;

The algorithemalgorithm will look like this:

public class Utils {


public boolean isFullCompleteTree(Tree<Integer> t) {
        TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
        return rootInfo.valid;
    }
public TreeInfo isFullCompleteTree(Node<Integer> node) {
    boolean valid = true;

    if (node == null) {
        return new TreeInfo(true, 0);
    }

    TreeInfo rightInfo = isFullCompleteTree(node.goRight());
    TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

    if ((!rightInfo.valid) || (!leftInfo.valid)) {
        valid = false;
    }
    if (rightInfo.numChildern != leftInfo.numChildern) {
        valid = false;
    }
    return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
            + 1);
}
}

class TreeInfo {
   public boolean valid;
   public int numChildern;

   public TreeInfo(boolean valid, int numChildern) {
       this.valid = valid;
       this.numChildern = numChildern;
}

} I didn't putinclude the tree implementation but it is pretty straightforward.

The idea of the algorithemalgorithm is to check if in every node the number of right son's childernchildren is equalsequal to the left son's childernchildren. ifIf a tree is not full and complete -, then in some node this rule will not apply.

Do you think that my alogrithemalogrithm is corret or am I missing something?

Thanks a lot.

algorithm that checks if a tree is full and complete

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 childern or none and all the leaves of the tree are at the same depth).

my idea is to use recursion. I will check for any node if it's left son has number of childern that euqals to it's right son's number of childern. If so - I will return true, otherwise false;

The algorithem will look like this:

public class Utils {


public boolean isFullCompleteTree(Tree<Integer> t) {
        TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
        return rootInfo.valid;
    }
public TreeInfo isFullCompleteTree(Node<Integer> node) {
    boolean valid = true;

    if (node == null) {
        return new TreeInfo(true, 0);
    }

    TreeInfo rightInfo = isFullCompleteTree(node.goRight());
    TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

    if ((!rightInfo.valid) || (!leftInfo.valid)) {
        valid = false;
    }
    if (rightInfo.numChildern != leftInfo.numChildern) {
        valid = false;
    }
    return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
            + 1);
}
}

class TreeInfo {
   public boolean valid;
   public int numChildern;

   public TreeInfo(boolean valid, int numChildern) {
       this.valid = valid;
       this.numChildern = numChildern;
}

} I didn't put the tree implementation but it is pretty straightforward.

The idea of the algorithem is to check if in every node the number of right son's childern is equals to the left son's childern. if a tree is not full and complete - then in some node this rule will not apply.

Do you think that my alogrithem is corret or am I missing something?

Thanks a lot.

Algorithm that checks if a tree is full and complete

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 children or none and all the leaves of the tree are at the same depth).

My idea is to use recursion. I will check for any node if its left son has number of children that is equal to its right son's number of children. If so, I will return true, otherwise false;

The algorithm will look like this:

public class Utils {


public boolean isFullCompleteTree(Tree<Integer> t) {
        TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
        return rootInfo.valid;
    }
public TreeInfo isFullCompleteTree(Node<Integer> node) {
    boolean valid = true;

    if (node == null) {
        return new TreeInfo(true, 0);
    }

    TreeInfo rightInfo = isFullCompleteTree(node.goRight());
    TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

    if ((!rightInfo.valid) || (!leftInfo.valid)) {
        valid = false;
    }
    if (rightInfo.numChildern != leftInfo.numChildern) {
        valid = false;
    }
    return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
            + 1);
}
}

class TreeInfo {
   public boolean valid;
   public int numChildern;

   public TreeInfo(boolean valid, int numChildern) {
       this.valid = valid;
       this.numChildern = numChildern;
}

} I didn't include the tree implementation but it is pretty straightforward.

The idea of the algorithm is to check if in every node the number of right son's children is equal to the left son's children. If a tree is not full and complete, then in some node this rule will not apply.

Do you think that my alogrithm is corret or am I missing something?

Source Link
Matoy
  • 153
  • 3

algorithm that checks if a tree is full and complete

I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 childern or none and all the leaves of the tree are at the same depth).

my idea is to use recursion. I will check for any node if it's left son has number of childern that euqals to it's right son's number of childern. If so - I will return true, otherwise false;

The algorithem will look like this:

public class Utils {


public boolean isFullCompleteTree(Tree<Integer> t) {
        TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
        return rootInfo.valid;
    }
public TreeInfo isFullCompleteTree(Node<Integer> node) {
    boolean valid = true;

    if (node == null) {
        return new TreeInfo(true, 0);
    }

    TreeInfo rightInfo = isFullCompleteTree(node.goRight());
    TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

    if ((!rightInfo.valid) || (!leftInfo.valid)) {
        valid = false;
    }
    if (rightInfo.numChildern != leftInfo.numChildern) {
        valid = false;
    }
    return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
            + 1);
}
}

class TreeInfo {
   public boolean valid;
   public int numChildern;

   public TreeInfo(boolean valid, int numChildern) {
       this.valid = valid;
       this.numChildern = numChildern;
}

} I didn't put the tree implementation but it is pretty straightforward.

The idea of the algorithem is to check if in every node the number of right son's childern is equals to the left son's childern. if a tree is not full and complete - then in some node this rule will not apply.

Do you think that my alogrithem is corret or am I missing something?

Thanks a lot.