Java, convert Ternary operator to IF ELSE - java

I am sure this repeats something, but yet due to short time I have to ask you straight forward help.
How to convert Ternary (: and ?) operator into IF-ELSE statement in Java?
public Integer get(Integer index) {
if (first == null) {
return null;
}
MyNode curNode = first;
while (index >= 0) {
if (index == 0) {
return curNode == null ? null : curNode.getValue();
} else {
curNode = curNode == null ? null : curNode.getNext();
index--;
}
}
return null;
}
What I did tried, but it gave me wrong output (this is all about LinkedList) is modifying it to:
while (index >= 0) {
if (index == 0) {
// pre-modified -> return curNode == null ? null : curNode.getValue();
if (first == null) {
return null;
} else {
return curNode.getValue();
}
} else {
if (curNode != null) {
return curNode.getNext().getValue();
//return null;
} else {
return null;
}
// pre-modified -> curNode = curNode == null ? null : curNode.getNext();
// pre-modified -> index--;
}
}

In addition to that it was necessary also to fix part after else to:
else {
if (curNode != null) {
curNode = curNode.getNext();
index--;
//return null;
} else {
curNode = null;
}
// pre-modified -> curNode = curNode == null ? null : curNode.getNext();
// pre-modified -> index--;
}
Question solved.

Related

Java - Node search optimization and performance

I need to search for 7 ID and if 1 is found return this Node. As I can't find any way to search for id.contains I made the code below that meets my need. But it hurts my eyes to look at it. Is there a way to optimize this code ??
As for performance, would it be necessary to use something like node.recycle() in the List to avoid overloading ???
private AccessibilityNodeInfo searchID() {
AccessibilityNodeInfo node = getRootInActiveWindow();
if (node != null) {
List<AccessibilityNodeInfo> id1 = node.findAccessibilityNodeInfosByViewId("com....:id/id1");
List<AccessibilityNodeInfo> id2 = node.findAccessibilityNodeInfosByViewId("com....:id/id2");
List<AccessibilityNodeInfo> id3 = node.findAccessibilityNodeInfosByViewId("com....:id/id3");
List<AccessibilityNodeInfo> id4 = node.findAccessibilityNodeInfosByViewId("com....:id/id4");
List<AccessibilityNodeInfo> id5 = node.findAccessibilityNodeInfosByViewId("com....:id/id5");
List<AccessibilityNodeInfo> id6 = node.findAccessibilityNodeInfosByViewId("com....:id/id6");
List<AccessibilityNodeInfo> id7 = node.findAccessibilityNodeInfosByViewId("com....:id/id7");
if (id1 != null && !id1.isEmpty()) {
return id1.get(0);
} else if (id2 != null && !id2.isEmpty()) {
return id2.get(0);
} else if (id3 != null && !id3.isEmpty()) {
return id3.get(0);
} else if (id4 != null && !id4.isEmpty()) {
return id4.get(0);
} else if (id5 != null && !id5.isEmpty()) {
return id5.get(0);
} else if (id6 != null && !id6.isEmpty()) {
return id6.get(0);
} else {
if (id7 != null && !id7.isEmpty()) {
return id7.get(0);
}
}
}
return null;
}

LeetCode tree question no. 872 (leaf similar trees)

class Solution {
static ArrayList<Integer> leaves1 = new ArrayList<Integer>();
static ArrayList<Integer> leaves2 = new ArrayList<Integer>();
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
if(root1.left == null && root1.right == null && root2.left == null && root2.right == null)
{
if(root1.val == root2.val)
return true;
else
return false;
}
leafmethod1(root1);
leafmethod2(root2);
if (leaves1.size() != leaves2.size())
return false;
for(int k=0;k<leaves1.size();k++)
{
if(leaves1.get(k) != leaves2.get(k))
return false;
}
return true;
}
public void leafmethod1(TreeNode root)
{
if(root!=null && root.left==null && root.right==null)
{
leaves1.add(root.val);
}
if(root!=null)
{
if(root.left != null)
{
leafmethod1(root.left);
}
if(root.right != null)
{
leafmethod1(root.right);
}
}
}
public void leafmethod2(TreeNode root)
{
if(root!=null && root.left == null && root.right == null)
{
leaves2.add(root.val);
}
if(root!=null)
{
if(root.left != null)
{
leafmethod2(root.left);
}
if(root.right != null)
{
leafmethod2(root.right);
}
}
}
}
This is the code I wrote. But it's not running for some particular case like eg; root1 = [1,2] and root2 = [2, 2]. It seemed to work for all other big cases but only in small cases like eg; root1 =[1] and root2 = [1] it's giving me wrong answer.
I don't know where I am going wrong. Kindly help.

