Josh Susa
Josh Susa

Reputation: 173

How do I do a pre-order traversal in Java?

I'm attempting to write a pre-order traversal algorithm on a binary tree using the recursive method. Here's what I have:

void traverse(BT t) {
        if (t == null){
            return;
        }

        System.out.print(t);  
        traverse(t.left);
        traverse(t.right);
        }

That doesn't compile for some reason. I think the problem is with the rest of my code. Here's the entire code:

class ZOrep extends TreeAndRepresentation {
  private int k;
  ZOrep left;  
  ZOrep right;  
  ZOrep( int m, int[] b ) { // given sequence build tree
     super( m, b );
     N = (M-1)/2;
     k  = -1;
     t = build();
    }
  ZOrep( int n, BT t ) { // given tree build sequence
      super(n, t);
      t = build();
      traverse( t );
    }
  BT build() {
      return(a[++k] == 0 ? null : new BT( build(), build() ));
    }

  void traverse(BT t) {
    if (t == null){
        return;
    }

    System.out.print(t);  
    traverse(t.left);
    traverse(t.right);
    }
}

I feel like I'm missing something when I'm building the tree (with my ZOrep method). Also here's the BT class:

class BT {
  BT L; BT R;
  BT( BT l, BT r ) { L = l; R = r; }
}

Currently my compiler says it can't find the symbol for t.left and t.right.

Upvotes: 0

Views: 580

Answers (4)

vlapas
vlapas

Reputation: 51

// Java

static String tree = "";

private static void preOrder(HuffTree currentObject) {

    if (currentObject == null) {
        return;
    }
    if (currentObject.filling == null) tree += 1;
    else tree += 0;
    preOrder(currentObject.child0);
    preOrder(currentObject.child1);
}

}

// class code here

import java.util.Objects;

/**

  • Huffman tree as class */

class HuffTree implements Comparable {

// element filling
Byte filling;
// element repeats
int repeats;
// zero child
HuffTree child0;
// child 1
HuffTree child1;

/**
 * constructor for tree fathers and leaves
 */
public HuffTree(Byte filling, int repeats, HuffTree child0, HuffTree child1) {
    // father filling
    this.filling = filling;
    // father repeats
    this.repeats = repeats;
    // zero child
    this.child0 = child0;
    // child 1
    this.child1 = child1;
}

/**
 * finding difference between our tree's items
 */
@Override
public int compareTo(HuffTree currentByte) {
    return currentByte.repeats - repeats;
}


/**
 * take byte code as a string by recursive three search in depth
 */
public String getCodeForByte(Byte currentByte, String wayToFather) {
    // there is 4 cases:
    if (!Objects.equals(filling, currentByte)) {
        // case 1 - zero child found
        if (child0 != null) {
            // recursive code add for zero child
            String currentWay = child0.getCodeForByte(currentByte, wayToFather + "0");
            // return temporary string
            if (currentWay != null) return currentWay;
        }
        // case 2 - child 1 found. recursive code add for child 1. return temporary string
        if (child1 != null) return child1.getCodeForByte(currentByte, wayToFather + "1");
    }
    // case 3 - correct leaf found. return correct code
    if (Objects.equals(filling, currentByte)) return wayToFather;
    // case 4 - wrong leaf found. return null
    return null;
}

}

Upvotes: 0

DonutGaz
DonutGaz

Reputation: 1542

What are you passing into your transverse function? If it's a BT object, then you can't use left and right, you must use L and R. Left and right are parts of your object that extends from BT, but it looks like you're passing in a BT.

Upvotes: 0

Mshnik
Mshnik

Reputation: 7032

When the compiler says it can't find the symbol, it means the field you're trying to reference doesn't exist.

Looking at your class BT, this is correct; BT doesn't have left or right, it has L and R. Thus, replacing

traverse(t.left);
traverse(t.right);

with

traverse(t.L);
traverse(t.R);

Will fix this issue.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533482

Currently my compiler says it can't find the symbol for t.left and t.right.

This is because t is a BT and it doesn't have a left and a right.

I suggest you decide what you want to call your tree node class. Is it ZOrep or BT and only use one of these or you will create confusion.

System.out.print(t); 

If you want to print out a BT, you will need to add a toString() method to it as the default won't tell you anything useful.

Upvotes: 1

Related Questions