level of a binary tree

This is a discussion on level of a binary tree within the C Programming forums, part of the General Programming Boards category; hello all, here is a question write a c-function that accepts a pointer to a binary tree & a pointer ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    level of a binary tree

    hello all,
    here is a question


    write a c-function that accepts a pointer to a binary tree & a pointer to a node of the tree and returns the level of the node in the tree.
    i've made a function that accepts a pointer to a node and finds the level of the node in the tree.
    Code:
    int level(struct node *p)
    {
    int level=0;
    struct node *r;
    for(r=p;r->father!=NULL;r=p->father)
    level++;
    return(level);
    }
    my problem is that how can i make a function that should accept another pointer to the tree and returns the level.and also i'm not able to understand what does a pointer to a tree means.is it an array of nodes.
    thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    From your description, your function should take two arguments (pointers) instead of just the one you've provided. Is this a binary search tree or just a generalized binary tree (ordered/unordered)?

    Quote Originally Posted by BEN10
    and also i'm not able to understand what does a pointer to a tree means.is it an array of nodes.
    As long as struct node contains pointers to itself, then the pointers you are dealing with are "tree" pointers. The nodes and the other nodes they point to represent the tree object. It can be physically represented using an array of contiguous memory or as a group of distinct/non-adjacent memory locations.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Is this a binary search tree or just a generalized binary tree (ordered/unordered)?
    this is a unordered generalized binary tree.
    still i'm not able to understand what is a pointer to a tree.
    i'm declaring the nodes as
    Code:
    struct node
    {
    int info;
    struct node *left;
    struct node *right;
    struct node *father;
    };

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    A pointer to a node within a tree, and a pointer to a tree, are the same thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    More specifically, a pointer to a tree is the same as a pointer to the root of that tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  2. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  3. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. binary tree start
    By curlious in forum C++ Programming
    Replies: 6
    Last Post: 01-01-2004, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21