Link to home
Start Free TrialLog in
Avatar of jmkotman
jmkotmanFlag for United States of America

asked on

BST In Order Traversal

I have written code to input data into a BST.  This data after being placed in the tree is shown below.  I would like to print this data in order.  After making the tree on paper i noticed that the path we need to take is Left pointer, parent, right pointer.  What is the best way to do this?
microsoft        3989898989 02 03 
java             1357357357 05 12 
zipinfo          0490080000 04 28 
pizzahut         3789789789 20 07 
google           3333333333 06 08 
cnn              1444444444 21 09 
yahoo            2121212121 11 00 
ilounge          4040404040 18 00 
dictionary       0555555555 31 10 
expedia          1234543210 00 17 
un               3939393939 13 14 
limewire         1010101010 22 00 
quicken          4294967295 00 15 
verizon          2468246824 00 16 
secondlive       1888888888 25 26 
wikipedia        4242424242 00 19 
facebook         1616161616 00 29 
howstuffworks    0000000001 00 00 
xe               0123123123 00 00 
nytimes          1234554321 27 23 
barnesandnoble   1111111111 24 00 
kalamazoocity    2444666888 00 00 
oprah            2999999999 00 00 
amazon           2222222222 00 30 
radioshack       4294000000 00 00 
tripadvisor      0777777777 00 00

Open in new window

Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

The easiest way uses a recursive function to walk the nodes.  It would be easier to explain if I saw your data structure, but here's some pseudo-code to get started.  Pass the root node to PrintTree to print the entire tree or any child node to print just that node and all descendants.

Function:  PrintTree(node_pointer)
1. If node_pointer is null, return.
2. Call PrintNode(node_pointer.left_pointer).
3. Print the current node.
3. Call PrintNode(node_pointer.right_pointer).

That should do it.
Avatar of jmkotman

ASKER

The data for the BST was attached to this question.  If you look at the BST, Microsoft is the root of the tree and then the left and right child pointers point to other nodes.
I understand that.  What part of my answer are you having trouble with?  This is a standard BST search algorithm.
I am able to find the last node of the tree by following the left pointer until it equals 00.  Once i get to there i know i need to write the parent node and then the right node.  The trouble i have is, when i get to cnn lets say, i have multiple nodes off of that which i have to write.  Then after that i have to come back to cnn and write the parent node of that.  The code that i am writing for this isn't working out.
Did you read my post about creating a recursive function to handle this?  The beauty of using a recursive function is that you don't need to follow any path, the function is smart enough to find all nodes and print them in order.  It works every time.  If you post some of your source code, I'll help you create one.  I need to know how you are storing the nodes.
Below is the code that i use to create the BST.  A line is read in from a file and inputed into a string[,] BSTHold.  Once this is done, it ouputs the array i placed in my original question.  Let me know if this helps, thanks a lot!
public static void Create(ref int nextEmpty, ref int rootPtr, ref string url, ref string ipAddress, 
            ref int Incrementer, ref int Incrementer2, ref string data, ref int N, ref StreamWriter WriteToLog, 
            ref int leftChPtr, ref int rightChPtr, ref string[,] BSTHold, ref StreamReader ReadInFile, ref int menuchoice, ref int NodeCount)
        {
 
            data = ReadInFile.ReadLine();//Reads first line of file
 
            for (int i = 0; i < 16; i++)//Reading the first 16 characters of the data string
            {
                url += data[i];
            }
 
            for (int j = 17; j < 27; j++)//Reading the second 10 characters of the data string
            {
                ipAddress += data[j];
            }
 
            N++;
 
            while (data != null)
            {
                InsertInBST(ref rootPtr, ref BSTHold, ref nextEmpty, ref url, ref ipAddress, 
                    ref Incrementer, ref Incrementer2, ref rightChPtr, ref leftChPtr, ref N, ref data, ref ReadInFile, ref menuchoice, ref NodeCount);
            }
 
            WriteToLog.WriteLine("*Create BST Done: {0} BST nodes, root is at node 1", N.ToString("d2"));
        }
 
 
        public static void InsertInBST(ref int rootPtr, ref string[,] BSTHold, ref int nextEmpty, ref string url, ref string ipAddress,
            ref int Incrementer, ref int Incrementer2, ref int leftChPtr, ref int rightChPtr, ref int N, ref string data, ref StreamReader ReadInFile, ref int menuchoice, ref int NodeCount)
        {
            if (rootPtr == 0)
            {
 
                BSTHold[nextEmpty, 0] = url;
                BSTHold[nextEmpty, 1] = ipAddress;
                BSTHold[nextEmpty, 2] = leftChPtr.ToString();
                BSTHold[nextEmpty, 3] = rightChPtr.ToString();
 
                rootPtr = 1;
                nextEmpty++;
            }
 
            else
            {
                BSTHold[nextEmpty, 0] = url;
                BSTHold[nextEmpty, 1] = ipAddress;
                BSTHold[nextEmpty, 2] = leftChPtr.ToString();
                BSTHold[nextEmpty, 3] = rightChPtr.ToString();
 
                int v = 1;
                Incrementer2 = 1;
                int RowDim = 0;
 
                while (BSTHold[Incrementer + 1, 0] != null)
                {
                    while (BSTHold[Incrementer2 + 1, 0] != null)
                    {
                        if (string.Compare(url, BSTHold[v, 0]) < 0)
                        {
                            if (BSTHold[v, 2].Equals("0"))
                            {
                                BSTHold[v, 2] = nextEmpty.ToString();
                                v++;
 
                                Incrementer2 = (BSTHold.GetLength(RowDim) - 3);
                            }
                            else
                            {
                                v = Convert.ToInt32(BSTHold[v, 2]);
                            }
 
                            Incrementer2++;
                        }
                        else
                        {
                            if (BSTHold[v, 3].Equals("0"))
                            {
                                BSTHold[v, 3] = nextEmpty.ToString();
                                v++;
 
                                Incrementer2 = (BSTHold.GetLength(RowDim) - 3);
                            }
                            else
                            {
                                v = Convert.ToInt32(BSTHold[v, 3]);
                            }
 
                            Incrementer2++;
                        }
 
                        NodeCount++;
                    }
                    Incrementer++;
                    v++;
                }
 
                nextEmpty++;
            }
 
            if (menuchoice == 1)
            {
                data = ReadInFile.ReadLine();//Reads first line of file
 
                if (data != null)
                {
                    ipAddress = "";
                    url = "";
 
                    for (int i = 0; i < 16; i++)//Reading the first 16 characters of the data string
                    {
                        url += data[i];
                    }
 
                    for (int j = 17; j < 27; j++)//Reading the second 10 characters of the data string
                    {
                        ipAddress += data[j];
                    }
 
                    N++;
                }
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial