Thisi s a program of a binary tree which is extended from its super class Simple Tree. It uses all the functionalities from its super class. A binary tree consists of two leaves mainly left child nodes and right child nodes.
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
Binary tree in java
1.
2. Binary Tree in Java
Thisis a program of a binary tree which is extended from its super
class SimpleTree. It uses all the functionalities from its super
class. A binary tree consists of two leaves mainly left child nodes
and right child nodes.
There are different methods defined performing different
functionalities. Initially it is checked whether the root node is
created for a binary tree or not. Another method is counting for the
leaves of the binary tree, by counting the left and right child nodes.
Some method is checking for the nodes having both the child nodes
available or not. Also methods are modifying and removing the required
nodes.
public class BinaryTree extends SimpleTree {
// counting leaves
public int countLeaves() {
if (root == null) {
return 0;
} else {
return countLeaves(root);
}
}
/*
* determine how many leaves are in the subtree whose root is
node
* Precondition: node is not null
*/
private int countLeaves(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
return 1;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
return countLeaves(node.getRight());
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
return countLeaves(node.getLeft());
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
return countLeaves(node.getLeft()) +
countLeaves(node.getRight());
}
}
// count nodes
3. /**
* return true when every node in the tree has either two children
or none
* (but no node has only one child)
*/
public boolean twoChildrenOrNone() {
if (root == null) {
return true;
} else {
return twoChildrenOrNone(root);
}
}
/*
* determine whether all the node has two children or none
* Precondition: node is not null
*/
private boolean twoChildrenOrNone(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
return true;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
return false;
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
return false;
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
return twoChildrenOrNone(node.getLeft()) &&
twoChildrenOrNone(node.getRight());
}
}
// remove leaves
/**
* modify the tree by removing every leaf node
*/
public void trim() {
if (root != null) {
root = trim(root);
}
}
/*
* remove all the leaves.
* Precondition: node is not null
*/
private BinaryNode trim(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
4. // this is a leaf, remove it from the tree.
return null;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
node.setRight(trim(node.getRight()));
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
node.setLeft(trim(node.getLeft()));
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
node.setLeft(trim(node.getLeft()));
node.setRight(trim(node.getRight()));
}
return node;
}
}