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
13 changes: 13 additions & 0 deletions src/BinNodeComplexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# סיבוכיות זמן ריצה של עצים בינאריים

### ספציפית לפעולה followerN (עמוד 204 תרגיל 19)

## סיבוכיות הפעולה היא O(n*m)

## m - כמות הצמתים בעץ

## n - המספר שקובל כפרמטר

## פעמים מזמנים את הפעולה של ספורמספר על על צמתי העץ nמכיוון ש

## ולכן זה O(n*m)
79 changes: 57 additions & 22 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 All @@ -436,7 +436,7 @@ public static boolean hasValue(Node<Integer> node, int value) {
* @return the number of time value is in the node
* @RuntimeComplexity O(n)
*/
public static int countVal(Node<Integer> node, int value) {
public static int countValue(Node<Integer> node, int value) {
int count = 0;
while (node != null) {
if (node.getValue() == value) {
Expand Down Expand Up @@ -556,14 +556,14 @@ public static Node<Integer> sortNodeAllowed(Node<Integer> node) {
Node<Integer> sorted = new Node<>(0);
Node<Integer> p = sorted;
int firstmin = findMin(node);
for (int i = 0; i < countVal(node, firstmin); i++) {
for (int i = 0; i < countValue(node, firstmin); i++) {
p.setNext(new Node<>(firstmin));
p = p.getNext();
}
node = removeValue(node, firstmin);
for (int i = 0; i < lenNodes(node); i++) {
int min = findMin(node);
for (int j = 0; j < countVal(node, min); j++) {
for (int j = 0; j < countValue(node, min); j++) {
p.setNext(new Node<>(min));
p = p.getNext();
}
Expand Down Expand Up @@ -599,7 +599,7 @@ public static Node<Integer> removeValue(Node<Integer> node, int value) {
* @return the number of time value is in the node
* @RuntimeComplexity O(n)
*/
public static int countVal(Node<Character> node, char value) {
public static int countValue(Node<Character> node, char value) {
int count = 0;
while (node != null) {
if (node.getValue() == value) {
Expand Down Expand Up @@ -731,11 +731,11 @@ public static int[][] arrowMat(BinNode<Integer> root) {
BinNode<Integer> hz = copyCnstr(root);
BinNode<Integer> vr = copyCnstr(root);

hz.setValue((int) (Math.pow(2, hightTree(hz) - 1)));
hz.setValue((int) (Math.pow(2, HelpCommands.getTreeHeight(hz) - 1)));
vr.setValue(0);

int[][] arr = arrowMat(root, indexVr(vr), indexHz(hz),
new int[hightTree(root)][(int) (Math.pow(2, hightTree(root))) + 1]);
new int[HelpCommands.getTreeHeight(root)][(int) (Math.pow(2, HelpCommands.getTreeHeight(root))) + 1]);

for (int i = 1; i < arr.length; i++) {

Expand Down Expand Up @@ -837,15 +837,15 @@ public static String[][] printMat(BinNode<Integer> root) {
BinNode<Integer> hz = copyCnstr(root);
BinNode<Integer> vr = copyCnstr(root);

hz.setValue((int) (Math.pow(2, hightTree(hz) - 1)));
hz.setValue((int) (Math.pow(2, HelpCommands.getTreeHeight(hz) - 1)));
vr.setValue(0);

return printMat(root, indexVr(vr), indexHz(hz),
new String[hightTree(root)][(int) (Math.pow(2, hightTree(root))) + 1]);
new String[HelpCommands.getTreeHeight(root)][(int) (Math.pow(2, HelpCommands.getTreeHeight(root))) + 1]);

}

public static boolean isLeaf(BinNode<Integer> root) { // returns true if the node is a leaf
public static <T> boolean isLeaf(BinNode<T> root) { // returns true if the node is a leaf
if (root == null)
return false;
if (root.getLeft() == null && root.getRight() == null)
Expand All @@ -854,13 +854,15 @@ public static boolean isLeaf(BinNode<Integer> root) { // returns true if the nod
return false;
}

public static int hightTree(BinNode<Integer> root) {
if (root == null)
public static <T> boolean hasTwoSons(BinNode<T> root){
return root.hasLeft() && root.hasRight();
}

public static <T> int getTreeHeight(BinNode<T> root){
if (root == null){
return 0;
if (isLeaf(root))
return 1;
else
return (1 + Math.max(hightTree(root.getLeft()), hightTree(root.getRight())));
}
return 1 + Math.max(getTreeHeight(root.getRight()), getTreeHeight(root.getLeft()));
}

public static int nodeLevel(BinNode<Integer> t, int lev, int num) {
Expand All @@ -884,9 +886,9 @@ public static BinNode<Integer> indexHz(BinNode<Integer> index) {
if (index != null) {

if (index.getLeft() != null)
index.getLeft().setValue(index.getValue() - ((int) (Math.pow(2, hightTree(index) - 2))));
index.getLeft().setValue(index.getValue() - ((int) (Math.pow(2, HelpCommands.getTreeHeight(index) - 2))));
if (index.getRight() != null)
index.getRight().setValue(index.getValue() + ((int) (Math.pow(2, hightTree(index) - 2))));
index.getRight().setValue(index.getValue() + ((int) (Math.pow(2, HelpCommands.getTreeHeight(index) - 2))));

indexHz(index.getLeft());
indexHz(index.getRight());
Expand Down Expand Up @@ -942,21 +944,54 @@ public static void addNodeToTree(BinNode<Integer> root, int num, int side) { //
}
}

public static void preOrder(BinNode<Integer> root) {
public static <T> void preOrder(BinNode<T> root) {
if (root != null) {
System.out.print(root.toString());
System.out.print(root);
preOrder(root.getLeft());
preOrder(root.getRight());
}
}

public static void inOrder(BinNode<Integer> root) {
public static <T> void inOrder(BinNode<T> root) {
if (root != null) {
inOrder(root.getLeft());
System.out.print(root.toString());
System.out.print(root);
inOrder(root.getRight());
}
}

/**
*
* @param root the tree root
* @param value the value to look for
* @return does the value exist in the tree
* @param <T> the type of the tree
*/
public static <T> boolean hasValue(BinNode<T> root, T value){
if (root == null) {
return false;
}
if (root.getValue() == value) {
return true;
}
return hasValue(root.getRight(), value) || hasValue(root.getLeft(), value);
}

/**
*
* @param root the tree root
* @param value the value to look for
* @return the number of times the value is in the tree
* @param <T> the type of the tree
*/
public static <T> int countValue(BinNode<T> root, T value){
if (root == null) {
return 0;
}
if (root.getValue() == value) {
return 1 + countValue(root.getRight(), value) + countValue(root.getLeft(), value);
}
return countValue(root.getRight(), value) + countValue(root.getLeft(), value);
}

}
41 changes: 41 additions & 0 deletions src/L18.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class L18 extends HelpCommands {

public static int ex16pg204(BinNode<Integer> root){
if (root == null) {
return 0;
}
if (hasTwoSons(root)) {
return root.getValue() + ex16pg204(root.getLeft()) + ex16pg204(root.getRight());
}
return ex16pg204(root.getLeft()) + ex16pg204(root.getRight());
}

public static boolean ex19pg204(BinNode<Integer> root, int n) {
if (n == 0) {
return true;
}
if (countValue(root, n) != 1) {
return false;
}
return ex19pg204(root, n-1);
}

public static boolean ex18pg204(BinNode<Integer> t1, BinNode<Integer> t2){
if (t2 == null) {
return true;
}
if (!hasValue(t1, t2.getValue())) {
return false;
}
return ex18pg204(t1, t2.getLeft()) && ex18pg204(t1, t2.getRight());
}

public static void main(String[] args) {
BinNode<Integer> binNode = new BinNode<>(1, new BinNode<>(2), null);
BinNode<Integer> binNode2 = new BinNode<>(2, null, new BinNode<>(1));
printMeTree(buildTree(100));
// printMeTree(binNode2);
// System.out.println(ex18pg204(binNode, binNode2));
}

}
Binary file added src/modelimmmm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/moremodelim.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.