Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,183 people online right now. Registration is fast and FREE... Join Now!




Binary tree program

 
Reply to this topicStart new topic

Binary tree program

aedonofrio
21 Aug, 2008 - 07:08 AM
Post #1

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 2

Hi everyone.

I have to write a program to input ten integers into a tree and the program then prints out the inorder, postorder, and preorder traversal for the tree indicating the LEVEL of each node as it is printed out. Finally, I have to compute the maximum and minimum leaf levels of the tree. I got the program to run and print out the inorder, postorder, and preorder traversals, but I'm stuck on how to print out the level of each node and the maximum/minimum leaf levels. Can anyone give me any suggestions? It would be greatly appreciated.

CODE

#include <iostream>
using namespace std;

const int nil = 0;
class treenode_type
{
public:
    int info;
    treenode_type *left;
    treenode_type *right;
};

void setleft(int x);
void setright(int x);
void inorder(treenode_type *p);
void preorder(treenode_type *p);
void postorder(treenode_type *p);
treenode_type *p, *q, *root;
int number;

void main()
{
    cout << "Enter first value: \n";
    cin >> number;
    cout << number << "\n";
    root = new treenode_type;
    (*root).info = number;
    (*root).left = nil;
    (*root).right = nil;
    cout << "Enter other numbers terminated by -999\n";
    cin >> number;
    while (number != -999)
    {
        p = root;
        q = p;
        while ((number != (*p).info) && (q!=nil))
        {
            p = q;
            if (number < (*p).info)
                q = (*p).left;
            else
                q = (*p).right;
        }
        if (number == (*p).info)
            cout << number << " is a duplicate \n";
        else if (number < (*p).info)
        {
            setleft(number);
            cout << number << " is a left child of " << (*p).info << "\n";
        }
        else
        {
            setright(number);
            cout << number << " is a right child of " << (*p).info << "\n";
        }
        cin >> number;
    }
    cout << "The tree traversed INORDER is \n";
    p = root;
    inorder(p);

    cout << "The tree traversed PREORDER is \n";
    p = root;
    preorder(p);

    cout << "The tree traversed POSTORDER is \n";
    p = root;
    postorder(p);
}
void setleft(int x)
{
    treenode_type *q;
    q = new treenode_type;
    (*q).info = x;
    (*q).left = nil;
    (*q).right = nil;
    (*p).left = q;
}
void setright(int x)
{
    treenode_type *q;
    q = new treenode_type;
    (*q).info = x;
    (*q).left = nil;
    (*q).right = nil;
    (*p).right = q;
}
void inorder(treenode_type *r)
{
    if (r != nil)
    {
        inorder((*r).left);
        cout << (*r).info << "\n";
        inorder((*r).right);
    }
}
void preorder(treenode_type *r)
{
    if (r != nil)
    {
        cout << (*r).info << "\n";
        preorder((*r).left);
        preorder((*r).right);
    }
}
void postorder(treenode_type *r)
{
    if (r != nil)
    {
        postorder((*r).left);
        postorder((*r).right);
        cout << (*r).info << "\n";
    }
}


User is offlineProfile CardPM
+Quote Post

Mallstrop
RE: Binary Tree Program
23 Aug, 2008 - 02:44 AM
Post #2

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 32



Thanked: 1 times
My Contributions
I'd use one of your traversal methods but include a depth variable that you can increment each time you look at a layer further down.

CODE

void inorder(treenode_type *r, int depth)
{
    if (r != nil)
    {
        inorder((*r).left,depth+1);
        cout << (*r).info << " : " << depth << "\n";
        inorder((*r).right,depth+1);
    }
}

User is offlineProfile CardPM
+Quote Post

aedonofrio
RE: Binary Tree Program
23 Aug, 2008 - 08:06 AM
Post #3

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 2

thanks so much! that was extremely helpful!
User is offlineProfile CardPM
+Quote Post

phokojoe
RE: Binary Tree Program
Yesterday, 09:56 PM
Post #4

New D.I.C Head
*

Joined: Yesterday, 09:49 PM
Posts: 1

QUOTE(aedonofrio @ 23 Aug, 2008 - 08:06 AM) *

thanks so much! that was extremely helpful!


i am a new member and have just joined dreamcode. of course i do dream code. i am learning c++ on my own where i work and would want to be fluent in c++ programming. i happen to read about the trees and tree traversals and so on. but how to apply the traversal in a code, your code helped me a lot. i was just looking at it and analysing it. but now, how do you implement the computation of the leaves of the binary tree in your code.

please. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 01:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month