MQL4 Partial Close orders and add a trailing stop - algorithm

I've coded an algorithm that can enter multiple trades based on several signals from different indicators. I am now trying to figure out how to close half of my open positions (partial close) when they've reached a target profit range and then add a trailing stop on the other half of those particular trades that have been closed.
Any ideas on how this can be done? The main issue I'm having is when it comes to writing a code that allows the EA to detect when an open order is the other half of a previously closed order.
Any advice will be much appreciated.
//CLOSING ORDER LOOP
int ticket = OrderTicket();
double orderlots = OrderLots();
for (int b = OrdersTotal() -1 ; b >=0 ;b--)
{
if (!OrderSelect (b, SELECT_BY_POS, MODE_TRADES))continue;
if (OrderSymbol() == Symbol() && OrderType() <= OP_SELL && NormalizeDouble(orderlots,1) == NormalizeDouble(lots, 1))
//CHECK PARTIAL CLOSE
{
if(OrderType() == OP_BUY && (Bid - OrderOpenPrice()) >= TakeProfit)
{
if(!OrderClose(ticket, orderlots/2, Bid, 3, Blue))
return;
}
else
{
if(OrderType() == OP_SELL && (OrderOpenPrice() - Ask) >= (TakeProfit))
{
if(!OrderClose(ticket, orderlots/2, Ask, 3, Blue))
{
bool answer = OrderSelect (b, SELECT_BY_POS, MODE_TRADES);
ticket = OrderTicket();
orderlots = OrderLots();
}
return;
//REDECLARE TICKETS
}
}
if (OrderSymbol() == Symbol() && OrderType() <= OP_SELL && NormalizeDouble(orderlots,1) != NormalizeDouble(lots, 1))continue;
//END
{
if(OrderType() == OP_BUY)
{
if(Bid - OrderOpenPrice() > stopLossATR)
{
if(!OrderModify(ticket, OrderOpenPrice(),Bid + (stopLossATR), OrderTakeProfit(), 0 , Green))
return;
}
}
else
if(OrderType() == OP_SELL)
{
if(OrderOpenPrice() - Ask > (stopLossATR))
{
if(!OrderModify(ticket, OrderOpenPrice(), Ask + (stopLossATR), OrderTakeProfit(), 0 , Green))
return;
if (!OrderSelect (b, SELECT_BY_POS, MODE_TRADES))continue;
}
}
}
}
}

Related

Optimizing Conway's Game of Life for an Arduino

