Thread: Help in implementing N-ary trees or general trees in C.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    Cool Help in implementing N-ary trees or general trees in C.

    Hi, I am new to trees in C. I am implementing an n-ary tree in C. I have implemented binary trees. But I am facing problems in implementing n-ary trees. The implementation is very easy for binary trees is easy for me but the implementation of n-ary trees are very confusing to me. So it would be very helpful if someone gives me some hints or code snippets or resources or explanation or if someone has a lot of time then perhaps a tutorial which would also be helpful to other newcomers.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    For an "N-ary" tree to function properly, its leaves must be able to be distinguished from each other in at least N ways. Once you understand how to do that, making nodes that can hold N pointers each is piece of cake.

    For example, in a binary tree, we sort using two states, either smaller or not smaller (or variations of this). A ternary tree could be configured to sort for lesser, equal or greater. Higher trees such as quad tree and oct tree are sometimes used with spatial dimensions, because they help subdivide the space into smaller and smaller blocks. A 2D vector, because it consists of two coordinates, each of which can differ in two or three ways, can itself differ from another 2D vector in 4 to 8 ways. That's one way of looking at it...
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,765
    Perhaps you can start with turning a binary tree
    Code:
    struct node {
      int data;
      struct node *left;
      struct node *right;
    };
    Into a 2-way tree
    Code:
    struct node {
      int data;
      struct node children[2];
    };
    I mean, once you figure out how to choose a children[i], the rest is the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    Thanks, I got the idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AVL trees
    By Nasief in forum C Programming
    Replies: 1
    Last Post: 03-22-2011, 04:53 PM
  2. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  3. General Trees, calculating height
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2005, 08:59 AM
  4. Help with (2,3,4)/2-4 trees
    By Nornny in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2004, 04:58 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM

Tags for this Thread