Java trees examples
<p>Hi I would like to know how to reverse a string by words , can someone tell me why my code is not compileing?</p>

<p>I will explain in this article how to reverse a given string in a Windows console application using C# without using a function. This type of question might be asked by an interviewer in a .Net position related interview.</p>

<p>My intent for this article is to explain how to answer a question that is often asked in an interview, which is: Write a program to reverse the given String without using the string function. A candidate new to the interview can become totally confused because the first problem is the candidate just thinks about functions, not other techniques to reverse a string.</p>

<p>So considering the preceding requirement and numerous emails sent to me from beginners I have decided to write this article with the basics and specially focusing on the beginner, student and whoever might face this type of question in an interview.</p>

<p>So let us start with the basics.</p>

<p>What is a String ?</p>

<pre><code>http://code.google.com/p/myhibernateproject/source/browse/trunk/src/citi/CharAuditorImpl.java?r=134

http://code.google.com/p/software-engineering-excercise-repository/source/browse/trunk/seer_java/src/common/tree/PrintBoundary.java?r=23

http://code.google.com/p/software-engineering-excercise-repository/source/browse/trunk/seer_java/src/common/tree/SerializeTree.java?r=23




// Here is my code
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class PrintTree {

    /**
     * 
     *        1
     *    2       3
     * 4    5   6    
     * 
     * 
     * 
     * 1#1#2;2#2#2;4#4#0;5#5#0;6#3#1
     * 
     * 
     */
    public static void main(String[] args) {

        TreeNode tn6 = new TreeNode("6", "6", null);
        TreeNode tn5 = new TreeNode("5", "5", null);
        TreeNode tn4 = new TreeNode("4", "4", null);

        List&lt;TreeNode&gt; list_1 = new ArrayList&lt;TreeNode&gt;();
        list_1.add(tn4);
        list_1.add(tn5);

        List&lt;TreeNode&gt; list_2 = new ArrayList&lt;TreeNode&gt;();
        list_2.add(tn6);

        TreeNode tn2 = new TreeNode("2", "2", list_1);
        TreeNode tn3 = new TreeNode("3", "3", list_2);

        List&lt;TreeNode&gt; list_0 = new ArrayList&lt;TreeNode&gt;();
        list_0.add(tn2);
        list_0.add(tn3);

        TreeNode tn1 = new TreeNode("1", "1", list_0);


        StringBuilder sb = new StringBuilder();
        serializeTree(tn1 , sb);
        System.out.println(sb.toString());

        StringTokenizer st = new StringTokenizer(sb.toString(), TreeNode.NODE_DELIMITER);
        TreeNode deserialize_tree = deserializeTree(st);
        StringBuilder sb1 = new StringBuilder();
        serializeTree(deserialize_tree , sb1);
        System.out.println(sb1.toString());

    }

    private static TreeNode deserializeTree(StringTokenizer st) {
        if(!st.hasMoreTokens()){
            return null;
        }

        String nextToken = st.nextToken();

        String[] attributes = nextToken.split(TreeNode.ATTRIBUTE_DELIMITER);
        TreeNode node = new TreeNode(attributes[0], attributes[1], null);
        for(int i = 0 ; i &lt; Integer.parseInt(attributes[2]) ; i++){
            node.addChild(deserializeTree(st));
        }



        return node;
    }

    private static void serializeTree(TreeNode tn , StringBuilder sb) {

        sb.append(tn.toString());
        if(tn.childs != null){
            for(TreeNode tnItem : tn.childs){
                serializeTree(tnItem , sb);
            }
        }

    }


    public class reverseString {

        /**
         * @param args
         */
        public static void main(String[] args) {
            String str = "i am the man";
            System.out.println(reverseByChar(str));
            System.out.println(reverseByWord(str));
        }

        private static String reverseByWord(String str) {
            if(str == null ){
                return str;
            }

            StringTokenizer tokenizer = new StringTokenizer(str);

            return reverseByWordImpl(tokenizer);
        }


        private static String reverseByWordImpl(StringTokenizer tokenizer ) {
            if(tokenizer == null || !tokenizer.hasMoreTokens()){
                return "";
            }
            String nextToken =  tokenizer.nextToken() + " ";
            return  reverseByWordImpl(tokenizer) +  nextToken;

        }

        private static String reverseByChar(String str) {
            if(str ==null || str.length() ==1){
                return str;
            }

            return   reverseByChar(str.substring(1))  + str.charAt(0);
        }


        private static void mergeSort() {
            System.out.println("========mergeSort");
            List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
            list.add(4);
            list.add(2);
            list.add(3);
            list.add(1);

            List&lt;Integer&gt; mergedList = mergeSortImpl(list);
            for(int k = 0 ; k &lt; mergedList.size() ; k++){
                System.out.println(mergedList.get(k));
            }

        }

        private static List&lt;Integer&gt; mergeSortImpl(List&lt;Integer&gt; list) {
            if(list == null ||list.size() &lt; 2){
                return list;
            }

            int mid = list.size() / 2;
            List&lt;Integer&gt; list_low = mergeSortImpl(list.subList(0, mid));
            List&lt;Integer&gt; list_high = mergeSortImpl(list.subList(mid, list.size()));
            List&lt;Integer&gt; mergedList = mergeLists(list_low , list_high);
            return mergedList;
        }





        private static List&lt;Integer&gt; mergeLists(List&lt;Integer&gt; list_low,
                List&lt;Integer&gt; list_high) {
            List&lt;Integer&gt; mergedList = new ArrayList&lt;Integer&gt;();
            int counter_low = 0, counter_high = 0;
            while(counter_low &lt; list_low.size() || counter_high &lt; list_high.size()){

                if (    (list_low.size() != counter_low) &amp;&amp;
                        ((list_high.size() == counter_high)||
                          list_low.get(counter_low) &lt; list_high.get(counter_high))){
                    mergedList.add(list_low.get(counter_low));
                    counter_low++;
                }else if(list_high.size() != counter_high){
                    mergedList.add(list_high.get(counter_high));
                    counter_high++;
                }
            }

            return mergedList;
        }


        private static LinkedListNode reverseList(LinkedListNode head) {

            if(head == null || head.next == null){
                return head;
            }


            LinkedListNode next = head.next;
            head.next = null;
            LinkedListNode node  = reverseList(next);
            head.next = next.next;
            next.next = head;
            return node;


        }

}
</code></pre>