I am making Conway's Game of life on an Arduino with a 64x64 dot matrix grid. it is working but its a little slow while running the full size. This is the code that I think is taking the longest:
int same;
int c;
// My code can run in different sizes so these needed to be writable.
int width1=64;
int height=64;
int row0[WIDTH]; // WIDTH is just the constant 64.
void check(int y)
{
int alive=0;
for(int x=0;x < width1;++x)
{
alive=0;
if(x > 0)
{
if(getPixelColor(x-1, y) > 0)
{
alive+=1;
//Serial.println("(left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y) > 0)
{
alive+=1;
//Serial.println("(right)");
}
}
if(y > 0)
{
if(getPixelColor(x, y-1) > 0)
{
alive+=1;
//Serial.println("(top)");
}
if(x > 0)
{
if(getPixelColor(x-1, y-1) > 0)
{
alive+=1;
//Serial.println("(top left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y-1) > 0)
{
alive+=1;
//Serial.println("(top right)");
}
}
}
if(row < height-1)
{
if(getPixelColor(x, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom)");
}
if(x > 0)
{
if(getPixelColor(x-1, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom right)");
}
}
}
god_Conway(x, y, alive);
}
}
void god_Conway(int x, int y, int a)
{
int born[]={3};
int survive[]={2, 3};
int kill=0;
bool birth1=0;
int living=getPixelColor(x, y);
if(living > 0)
{
if (a == 2 || a == 3)
{
kill=1;
}
else
{
kill=-1;
}
}
else
{
if (a == 3)
{
birth1=1;
}
}
if (kill == -1 || birth1 == 1)
{
for(int c=0;c<width1;c++)
{
if(row0[c]==-1)
{
row0[c]=x;
if(c,width1)
{
row0[c+1]=-1;
}
break;
}
}
}
if(kill == 1 || birth1 == 0)
{
same++;
}
}
This code checks around each pixel in a row and discovers how many pixels are on around a certain pixel. getPixelColor(x, y) is code I found for the matrix the reads the pixel's color and return a number greater than 0 if on. The check function takes about 29-30ms per row. Every millisecond counts.
I've tried a big if for just the non-edge pixels. getPixelColor(x, y) does not always return same number so dividing it by expected return number is not always accurate. I made a function to return 1 and 0 automatically then do alive+=That_function(x, y); but it slowed it down.
It only writes down the y of the pixels that needs changing on row0. The code that prints this stops when there is a -1.
Don't use the time consuming function getPixelColor to read the pixels from the display. Instead maintain the board in memory, e. g. static char board[64][64];, and keep a copy of the previous generation also in memory. If there is no library function to display the whole image at once, you need at least to call the presumed setPixelColor function only for the changed pixels.
You might even use a static char board[66][66]; with the image indexes 1..64 and spare yourself the edge considerations.

_mm256_blend_pd : merge elements from two vector

I have a function as below, and I'd like to leverage some avx intrinsics to speed it up.
bool Box::intersect(Line & line)
{
double left_x = line.x0();
double right_x = line.x1();
double lower_y = line.y0();
double upper_y = line.y1();
if (left_x > right_x) { std::swap(left_x, right_x); }
if (lower_y > upper_y) { std::swap(lower_y, upper_y); }
double box[4] = {x0(), y0(), x1(), y1()};
if (box[0] < left_x || box[2] > right_x || box[3] < lower_y || box[1] > upper_y)
{
return false;
}
if (box[0] <= left_x && box[2] >= right_x && box[1] <= lower_y && box[3] >= upper_y)
{
return true;
}
if (line.x0() == line.x1() || line.y0() == line.y1())
{
return true;
}
return false;
}
For the first two if condition, I want to make two __mm256 vector and compare it
double line_arr[4] = {left_x, right_x, lower_y, upper_y};
__m256d vec_line = _mm256_loadu_pd(line_arr);
__m256d vec_box = _mm256_loadu_pd(box);
auto vec_left = _mm256_blend_pd( vec_box, vec_line, /*???*/);
auto vec_right = _mm256_blend_pd( vec_box, vec_line, /*???*/);
auto vec_res = _mm256_cmp_pd(vec_left, vec_right, _CMP_LT_OS);
if (vec_res[0] || vec_res[1] || vec_res[2] || vec_res[3])
{
return false;
}
The problem is, how could I select elements from different vector and merge them into one __m256 vector by using _mm256_blend_pd?

Game AI algorithm, enemy following player

I'm using LigGdx to make a game, it looks like a RPG game. When enemy is in alert state, it have to follow the player, but it can move only forward, backward, left and right, and also have to divert objects when it collides, searching for the best way to reach the player, i'm newbie on game development, and my algorithm may be completely wrong, so, I really need help...
private void enemyInAlert(Enemy enemy, float delta) {
detectDirection(enemy);
enemyWalking(enemy, delta);
if (Math.round(getDistanceXofLastPosition(enemy)) == 0 && Math.round(getDistanceYofLastPosition(enemy)) == 0) {
enemy.setState(States.IDLE);
lastPosition = null;
}
}
private void detectDirection(Enemy enemy) {
float diff = getDistanceXofLastPosition(enemy) - getDistanceYofLastPosition(enemy);
if (diff < 0) {
getDirectionX(enemy);
} else if (diff > 0) {
getDirectionY(enemy);
}
}
private void getDirectionY(Enemy enemy) {
int enemyY = Math.round(enemy.getY());
int lastPositionY = Math.round(lastPosition.getY());
if (enemyY < lastPositionY && enemy.isDirectionBlocked(Direction.FORWARD) == false) { //Enemy needs to go forward
enemy.setDirection(Direction.FORWARD);
enemy.blockDirection(Direction.BACKWARD);
} else if (enemyY > lastPositionY && enemy.isDirectionBlocked(Direction.FORWARD) == false) { //Enemy needs to go backward
enemy.setDirection(Direction.BACKWARD);
enemy.blockDirection(Direction.FORWARD);
} else { //Enemy needs to change direction
if (enemy.isDirectionBlocked(Direction.LEFT) == false || enemy.isDirectionBlocked(Direction.LEFT) == false) {
enemy.blockDirection(Direction.BACKWARD);
enemy.blockDirection(Direction.FORWARD);
getDirectionX(enemy);
} else {
sortRandomDirection(enemy);
}
}
}
private void getDirectionX(Enemy enemy) {
int enemyX = Math.round(enemy.getX());
int lastPositionX = Math.round(lastPosition.getX());
if (enemyX < lastPositionX && enemy.isDirectionBlocked(Direction.RIGHT) == false) { //Enemy needs to go right
enemy.setDirection(Direction.RIGHT);
enemy.blockDirection(Direction.LEFT);
} else if (enemyX > lastPositionX && enemy.isDirectionBlocked(Direction.LEFT) == false) {
enemy.setDirection(Direction.LEFT);
enemy.blockDirection(Direction.RIGHT);
} else { //Enemy needs to change direction
if (enemy.isDirectionBlocked(Direction.FORWARD) == false && enemy.isDirectionBlocked(Direction.BACKWARD) == false) {
enemy.blockDirection(Direction.LEFT);
enemy.blockDirection(Direction.RIGHT);
getDirectionY(enemy);
} else {
sortRandomDirection(enemy);
}
}
}
I'm accepting suggestions, I can change all the code, no mercy... Sorry for the bad English :D
Thanks!!
Edit: now, I'm trying to use A*, or something like it. :D ... my code:
private void calculateRoute(Enemy enemy) {
int lowerPath = getDistanceXofLastPosition(enemy.getBounds()) + getDistanceYofLastPosition(enemy.getBounds());
path = new ArrayList<Rectangle>();
Rectangle finalRect = new Rectangle(enemy.getBounds());
List<Rectangle> openList = new ArrayList<Rectangle>();
while (getDistanceXofLastPosition(finalRect) > 0 || getDistanceYofLastPosition(finalRect) > 0) {
for (int i = -1; i < 2; i+= 1) {
outerloop:
for (int j = -1; j < 2; j+= 1) {
Rectangle temp = new Rectangle(finalRect);
temp.offSet(i, j);
if (openList.contains(temp)) {
continue;
}
if ((i == -1 && j == -1) || (i == 1 && j == -1) || (i == 0 && j == 0) || (i == 1 && j == -1) || (i == 1 && j == 1)) {
continue;
}
for (Collider collider : colliders) {
if (collider.isSolid() && Utils.detectCollision(temp, collider.getBounds())) {
continue outerloop;
}
}
openList.add(temp);
}
}
int lowerDistance = Integer.MAX_VALUE;
for (Rectangle rect : openList) {
int distance = getDistanceXofLastPosition(rect) + getDistanceYofLastPosition(rect);
distance = distance + lowerPath;
if (distance < lowerDistance) {
lowerDistance = distance;
finalRect = rect;
}
}
path.add(new Rectangle(finalRect));
}
}
but is very slow, what I can do to increase performance?
You may want to look into A* .
You can easily convert your map into a graph, using each tile as a vertex, and having said vertex connected with an edge to all the other vertices close to it. The cost associated with the edge may be variable, for instance, moving i tile on a river could cost more than moving one tile in a plane.
Then you can use a path search algorithm to find the best path from one point to another. Using this algorithm will have 2 downsides :
It has an high computational cost
It always finds the optimal solution, making your bot smarter than the average player :)
If computational and storage cost are indeed a problem, you can resort to one of A*'s cousins, such as
IDA* for cheaper memory requirements, iterate over the depth of the solutions
SMA* bounds the amount of memory the algorithm can use

Check if a binary tree is a mirror image or symmetric

What is the basic algorithm for testing if a tree is symmetrical? Because it is a binary tree, I would assume that it would be a recursive definition of sorts
The formal question is below:
A binary tree is a mirror image of itself if its left and right subtrees are identical mirror images i.e., the binary tree is symmetrical.
This is best explained with a few examples.
1
/ \
2 2
TRUE
1
/ \
2 2
\
3
FALSE
1
/ \
2 2
/ \ / \
4 3 3 4
TRUE
1
/ \
2 2
/ \ / \
3 4 3 4
FALSE
1
/ \
2 2
/ \
3 3
TRUE
In a programming language of choice, define a BTree class/C struct and an associated method to check if the tree is a mirror image. For statically typed languages, you can assume that node values are all integers.
Class/structure definition
BTree {
BTree left;
BTree right;
int value;
}
Assume that the root of the tree is tracked by caller and function isMirror() is invoked on it.
Also, if defining a class, make sure to provide a no-argument constructor and getter/setter methods if data elements are not publicly accessible.
How about calling mirrorEquals(root.left, root.right) on the following function :-
boolean mirrorEquals(BTree left, BTree right) {
if (left == null || right == null) return left == null && right == null;
return left.value == right.value
&& mirrorEquals(left.left, right.right)
&& mirrorEquals(left.right, right.left);
}
Basically compare the left subtree and inverted right subtree, drawing an imaginary line of inversion across root.
Solution 1 - Recursively:
bool isMirror(BinaryTreeNode *a, BinaryTreeNode *b)
{
return (a && b) ?
(a->m_nValue==b->m_nValue
&& isMirror(a->m_pLeft,b->m_pRight)
&& isMirror(a->m_pRight,b->m_pLeft)) :
(a == b);
}
bool isMirrorItselfRecursively(BinaryTreeNode *root)
{
if (!root)
return true;
return isMirror(root->m_pLeft, root->m_pRight);
}
Solution 2 - Iteratively:
bool isMirrorItselfIteratively(BinaryTreeNode *root)
{
/// use single queue
if(!root) return true;
queue<BinaryTreeNode *> q;
q.push(root->m_pLeft);
q.push(root->m_pRight);
BinaryTreeNode *l, *r;
while(!q.empty()) {
l = q.front();
q.pop();
r = q.front();
q.pop();
if(l==NULL && r==NULL) continue;
if(l==NULL || r==NULL || l->m_nValue!=r->m_nValue) return false;
q.push(l->m_pLeft);
q.push(r->m_pRight);
q.push(l->m_pRight);
q.push(r->m_pLeft);
}
return true;
}
Recursive and Iterative solutions in Java using approaches discussed above
Recursive
public Boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSymmetricInternal(root.left, root.right);
}
private Boolean isSymmetricInternal(TreeNode leftNode,
TreeNode rightNode) {
boolean result = false;
// If both null then true
if (leftNode == null && rightNode == null) {
result = true;
}
if (leftNode != null && rightNode != null) {
result = (leftNode.data == rightNode.data)
&& isSymmetricInternal(leftNode.left, rightNode.right)
&& isSymmetricInternal(leftNode.right, rightNode.left);
}
return result;
}
Iterative using LinkedList as a Queue
private Boolean isSymmetricRecursive(TreeNode root) {
boolean result = false;
if (root == null) {
return= true;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root.left);
queue.offer(root.right);
while (!queue.isEmpty()) {
TreeNode left = queue.poll();
TreeNode right = queue.poll();
if (left == null && right == null) {
result = true;
}
else if (left == null ||
right == null ||
left.data != right.data) {
// It is required to set result = false here
result = false;
break;
}
else if (left != null && right != null) {
queue.offer(left.left);
queue.offer(right.right);
queue.offer(left.right);
queue.offer(right.left);
}
}
return result;
}
Test Case
#Test
public void testTree() {
TreeNode root0 = new TreeNode(1);
assertTrue(isSymmetric(root0));
assertTrue(isSymmetricRecursive(root0));
TreeNode root1 = new TreeNode(1, new TreeNode(2), new TreeNode(2));
assertTrue(isSymmetric(root1));
assertTrue(isSymmetricRecursive(root1));
TreeNode root2 = new TreeNode(1,
new TreeNode(2, null, new TreeNode(3)), new TreeNode(2));
assertFalse(isSymmetric(root2));
assertFalse(isSymmetricRecursive(root2));
TreeNode root3 = new TreeNode(1, new TreeNode(2, new TreeNode(4),
new TreeNode(3)), new TreeNode(2, new TreeNode(3),
new TreeNode(4)));
assertTrue(isTreeSymmetric(root3));
assertTrue(isSymmetricRecursive(root3));
TreeNode root4 = new TreeNode(1, new TreeNode(2, new TreeNode(3),
new TreeNode(4)), new TreeNode(2, new TreeNode(3),
new TreeNode(4)));
assertFalse(isSymmetric(root4));
assertFalse(isSymmetricRecursive(root4));
}
Tree Node class
public class TreeNode {
int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data){
this(data, null, null);
}
public TreeNode(int data, TreeNode left, TreeNode right)
{
this.data = data;
this.left = left;
this.right = right;
}
}
The recursive solution from #gvijay is very clear, and here's an iterative solution.
Inspect each row of the tree from top to bottom and see if the values are a palindrome. If they all are then, yes, it's a mirror. You'll need to implement an algorithm to visit each row and include null values for sparse trees. In pseudocode:
boolean isMirror(BTree tree) {
foreach (List<Integer> row : tree.rows() {
if (row != row.reverse()) return false;
}
return true;
}
The trick is to design the algorithm to iterate the rows of a tree with consideration that sparse trees should have null values as place holders. This Java implementation seems ok:
public static boolean isMirror(BTree root) {
List<BTree> thisRow, nextRow;
thisRow = Arrays.asList(root);
while (true) {
// Return false if this row is not a palindrome.
for (int i=0; i<thisRow.size()/2; i++) {
BTree x = thisRow.get(i);
BTree y = thisRow.get(thisRow.size()-i-1);
if ((x!=null) && (y!=null)
&& (x.value != y.value))
return false;
if (((x==null) && (y!=null))
|| (x!=null) && (y==null))
return false;
}
// Move on to the next row.
nextRow = new ArrayList<BTree>();
for (BTree tree : thisRow) {
nextRow.add((tree==null) ? null : tree.lt);
nextRow.add((tree==null) ? null : tree.rt);
}
boolean allNull = true;
for (BTree tree : nextRow) {
if (tree != null) allNull = false;
}
// If the row is all empty then we're done.
if (allNull) return true;
thisRow = nextRow;
}
}
EDIT
As was pointed out in the comments, my first version of the algorithm failed for certain inputs. I'm not going to reinvent the wheel, I'll just provide a Python answer using #gvijay correct algorithm. First, a representation for the binary tree:
class BTree(object):
def __init__(self, l, r, v):
self.left = l
self.right = r
self.value = v
def is_mirror(self):
return self._mirror_equals(self.left, self.right)
def _mirror_equals(self, left, right):
if left is None or right is None:
return left is None and right is None
return (left.value == right.value
and self._mirror_equals(left.left, right.right)
and self._mirror_equals(left.right, right.left))
I tested the above code using all the sample trees in the question and the trees which were returning incorrect results, as mentioned in the comments. Now the results are correct for all cases:
root1 = BTree(
BTree(None, None, 2),
BTree(None, None, 2),
1)
root1.is_mirror() # True
root2 = BTree(
BTree(None, BTree(None, None, 3), 2),
BTree(None, None, 2),
1)
root2.is_mirror() # False
root3 = BTree(
BTree(
BTree(None, None, 4),
BTree(None, None, 3),
2),
BTree(
BTree(None, None, 3),
BTree(None, None, 4),
2),
1)
root3.is_mirror() # True
root4 = BTree(
BTree(
BTree(None, None, 3),
BTree(None, None, 4),
2),
BTree(
BTree(None, None, 3),
BTree(None, None, 4),
2),
1)
root4.is_mirror() # False
root5 = BTree(
BTree(BTree(None, None, 3), None, 2),
BTree(None, BTree(None, None, 3), 2),
1)
root5.is_mirror() # True
root6 = BTree(BTree(None, None, 1), None, 1)
root6.is_mirror() # False
root7 = BTree(BTree(BTree(None, None, 1), None, 2), None, 1)
root7.is_mirror() # False
Here is a C++ solution per gvijay
bool isMirrorTree(BTnode* LP, BTnode* RP)
{
if (LP == NULL || RP == NULL) // if either is null check that both are NULL
{
return ( LP == NULL && RP == NULL );
}
// check that data is equal and then recurse
return LP->data == RP->data &&
isMirrorTree( LP->left, RP->right ) &&
isMirrorTree( LP->right, RP->left );
}
Below is the solution with respect to C- COde
isMirror(root)
{
Symmetric(root->left, root->right);
}
Symmetric(root1,root2)
{
if( (root1->left EX-NOR root2->right) && (root1->right EX-NOR root2->left) && (root1->value==root2->value) )
//exnor operation will return true if either both present or both not present
// a EX-NOR b =(!a && !b) || (a && b))
{
Symmetric(root1->left, root2->right);
Symmetric(root1->right, root2->left);
}
else return false;
}
If someone needs a Swift version, here's one.
Another approach would be to just invert one of the subtrees, and compare the two resulting subtrees in a straightforward manner.
func compareTrees(left: TreeNode?, right: TreeNode?) -> Bool {
var res = false
if left == nil && right == nil {return true}
if left != nil && right != nil {
res = left!.val == right!.val &&
compareTrees(left!.left, right: right!.left) &&
compareTrees(left!.right, right: right!.right)
}
return res
}
func invertTree(node: TreeNode?) {
if node == nil {return}
var tmp = node!.left
node!.left = node!.right
node!.right = tmp
invertTree(node!.left)
invertTree(node!.right)
}
// and run it as:
if root == nil {print("Y")}
invertTree(root!.right)
compareTrees(root!.left, right: root!.right) ? print("Y") : print("N")
Slightly different approach.
How about do an inorder traversal of the binary tree storing all the contents in some data structure like a string/ array.
Once traversal is complete, check if the elements in your array form a palindrome.
Not as efficient space wise (recursion takes O(log(n)), this method tales O(n)) but this will work as well.
Iterative solution using slightly different approach in python. Use queue1 to store left children in order of left to right and queue2 to store right children in order of right to left and compare for equality.
def isSymmetric(root):
if not root:
return True
if not (root.left or root.right):
return True
q1 = collections.deque([root.left])
q2 = collections.deque([root.right])
while q1 and q2:
n1 = q1.popleft()
n2 = q2.popleft()
if n1 is None and n2 is None:
continue
if (n1 is None) ^ (n2 is None):
return False
if n1.val != n2.val:
return False
q1.append(n1.left)
q1.append(n1.right)
q2.append(n2.right)
q2.append(n2.left)
if not (q1 and q2):
return True
return False
using python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def helper(root1, root2):
if not root1 and not root2: return True
if not root1 or not root2: return False
if root1.val != root2.val: return False
if helper(root1.left, root2.right): return helper(root1.right, root2.left)
return False
return helper(root, root)
Thought I'd add a solution in Python that some people might find easier to understand than other approaches. The idea is:
add +1 to value returned by left child.
add -1 to value returned by right child.
return l+r to parent
So if l+r == 0 for any node in the tree, then the sub-tree anchored at that node is symmetric. Therefore the entire tree is symmetric only if l+r == 0 at root.
def check(root):
l = check(root.left)+1 if root.left else 0
r = check(root.right)-1 if root.right else 0
return l+r
def is_symmetric(root):
return root is not None and check(root) == 0
public class SymmetricTree {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] array = {1,2,2,3,4,4,3};
/*
* 1
* / \
* / \
* / \
* 2 2
* / \ / \
* / \ / \
* 3 4 4 3
*
* */
//int[] array = {1,2};
BinaryTree bt=new BinaryTree();
bt.data=1;
bt.left = new BinaryTree(2);
bt.right = new BinaryTree(2);
bt.left.right = new BinaryTree(3);
bt.right.right = new BinaryTree(3);
//bt=BinaryTree.buildATree(bt, array);
System.out.print(isSymmetric(bt));
BinaryTree.inOrderTraversal(bt);
}
public static boolean isSymmetric(BinaryTree root){
if(root==null)
return true;
return isSymmetricLR(root.left,root.right);
}
public static boolean isSymmetricLR(BinaryTree left, BinaryTree right){
if(left == null && right == null)
return true;
if(left!=null && right!=null)
return (left.data == right.data) &&
(isSymmetricLR(left.left, right.right)) &&
(isSymmetricLR(left.right, right.left));
return false;
}
}
Using Queue , Because i find Recursion a bit hard.
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
q.push(root);
while(q.empty()==false){
TreeNode* a = q.front();
q.pop();
TreeNode* b = q.front();
q.pop();
if(a->val != b->val){
return false;
}
if(a->left == nullptr and b->right != nullptr)
return false;
if(a->left != nullptr and b->right == nullptr)
return false;
if(a->right != nullptr and b->left == nullptr)
return false;
if(a->right == nullptr and b->left != nullptr)
return false;
if(a->left != nullptr and b->right != nullptr){
q.push(a->left);
q.push(b->right);
}
if(a->right != nullptr and b->left != nullptr){
q.push(a->right);
q.push(b->left);
}`enter code here`
}
return true;
}
how about a java script solution
algorithm used
araay = ["10" , "2", "2", "#", "1", "1", "#"]
lets break this into each step
step 1 = ["10"]
step 2 = [ "2", "2" ]
step 3 = ["#", "1", "1", "#"]
check mirror for each step other than first step
lets take step 3 .
step 3 = ["#", "1", "1", "#"]
break this into 2 array from middle
step 3 1 = ["#", "1" ]
step 3 2 = ["1", "#"]
Now reverse step 3 2
step 3 2 = ["1", "#"]
now check if step 3 2 is equal to step 3 1
Do the above thing for each step
if all are mirror then binary tree is mirror
const Input = ["10" , "2", "2", "#", "1", "1", "#"];
function isEqual(a,b)
{
// if length is not equal
if(a.length!=b.length)
return false;
else
{
// comapring each element of array
for(var i=0;i<a.length;i++)
if(a[i] !== b[i]){
return false;
}
return true;
}
}
// Response
let symetric = true ;
let stepSymetric = true ;
let mirror = true ;
// length of input
const length = Input.length ;
// const length = 31 ;
// lets create binary tree from it
const totalSteps =
Math.log(length + 1)/Math.log(2) ;
// check for symetric binary tree
function checkInt(n) { return parseInt(n) === n };
symetric = checkInt(totalSteps);
//now check for mirror
let goneArrayLength = 0 ;
for (let i = 0; i < totalSteps; i++) {
const cStep = Math.pow(2,i);
const stepArray = [];
// console.log(goneArrayLength);
// now update the step array
const start = goneArrayLength ;
const end = goneArrayLength + cStep ;
for (let j = start; j < end; j++) {
stepArray.push(Input[j])
}
console.log(stepArray);
// Now we have each step lets find they are mirror image or not
// check for even length
if(stepArray.length%2 !== 0 && i !== 0){
stepSymetric = false ;
}
const partitation = stepArray.length / 2 ;
const leftArray = stepArray.slice(0,partitation);
const rightArray = stepArray.slice(partitation , partitation * 2);
// console.log("leftArray");
// console.log(leftArray);
// console.log("rightArray");
// console.log(rightArray);
function mirrorCheck (){
let check = false;
if(cStep === 1){
return true ;
}
let array1 = leftArray;
let array2 = rightArray.reverse();
// console.log("first");
// console.log(array1);
// console.log("second");
// console.log(array2);
let equal = isEqual(array1 , array2)
// console.log(equal);
check = true ;
if(!equal){
// console.log("Noooooooooooooooo")
check = false ;
}
return check;
}
let mirrorC = mirrorCheck();
if(!mirrorC){
mirror = false;
}
goneArrayLength = goneArrayLength + cStep ;
}
console.log(mirror + " " + symetric + " " + stepSymetric )
if(mirror && symetric && stepSymetric ){
console.log("Mirror Image");
}
else{
console.log("Not mirror image")
}
// console.log(isSymetric);
// console.log(totalSteps);
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def is_mirror(t1,t2):
# this is when I hit the leaf nodes
if t1 is None and t2 is None:
return True
# if node has only one child
if t1 is None or t2 is None:
return False
return t1.val==t2.val and is_mirror(t1.left,t2.right) and is_mirror(t1.right,t2.left)
return is_mirror(root.left,root.right)
I'm not very experienced (just a year at corporate), but in my opinion, this can be solved by using S=recursion
For example:
MYTREECLASS B1= new MYTREECLASS();
MYTREECLASS B2= new MYTREECLASS();
B1= OriginalTree;
B2=OriginalTRee;
Boolean CHECK(MYTREECLASS, MYTREECLASS)
{
if (B1.Node = B2.Node)
then (
CHECK(B1.Left, B2.Right);
CHECK(B1.Right,B2.Left)
)
elseIf(b1.Left==null or b2.right...blah blah ,,)
return False)
else return False,
Return true if it matches.

