How to convert following into a Binary Tree? - data-structures

What is the procedure to convert following into a binary tree? And what will be the output/answer ? A+(B+C+D+E)+F/G

A+(B+C+D+E)+F/G
result
|
A
A+(B+C+D+E)+F/G
result
| \
A obj1
A+(B+C+D+E)+F/G
result
| \
A obj2
/ \
obj1 F/G(could be linked to A for load balancing)
(B+C+D+E)
result
| \
A obj2
/ \
obj1 F/G
/
B
(B+C+D+E)
result
| \
A obj2
/ \
obj1 F/G
/ \
B C
(B+C+D+E)
result
| \
A obj2
/ \
obj3 F/G
| \
obj1 D
/ \
B C
(B+C+D+E)
result
| \
A obj2
/ \
obj4 F/G
| \
obj3 E
| \
obj1 D
/ \
B C
F/G
result
| \
A obj2
/ \
obj4 obj5
| \ |
obj3 E F
| \
obj1 D
/ \
B C
F/G
result
| \
A obj2
/ \
obj4 obj5
| \ | \
obj3 E F G
| \
obj1 D
/ \
B C
once tree is complete, start computing from bottom to top(all leafs) then all branchs whose leafs are complete.
B+C = obj1
B+C+D =obj3
B+C+D+E =obj4
F/G = obj5
(B+C+D+E)+F/G = obj2
A+(B+C+D+E)+F/G = result

Related

I would like to concatenate two figlet outputs (with different colors)

