Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions src/HelpCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public static int sumNodes(Node<Integer> node) {
* @return does value exist in the node
* @RuntimeComplexity O(n)
*/
public static boolean hasValue(Node<Integer> node, int value) {
public static <T> boolean hasValue(Node<T> node, T value) {
boolean has = false;
while (node != null) {
if (node.getValue() == value) {
Expand Down Expand Up @@ -480,7 +480,7 @@ public static int findMax(Node<Integer> node) {
* @return the length of the node
* @RuntimeComplexity O(n)
*/
public static int lenNodes(Node<Integer> node) {
public static <T> int lenNodes(Node<T> node) {
int len = 0;
while (node != null) {
len++;
Expand Down Expand Up @@ -510,12 +510,12 @@ public static int lenNodesChar(Node<Character> node) {
* @return a new node containing the numbers between indexes start,end in the node
* @RuntimeComplexity O(n = start + ( end - start))
*/
public static Node<?> subNode(Node<?> node, int start, int end) {
public static <T> Node<T> subNode(Node<T> node, int start, int end) {
for (int i = 1; i < start - 1; i++) {
node = node.getNext();
}
Node<?> subNode = node.getNext();
Node<?> temp = subNode;
Node<T> subNode = node.getNext();
Node<T> temp = subNode;
for (int i = 1; i < end - 1; i++) {
temp = temp.getNext();
}
Expand Down Expand Up @@ -668,6 +668,20 @@ public static <T> Node<T> nodeAt(Node<T> node, int index) {
return node;
}

public static <T> boolean isSame(Node<T> firstNode, Node<T> secondNode){
if (lenNodes(firstNode) != lenNodes(secondNode)) {
return false;
}
while (firstNode != null) {
if (firstNode.getValue() != secondNode.getValue()) {
return false;
}
firstNode = firstNode.getNext();
secondNode = secondNode.getNext();
}
return true;
}

public static BinNode<Integer> buildBinNode(int n) {
BinNode<Integer> root = new BinNode<Integer>((int) (Math.random() * 99) + 1);
BinNode<Integer> bn = root;
Expand Down Expand Up @@ -923,22 +937,34 @@ public static BinNode<Integer> buildTree(int n) {
return root;
}

public static void addNodeToTree(BinNode<Integer> root, int num, int side) { // adds a new leaf to the tree with value num to side ( 0 - left, 1- right)
public static <T> BinNode<T> buildTree(T... values) {
T value = values[0];
BinNode<T> root = new BinNode<>(value);
int side;
for (int i = 1; i < values.length; i++) {
side = (int) (Math.random() * 2);
value = values[i];
addNodeToTree(root, value, side);
}
return root;
}

public static <T> void addNodeToTree(BinNode<T> root, T value, int side) { // adds a new leaf to the tree with value num to side ( 0 - left, 1- right)
if (side == 0 && root.getLeft() == null) {
root.setLeft(new BinNode<Integer>(num));
root.setLeft(new BinNode<>(value));
return;
}
if (side == 1 && root.getRight() == null) {
root.setRight(new BinNode<Integer>(num));
root.setRight(new BinNode<>(value));
return;
}
if (side == 0 && root.getLeft() != null) {
side = (int) (Math.random() * 2);
addNodeToTree(root.getLeft(), num, side);
addNodeToTree(root.getLeft(), value, side);
}
if (side == 1 && root.getRight() != null) {
side = (int) (Math.random() * 2);
addNodeToTree(root.getRight(), num, side);
addNodeToTree(root.getRight(), value, side);
}
}

Expand All @@ -958,5 +984,14 @@ public static void inOrder(BinNode<Integer> root) {
}
}

public static <T> boolean hasValue(BinNode<T> root, T value) {
if (root == null) {
return false;
}
if (root.getValue() == value) {
return true;
}
return hasValue(root.getLeft(), value) || hasValue(root.getRight(), value);
}

}
59 changes: 59 additions & 0 deletions src/L20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.ArrayList;
import java.util.Arrays;

public class L20 extends HelpCommands {

public static <T> void wideScan(BinNode<T> root) {
Queue<BinNode<T>> qTree = new Queue<>();
qTree.insert(root);
while (!qTree.isEmpty()) {
while (qTree.head() == null) {
qTree.remove();
if (qTree.isEmpty()) {
return;
}
}
System.out.println(qTree.head().getValue());
qTree.insert(qTree.head().getLeft());
qTree.insert(qTree.head().getRight());
qTree.remove();
}
}

// public static boolean ex38(BinNode<Integer> root){
//
// }

public static <T> int ex41(BinNode<T> root, T value){
Queue<BinNode<T>> qTree = new Queue<>();
qTree.insert(root);
int i = 0;
while (!qTree.isEmpty()) {
while (qTree.head() == null) {
qTree.remove();
if (qTree.isEmpty()) {
return -1;
}
}
if (qTree.head().getValue() == value) {
return i;
}
if (qTree.head().hasLeft()) {
qTree.insert(qTree.head().getLeft());
}
if (qTree.head().hasRight()) {
qTree.insert(qTree.head().getRight());
}
qTree.remove();
i++;
}
return -1;
}

public static void main(String[] args) {
BinNode<Integer> root = buildTree(1,2,2,2,2,20);
printMeTree(root);
System.out.println(ex41(root, 20));
}

}