简体   繁体   中英

converting binary tree to stack without duplicates

I have a binary tree of an object type InfoTree the object has 2 properties-number and character, but some characters can repeat themselves.My ultimate goal is to find the object with the character that has the biggest number and return it's character. for that I have a function to find the maximum number and return in recursively, but I still need to match number with character in a binary tree so I thought of converting the tree to a stack containing only characters but without duplicates here is where I have problems with. My different characters are : k,e,c,d but what I did changes the stack to [k,d,c]-without e and basically doesn't work.

//input : The function gets a stack and a character
//output : The function returns true if the character given is in the stack
public static boolean isInStack(Stack<Character> s,char ch){
    while(!s.isEmpty()){
        if(s.pop()==ch)
            return true;
    }
    return false;
}

 //The function puts all different characters in a stack
public static void stack(BinTreeNode<InfoTree> t,Stack<Character> s){
    s.push(t.getValue().getTav());
    if(t!=null){
        if(t.getLeft()!=null&&t.getRight()!=null){
            if(!isInStack(s,t.getValue().getTav())){
                s.push(t.getValue().getTav());
            }
            stack(t.getLeft(),s);
            stack(t.getRight(),s);
        }
        else if(t.getLeft()!=null)
        {
            if(!isInStack(s,t.getValue().getTav())){
                s.push(t.getValue().getTav());
            }
            stack(t.getLeft(),s);
        }
        else if(t.getRight()!=null)
        {
            if(!isInStack(s,t.getValue().getTav())){
                s.push(t.getValue().getTav());
            }
            stack(t.getRight(),s);
        }
    }
}

the tree is :

//input : none
//output : The function builds  and returns a tree
public static BinTreeNode<InfoTree> buildtree(){
    BinTreeNode<InfoTree> t1=new BinTreeNode<InfoTree>(new BinTreeNode<InfoTree>(new InfoTree('k',1)),new InfoTree('k',3),new BinTreeNode<InfoTree>(new InfoTree('k',0)));
    t1.getLeft().setLeft(new BinTreeNode<InfoTree>(new InfoTree('k',0)));
    t1.getLeft().setRight(new BinTreeNode<InfoTree>(new InfoTree('k',0)));
    t1.getLeft().getRight().setLeft(new BinTreeNode<InfoTree>(new InfoTree('e',0)));
    t1.getLeft().getRight().getLeft().setRight(new BinTreeNode<InfoTree>(new InfoTree('d',0)));

    t1.getRight().setLeft(new BinTreeNode<InfoTree>(new InfoTree('c',1)));  
    t1.getRight().getLeft().setRight(new BinTreeNode<InfoTree>(new InfoTree('c',0)));
    return t1;
}

I'll be really thankful for the help!

A stack is a horrible data structure for collecting a set. Imagine you have a stack with element A,B,C where A is the top of stack and in your code you have another C. Your code will stop after popping all of the elements and return true that the element was there, but your stack will now have zero elements in it. Was that the last node of the tree that would be the result. That you have 3 out of 4 elements after your run is share luck.

The correct data structure would be a Set:

Set<Character> setA = new HashSet<Character>();
setA.add('a');
setA.add('a');
setA.add('b');
setA.add('b');
setA.contains('a'); // ==> true
Character[] result = setA.toArray(new Character[setA.size()]);

Basically you don't need to check if you've added it before since it takes care of it. In the end you'll have the unique chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM