Hi, I need some help please I am still learning java,How can i convert infix to posftix using BST?...so far this is what i have got.
Thank you in advance.
`package com.binarysearchtree.BSTNode;
class Node {
private Object item;
private Node next;
public Node(Object item,Node next) {
this.item = item;
this.next = next;
}
public Node() {
//this(0,null);
}
public Node(Object item) {
this(item,null);
}
public Object getItem(){
return item;
}
public Node getNext() {
return next;
}
public void setItem(int item) {
this.item = item;
}
public void setNext(Node next) {
this.next = next;
}
}
class InfixToPostfix{
Node top;
public InfixToPostfix(){}
public boolean isEmpty() {
return top == null;
}
public void push(Object item) {
Node temp = new Node(item, top);
top = temp;
}
public Object pop() {
Node temp = top;
top = top.getNext();
if(!this.isEmpty()){
temp.setNext(null);
}
return temp.getItem();
}
public Object peek() {
Object s = null;
if(!this.isEmpty()){
s = top.getItem();
}
return s;
}
public int priority(char c)
{
int p=999;
switch(c)
{
case '*':case '/': p=0; break;
case '+':case '-': p=1; break;
}
return p;
}
public String toString() {
StringBuffer sb = new StringBuffer();
if (!isEmpty()) {
Node p = top;
sb.append("[");
while (p != null) {
sb.append(p.getItem() + " ");
p = p.getNext();
}
sb.append("]");
}
return sb.toString();
}
}
public class BSTNode{
private int item;
private BSTNode left;
private BSTNode right;
InfixToPostfix infx = new InfixToPostfix();
public BSTNode (int item,BSTNode left, BSTNode right){
this.item = item;
this.left = left;
this.right = right;
}
public BSTNode(int item){
this(item,null,null);
}
public void preorder(){
//visit the root, do something on the item;
System.out.print(this.item+"");
if(left != null)
left.preorder();
if(right!= null)
right.preorder();
}
public void inorder(){
if(left !=null)
left.inorder();
System.out.print(this.item + " ");
if(right != null)
right.inorder();
}
public void postorder(){
if(left != null)
left.postorder();
if(right!= null)
right.postorder();
System.out.print(this.item+ "");
}
public String toString(){
return (left == null? " ": left.toString()) + this.item + " " + (right == null ? " " : right.toString());
}
public void insertV1(int item){
BSTNode temp = new BSTNode(item);
BSTNode p = this;
while(p != null){
if(item < p.item){
if(p.left == null){
p.left = temp;
infx.push(p.left);
break;
}
else{
p = p.left;
infx.push(p);
}
}
else{
if(p.right == null){
p.right = temp;
infx.push(p.right);
break;
}
else{
p = p.right;
infx.push(p);
}
}
}
}
public boolean containsV2(int item){
if(item == this.item)
return true;
else
if(item < this.item){
if(left == null)
return false;
else
return left.containsV2(item);
}
else{
if(right == null)
return false;
else
return right.containsV2(item);
}
}
public boolean containsV1(int item){
boolean found = false;
BSTNode p = this;
if(p.item == item)
found = true;
else{
while (p != null &&
item != p.item) {
if (item < p.item) {
p = p.getLeft();
}
else {
p = p.getRight();
}
}
if (p == null)
found = false;
else
found = true;
}
return found;
}
public void insertV2(int item) {
if (item < this.item) {
if (left == null){
this.setLeft(item);
infx.push(item);
}
else{
left.insertV1(item);
infx.push(item);
}
}
else
if (item > this.item) {
if (right == null){
this.setRight(item);
infx.push(item);
}
else{
right.insertV1(item);
infx.push(item);
}
}
}
public int findSmallest(){
BSTNode p = this;
while(p.left != null){
p = p.getLeft();
}
return p.item;
}
public int findLargest(){
BSTNode p = this;
int max = this.item;
while(p.right != null){
p = p.getRight();
}
return p.item;
}
public void setRight(int item) {
BSTNode temp = new BSTNode(item);
this.right = temp;
}
public void setLeft(int item) {
BSTNode temp = new BSTNode(item);
this.left = temp;
}
public void setItem(int item){
this.item = item;
}
public int getItem() {
return item;
}
public BSTNode getLeft() {
return left;
}
public BSTNode getRight() {
return right;
}
public static void main(String [] args){
InfixToPostfix in = new InfixToPostfix();
}
}`