Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# convert given String pattern into a binary tree using java.

Posted on 2007-10-02
Medium Priority
914 Views
Last Modified: 2012-05-05
( ( ( ! D ! ) B ( ( ! F ! ) E ( ! G ! ) ) A ( ( ! H I ) C ( ! I ! ) ) )
where ! means empty tree
i need to convert this String pattern into a binary tree using java.
A
/         \
B            C
/   \          /  \
D    E       H  I
/ \
F  G
0
Question by:pras_gupta
9 Comments

LVL 86

Assisted Solution

CEHJ earned 300 total points
ID: 20004274
Try using a parser generator: http://en.wikipedia.org/wiki/JavaCC

The grammar may need to be converted first
0

LVL 1

Author Comment

ID: 20006764
Thanks but i need a simple soultion in Java .. if you can share some code that will be very helpful..
0

LVL 20

Accepted Solution

gatorvip earned 1050 total points
ID: 20008145
You will need to conceptualize the input string. If your example shows the full grammar, then the logic goes like this:
- The character "(" signifies a new node and it can be followed by either another ( or by a "!"
- The character "(!" signifies a new leaf node and it is followed by 1 alphabetical character then by another "!" and by the closing char
- The character ")" signifies the end of a node

Use a stack to hold nodes as you are parsing
0

LVL 1

Author Comment

ID: 20008214
The example shows full grammer ..can you share bits of Code on how to do this..
0

LVL 20

Assisted Solution

gatorvip earned 1050 total points
ID: 20008224
fwiw, that string given above is not correct. You have an extra "(" and "( ! H I )" should be " ( ! H ! ) "
0

LVL 1

Author Comment

ID: 20009012
yeah right ...
0

LVL 20

Assisted Solution

gatorvip earned 1050 total points
ID: 20009057
Not sure what you mean by that. In your given string,

( ( ( ! D ! ) B ( ( ! F ! ) E ( ! G ! ) ) A ( ( ! H I ) C ( ! I ! ) ) )

you have 9 "(" and only 8 ")" - perhaps you can count these and verify for yourself.

Also, H should be followed by a " ! " not by " | "

Insofar as the actual code is concerned, what have you tried so far? We cannot do homework for you, but if you show work we can point out clues, mistakes, etc.
0

LVL 4

Assisted Solution

TheMajestic earned 150 total points
ID: 20012770
0

LVL 1

Author Comment

ID: 20015097
Thanks gatorvip: for opening my eyes .. I finally did it myself.. was getting lazy i suppose
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define â€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
###### Suggested Courses
Course of the Month12 days, 6 hours left to enroll

#### 564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.