2

I have created a binary search tree with {2,5,3,4,9,1,7,...,100} digits.

how can i save it as preorder? thanks

EDIT: Consider I have { 3,7,1,2} and make a binary search tree with these digits and I want to save this tree as preorder which is {3,1,2,7}

user472221
  • 3,014
  • 11
  • 35
  • 42
  • 1
    How do you mean "save it as preorder?" – Goran Jovic Dec 27 '10 at 14:47
  • Is their a horde of you with the same homework question today? See [Pre-order to post-order traversal](http://stackoverflow.com/questions/4537969/pre-order-to-post-order-traversal), although your question can't actually be solved unless the numbers given are in post-order. – moinudin Dec 27 '10 at 14:51
  • @user Referring to your edit, there are many valid BST structures for a given list of values. So you don't have enough information to construct the pre-order traversal. – moinudin Dec 27 '10 at 14:58

2 Answers2

5

Seeh here on literateprograms :

public List<E> toList() {
    List<E> result = new ArrayList<E>();
    treeToList(root, result);
    return result;
}

private void treeToList(Node<E> node, List<E> goal) {
    if (node != null) {
        treeToList(node.left, goal);
        goal.add(node.value);
        treeToList(node.right, goal);
    }
}

A complete article on binary trees and PreOrder traversals.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • I don't think OP actually has the tree structure to begin with. – moinudin Dec 27 '10 at 14:56
  • I have another question that when I make a binary search tree with 100 digits how can i save that BST and read it as preorder? – user472221 Dec 27 '10 at 15:07
  • @user Read http://stackoverflow.com/questions/4537969/pre-order-to-post-order-traversal . You can do this by storing the pre-order or post-order traversal, from which you can construct the original tree. – moinudin Dec 27 '10 at 15:11
  • thanks I get it but I have another question that i will ask it with other topic. – user472221 Dec 27 '10 at 15:23
0

The following algorithm prints all the nodes, one per line, of the subtree of the node at v in the binary tree bt1.

  public static void preorderPrintLines (BinaryTree bt1, BNode v) { 
          String s = bt1.elementAt(v).toString(); 
                             //the class for the element in the node 
                             //needs to have a toString() method 
          System.out.println (s);   // subtree root element printed

           if (bt1.isInternal(v))   { 
              BNode       p;   
               if (bt1.hasLeft(v)) { 
                                  p  = bt1.left(v); 
                                 preorderPrintLines (bt1, p ); }   
                            //We have just traversed tree of left child 
              if (bt1.hasRight(v)) { 
                                  p  = bt1.right(v); 
                                  preorderPrintLines (bt1, p );  }    
                  //We have just traversed tree of right child 
              } //end if 
}

source : here

Bobj-C
  • 5,276
  • 9
  • 47
  • 83