package psa.naloga1; public class NodeBinarno { private static int counter; private int key; public int compare(NodeBinarno node) { counter++; return node.key - this.key; } public int getCounter() { return counter; } public void resetCounter() { counter=0; } }//don't rewrite this package psa.naloga1; public class Binarno { private NodeBinarno root; public boolean insert(int element) { if (root == null) { root = new NodeBinarno(element); return true; } else { return root.insert(element); } } public boolean delete(int element) { if (root == null) { return false; } if (root.getKey() == element) { NodeBinarno temporary = new NodeBinarno(0); temporary.left = root; boolean close = root.delete(temporary, element); root = temporary.left; return close; } else { return root.delete(null, element); } } public boolean search(int element) { if (root == null) { return false; } return root.search(root, element); } public int getCounter() { return root != null?root.getCounter():null; } public void resetCounter() { if(root!= null) root.resetCounter(); } }  rewrite this code in another way

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

package psa.naloga1;

public class NodeBinarno {
private static int counter;
private int key;
public int compare(NodeBinarno node) {
counter++;
return node.key - this.key;
}
public int getCounter() {
return counter;
}
public void resetCounter() {
counter=0;
}
}//don't rewrite this


package psa.naloga1;

public class Binarno {
private NodeBinarno root;


public boolean insert(int element) {
if (root == null) {
root = new NodeBinarno(element);
return true;
} else {
return root.insert(element);
}
}


public boolean delete(int element) {
if (root == null) {
return false;
}
if (root.getKey() == element) {
NodeBinarno temporary = new NodeBinarno(0);
temporary.left = root;
boolean close = root.delete(temporary, element);
root = temporary.left;
return close;
}
else {
return root.delete(null, element);
}
}


public boolean search(int element) {
if (root == null) {
return false;
}
return root.search(root, element);
}


public int getCounter() {
return root != null?root.getCounter():null;
}

public void resetCounter() {
if(root!= null)
root.resetCounter();
}
}
 rewrite this code in another way 

Expert Solution
steps

Step by step

Solved in 6 steps with 4 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

package psa.naloga1;

public class NodeBinarno {
private static int counter;
private int key;
NodeBinarno left;
NodeBinarno right;

public NodeBinarno(int key) {
this.key = key;
this.left = null;
this.right = null;
}

public NodeBinarno() {
this.left = null;
this.right = null;
}

public NodeBinarno getLeft() {
return this.left;
}

public NodeBinarno setLeft(NodeBinarno left) {
return this.left = left;
}

public NodeBinarno getRight() {
return this.right;
}

public NodeBinarno setRight(NodeBinarno right) {
return this.right = right;
}


public int getKey() {
return this.key;
}

public boolean search(NodeBinarno root, int el) {
NodeBinarno c1 = new NodeBinarno(el);

if (root != null) {
int comp = root.compare(c1);

if (comp == 0) {
return true;
}
else if (comp < 0) {
return search(root.left, el);
}
else if (comp > 0) {
return search(root.right, el);
}

} return false;
}


public boolean insert(int el) {
NodeBinarno current = new NodeBinarno(el);

if ((compare(current)) == 0) {
return false;
}
else if ((compare(current)) > 0){
if (right == null) {
right = current;
return true;
}
else {
return right.insert(el);
}
}
else if ((compare(current)) < 0) {
if (left == null) {
left = current;
return true;
}
else {
return left.insert(el);
}
}
return false;
}

public boolean delete(NodeBinarno root, int element) {
NodeBinarno current = new NodeBinarno(element);
int cmp = compare(current);

if (cmp == 0) {
if (left != null && right != null) { // 2 children
int min = right.findMin();
this.key = min;
right.delete(this, this.key);
}
else if (root.right == this) {
root.right = (left != null) ? left : right;
}
else if (root.left == this) { // 1 child
root.left = (left != null) ? left : right;
}
return true;
}
if (cmp < 0) {
return (left != null) ? left.delete(this, element) : false;
}
return (right != null) ? right.delete(this, element) : false;
}


public int findMin() {
return left == null ? this.key : left.findMin();
}


public int compare(NodeBinarno node) {
counter++;
return node.key - this.key;
}

public int getCounter() {
return counter;
}

public void resetCounter() {
counter=0;
}
} i forgot this part of the code rewrite this part too

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education