How do I modify the Min Heap insert and delete function to accept a second comparison if the primary comparison happens to be equal?

below I have a standard insert and delete function for a Min Heap, what I need to do is add a special case to both the functions when the T.num comparison happen to be equal, I then need to then compare the T.Letter where the lower Ascii value is popped first. Without the comments is the standard insert and delete, add the commented section would be my attempt to add the new feature, which, for the life of me, I don't understand why it wouldn't work.
void MinHeap<T>::insert(T& e)
{
int CurrNode = ++HeapSize;
while(CurrNode != 1 && heap[CurrNode/2].num >= e.num)
{
/*
if(heap[CurrNode/2].num == e.num)
if(heap[CurrNode/2].letter <= e.letter)
break;
*/
heap[CurrNode] = heap[CurrNode/2];
CurrNode /= 2;
}
heap[CurrNode] = e;
}
void MinHeap<T>::delet()
{
T LastNode = heap[HeapSize--];
int CurrNode = 1;
int child = 2;
while(child <= HeapSize)
{
if(child < HeapSize && heap[child].num >= heap[child+1].num)
{
/*
if(heap[child].num == heap[child+1].num)
if(heap[child].letter <= heap[child+1].letter)
child--;
*/
child++;
}
if(LastNode.num <= heap[child].num)
{
/*
if (LastNode.num == heap[child].num)
{
if (LastNode.letter <= heap[child].letter)
break;
}
else
*/
break;
}
heap[CurrNode] = heap[child];
CurrNode = child;
child *= 2;
}
heap[CurrNode] = LastNode;
}
You can simply overload the comparison operator for the T type, like this:
bool operator >(const T &left, const T &right) {
return left.num > right.num ||
left.num == right.num && left.letter <= right.letter;
}
And then replace heap[CurrNode/2].num >= e.num with heap[CurrNode/2] > e.
It is best to avoid code such as that in your commented sections because it can quickly get confusing to follow and debug.

Resources