How to write code for Binary Search Tree Deletion?

I have written the following pseudocode for a removeNode() method while working with BST's:
If left is null
Replace n with n.right
Else if n.right is null
Replace n with n.left
Else
Find Predecessor of n
Copy data from predecessor to n
Recursively delete predecessor*
Not only do I want this method to delete or remove Nodes, but I also want it to return true if the deletion is successful.
This is what I have written so far, and I was wondering if anyone would have feedback, suggested changes, or tips to help me complete the method. I will also attach my whole program below this method.
private void removeNode(Node<E> n) {
if (n.left == null) {
replace(n, n.right);
} else if (n.right == null) {
replace(n, n.left);
} else {
//How do I find pred of n
//Copy data from pred to n
//Recursively delete pred
}
}
Here is the rest of my code:
import java.util.Random;
public class BinarySearchTree<E extends Comparable<? super E>> extends BinaryTree<E> {
public boolean contains(E item) {
return findNode(item, root) != null;
}
private Node<E> findNode(E item, Node<E> n) {
if (n == null || item == null) return null;
int result = item.compareTo(n.data);
if (result == 0) {
return n;
} else if (result > 0) {
return findNode(item, n.right);
} else {
return findNode(item, n.left);
}
}
public E max() {
Node<E> m = maxNode(root);
return (m != null) ? m.data : null;
}
private Node<E> maxNode(Node<E> n) {
if (n == null) return null;
if (n.right == null) return n;
return maxNode(n.right);
}
public E min() {
Node<E> m = minNode(root);
return (m != null) ? m.data : null;
}
private Node<E> minNode(Node<E> n) {
if (n == null) return null;
if (n.left == null) return n;
return minNode(n.left);
}
public E pred(E item) {
Node<E> n = findNode(item, root);
if (n == null) return null;
Node<E> pred = predNode(n);
return (pred != null) ? pred.data : null;
}
private Node<E> predNode(Node<E> n) {
assert n != null;
if (n.left != null) return maxNode(n.left);
Node<E> p = n.parent;
while (p != null && p.left == n) {
n = p;
p = p.parent;
}
return p;
}
public E succ(E item) {
Node<E> n = findNode(item, root);
if (n == null) return null;
Node<E> succ = succNode(n);
return (succ != null) ? succ.data : null;
}
private Node<E> succNode(Node<E> n) {
assert n != null;
if (n.right != null) return minNode(n.right);
Node<E> p = n.parent;
while (p != null && p.right == n) {
n = p;
p = p.parent;
}
return p;
}
public void add(E item) {
if (item == null) return;
if (root == null) {
root = new Node<>(item, null);
} else {
addNode(item, root);
}
}
private void addNode(E item, Node<E> n) {
assert item != null && n != null;
int result = item.compareTo(n.data);
if (result < 0) {
if (n.left == null) {
n.left = new Node<>(item, n);
} else {
addNode(item, n.left);
}
} else if (result > 0) {
if (n.right == null) {
n.right = new Node<>(item, n);
} else {
addNode(item, n.right);
}
} else {
return; // do not add duplicates
}
}
public boolean remove(E item) {
Node<E> n = findNode(item, root);
if (n == null) return false;
removeNode(n);
return true;
}
private void removeNode(Node<E> n) {
if (n.left == null) {
replace(n, n.right);
} else if (n.right == null) {
replace(n, n.left);
} else {
//How do I find pred of n
//Copy data from pred to n
//Recursively delete pred
}
}
private void replace(Node<E> n, Node<E> child) {
assert n != null;
Node<E> parent = n.parent;
if (parent == null) {
root = child;
} else if (parent.left == n) {
parent.left = child;
} else {
parent.right = child;
}
if (child != null) child.parent = parent;
}
public String toString() {
return inorder();
}
The code to remove an element is very straightforward.
Search for the node you want to remove.
Check if the node has children.
Case 1 - Has Only left child -> Replace current node with left child.
Case 2 - Has Only right child -> Replace current node with right child.
Case 3 - Has both children -> Find smallest element in right child subtree, replace current node with that node and then delete that node.
The Code can be implemented recursively as follows ->
BinarySearchTree.prototype.remove = function(data) {
var that = this;
var remove = function(node,data){
if(node.data === data){
if(!node.left && !node.right){
return null;
}
if(!node.left){
return node.right;
}
if(!node.right){
return node.left;
}
//2 children
var temp = that.findMin(node.right);
node.data = temp;
node.right = remove(node.right,temp);
}else if(data < node.data){
node.left = remove(node.left,data);
return node;
}else{
node.right = remove(node.right,data);
return node;
}
};
this.root = remove(this.root,data);
};

Printing branches on a Binary Tree

How do you count number of branches, in this case branches with even integers. Here's what I have so far. It seems to work for a couple of the cases.
public int evenBranches() {
return evenBranches(overallRoot);
}
private int evenBranches(IntTreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += evenBranches(root.left);
} else if (root.right != null) {
val += evenBranches(root.right);
}
if (root.data % 2 == 0) {
return val + 1;
} else {
return val;
}
}
You can modify the evenBranches() method as below: I think It will cover all edge cases, If any testcase is left, let me know, I will fix it.
public int evenBranches() {
return evenBranches(overallRoot, 0);
}
private int evenBranches(IntTreeNode root, int count) {
if(root == null || (root.left == null && root.right == null)) {
return count;
}
if(root.data % 2 == 0) {
count++;
}
count += evenBranches(root.left, count);
count += evenBranches(root.right, count);
return count;
}
You may need to remove the else condition when checking the occurrences in right branch. Otherwise it will check only one side. eg:
private int evenBranches(IntTreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += evenBranches(root.left);
}
if (root.right != null) {
val += evenBranches(root.right);
}
if (root.data % 2 == 0) {
return val + 1;
} else {
return val;
}
}
You can very well achieve the desired results by using a global variable, and applying BFS (breadth first search) on your tree, in this manner:
int evencount = 0; // global-var.
public int evenBranches() {
evenBranches(overallRoot);
return evencount;
}
private void evenBranches(IntTreeNode root) {
if(!root) return;
if( (root.left || root.right) && (root.data % 2 == 0)){
evencount++;
}
evenBranches(root.left);
evenBranches(root.right);
}

Searching binary tree recursively

Hi I'm having trouble getting this code working correctly. It seems to be jumping out of the stack when it recurses all the way down the leftmost edge of the tree. I just can't seem to figure this one out.
public static Node lookup(Node node, int lookupValue) {
if (node == null) {
return null;
} else {
if (node.value == lookupValue) {
System.out.println("Found");
return node;
} else if(node.left != null) {
return lookup(node.left, lookupValue);
} else if(node.right != null) {
return lookup(node.right, lookupValue);
} else {
return null;
}
}
}
You return whatever is returned form the left sub-tree (if present) without checking the right one. A lot of the else branching isn't necessary when there is a return statement in the if block. Change as follows:
public static Node lookup(Node node, int lookupValue) {
if (node == null)
return null;
if (node.value == lookupValue)
// System.out.println("Found");
return node;
Node rval = lookup(node.left, lookupValue);
// only return if found in left sub-tree
return (rval != null) ? rval : lookup(node.right, lookupValue);
}
Your else if are not correct you chould check the left and right everytimes:
if (node == null) return null;
if (node.value == lookupValue) {
System.out.println("Found");
return node;
}
Node found = lookup(node.left, lookupValue);
if(found != null) {
return found;
}
return lookup(node.right, lookupValue);

Categories