Currently I have this kind of output format:
{ echo "$(figlet buddhi)"; echo "$(figlet lw)"; }
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
And I would like to have this output format:
figlet buddhi lw
_ _ _ _ _ _
| |__ _ _ __| | __| | |__ (_) | |_ __
| '_ \| | | |/ _` |/ _` | '_ \| | | \ \ /\ / /
| |_) | |_| | (_| | (_| | | | | | | |\ V V /
|_.__/ \__,_|\__,_|\__,_|_| |_|_| |_| \_/\_/
The reason is: I would like to color each name (buddhi, lw) with a different color. But, retain the format of a continuous string, or at maximum space-separated, as above.
Example:
#COMMANDS CREATED INSIDE /ETC/BASH.BASHRC FILE
# USING ANSI COLORS
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
printf "${GREEN}"
printf "=================================\n"
printf "${ORANGE}"
figlet -f standard "Buddhi"
printf "${BLUE}"
figlet -f small "LW"
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
Store the lines of each word in arrays, output both the arrays line by line. As the first line of "Buddhi" seems to be one character shorter, I stored the longest line length of the first word in a variable, and used the %-s format to pad each line.
#! /bin/bash
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
mapfile -t left < <(figlet -f standard "Buddhi")
mapfile -t right < <(figlet -f small "LW")
maxlength=0
for line in "${left[#]}" ; do
if (( ${#line} > maxlength )) ; then
maxlength=${#line}
fi
done
printf "${GREEN}"
printf "=================================\n"
for ((i=0; i<=${#left[#]}; ++i)) ; do
printf "${ORANGE}%-${maxlength}s ${GREEN}%s\n" "${left[i]}" "${right[i]}"
done
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
If you need a shorter version:
printf "$GREEN=================================\n"
{ figlet Buddhi; echo 'EOF'; figlet LW; } | awk 'NF==1&&$1=="EOF" {noskip=1; next; } noskip==0 { f[++c]=$0; next; } { printf "%s%s%s%s\n","'"$ORANGE"'",f[++k],"'"$BLUE"'",$0;}'
printf "$GREEN=================================\n"
tput sgr0
I would recommend to use tput for setting the color as not every terminal will know your escape sequences
The guys who invented shell also invented awk for shell to call to manipulate text. Those escape sequences don't change colors on my terminal, they just show up as-is (fortunately so you can see where the script is puting them):
$ cat tst.sh
#!/usr/bin/env bash
awk '
BEGIN {
red = "\\e[31m"
orange = "\\e[33m"
blue = "\\e[94m"
green = "\\e[92m"
stop = "\\e[0m"
}
{
val[(NR==FNR),FNR] = $0
}
NR == FNR {
wid = length($0)
maxWid = ( wid > maxWid ? wid : maxWid )
}
END {
for ( lineNr=1; lineNr<=FNR; lineNr++ ) {
printf "%s%-*s%s%s%s\n", orange, maxWid, val[1,lineNr], blue, val[0,lineNr], stop
}
}
' <(cat Buddhi) <(cat LW)
$ ./tst.sh
\e[33m _ _ _ _ _ \e[94m _\e[0m
\e[33m| |__ _ _ __| | __| | |__ (_)\e[94m| |_ __\e[0m
\e[33m| '_ \| | | |/ _` |/ _` | '_ \| |\e[94m| \ \ /\ / /\e[0m
\e[33m| |_) | |_| | (_| | (_| | | | | |\e[94m| |\ V V /\e[0m
\e[33m|_.__/ \__,_|\__,_|\__,_|_| |_|_|\e[94m|_| \_/\_/\e[0m
Since I don't have figlet, I ran the above on these files:
$ head Buddhi LW
==> Buddhi <==
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
==> LW <==
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
Just change the last line of the script from:
' <(cat Buddhi) <(cat LW)
to
' <(figlet Buddhi) <(figlet LW)
to use actual figlet output.
the above assumes you only have 2 figlet output strings to concatenate and that both sets of output are the same length, it's easy tweaks if either of those assumptions is wrong.
In lieu of figlet I'll use the following as my inputs:
$ cat buddhi
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
$ cat lw
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
Assuming figlet generates the same number of output lines for each input string, we can use paste (# as a delimiter) and a while/read loop to generate the desired output:
printf "${GREEN}"
printf "============================\n"
maxwidth=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' buddhi)
while IFS='#' read -r col1 col2
do
printf "${ORANGE}%-*s ${BLUE}%s\n" "${maxwidth}" "${col1}" "${col2}"
done < <(paste -d"#" buddhi lw)
printf "${GREEN}"
printf "============================\n"
This generates:
Expanding to 3 input streams:
printf "${GREEN}"
printf "============================\n"
max1=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' buddhi)
max2=$(awk '{max=length($0) > max ? length($0) : max}END{print max}' lw)
while IFS='#' read -r col1 col2 col3
do
printf "${ORANGE}%-*s ${BLUE}%-*s ${RED}%s\n" "${max1}" "${col1}" "${max2}" "${col2}" "${col3}"
done < <(paste -d"#" buddhi lw buddhi)
printf "${GREEN}"
printf "============================\n"
This generates:
Using coordinates
#!/bin/bash
RED='\e[31m'
GRN='\e[32m'
XY(){ printf "\e[$2;${1}H$3"; }
mapfile -t frst < <(figlet -f standard "Buddhi")
mapfile -t scnd < <(figlet -f small "LW")
XY 1 1 "$GRN==============================================="; y=2
for line in "${frst[#]}"; { XY 0 $y "$RED$line"; ((y++)); }; y=2
for line in "${scnd[#]}"; { XY 35 $y "$GRN$line"; ((y++)); }
XY 1 8 "$GRN==============================================="
More examples here, here and here

How to print ASCII picture/logo in console using bash

I have a logo which I want to print/echo using bash in console, through scripts, but I can't even paste here properly(looks good inside .txt file):
___ _____ _ _ _____ _ _ ___
| _| / ___| | | || ___| \ | | |_ |
| | \ `--.| |_| || |__ | \| | | |
| | `--. \ _ || __|| . | | |
| | /\__/ / | | || |___| |\ | | |
| |_ \____/\_| |_/\____/\_| \_/ _| |
|___| |___|
How I can do that properly and make it colour without 3rd party extensions?
Also, is it possible to print it from the file?
You may use tput:
tput setaf 2; cat logo.txt
2 is the color code here (red). These are the available codes:
Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color

Trouble making ascii art in .bashrc file

I get these errors when trying to use: export PS1='ascii text at bottom'.
bash: command substitution: line 1: syntax error near unexpected token |'
bash: command substitution: line 1:/ ^ \ | |_) |'
bash: command substitution: line 3: syntax error near unexpected token |'
bash: command substitution: line 3: \ /\ / / _____ \ | |\ ---) |'
Could someone explain why this doesn't work?
________________. ___ .______
/ | / \ | _ \
| (-----| |----`/ ^ \ | |_) |
\ \ | | / /_\ \ | /
.------) | | | / _____ \ | |\ \-------.
|_________/ |__| /__/ \__\| _| `.________|
____ __ ____ ___ .______ ________.
\ \ / \ / / / \ | _ \ / |
\ \/ \/ / / ^ \ | |_) || (-----`
\ / / /_\ \ | / \ \
\ /\ / / _____ \ | |\ \---) |
\__/ \__/ /__/ \__\|__| `._______/
PS1 is not just a plain string; in it, certain characters have meaning to them. You can find a surprisingly long document telling you more about bash prompts than you ever wanted to know here; it's a bit dated, but most of it should still hold. The particular problem you run into is that backslashes and backticks need to be escaped -- backslashes because they're used to escape things, and backticks because they're used for command substitutions -- and newlines are best replaced with the \n control sequence to avoid mishaps with lines that end in \. Ultimately, you want the effect of writing
export PS1=' ________________. ___ .______ \n / | / \\ | _ \\\n | (-----| |----\`/ ^ \\ | |_) |\n \\ \\ | | / /_\\ \\ | /\n.------) | | | / _____ \\ | |\\ \\-------.\n|_________/ |__| /__/ \\__\\| _| \`.________|\n____ __ ____ ___ .______ ________.\n\\ \\ / \\ / / / \\ | _ \\ / |\n \\ \\/ \\/ / / ^ \\ | |_) || (-----\`\n \\ / / /_\\ \\ | / \\ \\\n \\ /\\ / / _____ \\ | |\\ \\---) |\n \\__/ \\__/ /__/ \\__\\|__| \`._______/'
...which is somewhat unwieldy. A way to do this on the fly so that it is easier on the eyes is
PS1=' ________________. ___ .______
/ | / \ | _ \
| (-----| |----`/ ^ \ | |_) |
\ \ | | / /_\ \ | /
.------) | | | / _____ \ | |\ \-------.
|_________/ |__| /__/ \__\| _| `.________|
____ __ ____ ___ .______ ________.
\ \ / \ / / / \ | _ \ / |
\ \/ \/ / / ^ \ | |_) || (-----`
\ / / /_\ \ | / \ \
\ /\ / / _____ \ | |\ \---) |
\__/ \__/ /__/ \__\|__| `._______/'
PS1="${PS1//\\/\\\\}"
PS1="${PS1//\`/\\\`}"
PS1="${PS1//
/\\n}"
export PS1 # <-- this isn't really necessary, by the way. PS1 is not
# usually used by processes the shell spawns.
Here ${variable//pattern/replacement} is a bash-specific parameter expansion that expands to the value of $variable with all occurrences of pattern replaced with replacement. That is to say:
PS1="${PS1//\\/\\\\}" # replaces \ with \\
PS1="${PS1//\`/\\\`}" # replaces ` with \`
PS1="${PS1// # replaces newlines with \n
/\\n}"

How to construct binary tree from algebraic expression

In my exam I got this expression and failed to construct binary tree. what would be the solution?
(5a-3b)^2*(3a+5b)^3
I drew the tree from above expression like
*
/ \
/ \
^ \
/ \ \
/ \ \
- 2 ^
/ \ / \
/ \ + 3
* * / \
/ \ / \ / \
5 a 3 b * *
/ \ / \
3 a 5 b
I got 0 from this answer! I could not understand the reason!
Can anybody help me?
Constructing a binary tree doesn't mean that you just draw the graphical representation which you have shown here.
I think It actually means to implement in code or may in pseudo code.
So think again using stack with the same expression
Convert the infix notation to postfix and you can solve it easily by using stack property.
I am currently taking a discrete mathematics course and we are on the subject of trees. You have to remember to put each level in its own (). So I believe the tree you created would look like this as an expression: ((5*a)-(3*b))^2*((3*a)+(5*b))^3. This is an Inorder traversal of the tree. For your answer you needed to have something more like this:
*
/ \
/ \
/ \
^ \
/ \ \
/ \ \
- 2 ^
/ \ / \
/ \ / 3
5a 3b +
/ \
/ \
3a 5b

Logic: is ( A && !(B || C)) || ( B || C ) the same as ( A || B || C )?

I've encountered some obj-c code and I'm wondering if there's a way to simplify it:
#if ( A && !(B || C)) || ( B || C )
is this the same as?
#if ( A || B || C )
If not, is there another way to formulate it that would be easier to read?
[edit]
I tried the truth table before asking the question, but thought I had to be missing something because I doubted that Foundation.framework/Foundation.h would employ this more complex form. Is there a good reason for it?
Here's the original code (from Foundation.h):
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
Yes. Like others said, you can truth table it. The De Morgan rules can also help.
However, I think the best option is to use a Karnaugh Map. It takes a few minutes to learn, but Karnaugh Maps allow you to consistently find the most minimal expression for boolean logic. Truth tables can verify a minimization, but they can't give it to you.
Here's how I got it:
First, the table layout:
AB
00 01 11 10
0| | | | |
C 1| | | | |
Now, considering your equation, B || C will always cause a truth:
AB
00 01 11 10
0| | T | T | |
C 1| T | T | T | T |
This leaves only two cases. In either case, the right side evaluates to false. For 000, the left side also evaluates to false (0 && !(whatever) is false). For 100, 1 && !(0 ||| 0) evaluates to true. Thus, the statement is true. Filling in:
AB
00 01 11 10
0| F | T | T | T |
C 1| T | T | T | T |
Now, we only need to "cover" all the truths. "C" will cover the bottom row. "B" will cover the middle square (of four values). Thus, "B || C" covers all but the top right square. Now, "A" will cover the right four-space square. It's OK that this is redundant. Thus, "A || B || C" covers all the true squares and omits the only false one.
Get pen + paper + try it, there are only 8 possible inputs
They are the same. You can use Truth Table Generator to test it. Both these expressions give false only in one case, when A, B and C are false.
A | B | C | (B || C) | (!(B || C)) | (A && !(B || C)) | (A && (!(B || C)) || (B || C) | (A || B || C)
------------------------------------------------------------------------------------------------------
T | T | T | T | F | F | T | T
T | T | F | T | F | F | T | T
T | F | T | T | F | F | T | T
T | F | F | F | T | T | T | T
F | T | T | T | F | F | T | T
F | T | F | T | F | F | T | T
F | F | T | T | F | F | T | T
F | F | F | F | T | F | F | F
Based on the last two columns, I would say yes.
Yes it is the same. Using De Morgan rules:
(A && !(B || C)) || (B || C) = (A && !B && !C) || (B || C).
So the second will be true when A = 1 and B, C = 0. If that is not the case the second part (B || C) will be true when B || C. So it is equal to the first.
You could also say :
(A && !(B || C)) || (B || C) rewrites to (A && !W) || W (1)
(1) rewrites to (A && !W) || (A || !A || W) (2)
(2) rewrites (A && !W) || (A || W) || (!A || W) (3)
(3) rewrites (A && !W) || !(A && !W) || (A || W) (4)
(4) leads to A || W and then A || B || C
Yes, the two expressions are equivalent. (I just wrote a couple of functions to test all eight possibilities.)

Resources