Thread: Binary Trees?

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Binary Trees?

    Well, now I've learned linked lists and stacks, now how do I go about making a binary tree? How about implementing it in a practical application?

    And while we're at it, give me a bunch of problems involving stacks and linked lists! Post your homework assignments! (Of course, I won't post the code, I just want the exercises)
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    do a RADIX sort using linked lists.

    or for a more complex tree scenario, do huffman coding of a text file.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>now how do I go about making a binary tree?
    A binary tree is really just a linked list with two forward links instead of one :-)
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    struct link {
      int num;
      link *left;
      link *right;
    
      link(int init, link *l = 0, link *r = 0): num(init), left(l), right(r){}
    };
    
    link *add(link *root, int num)
    {
      if (root == 0)
      {
        root = new link(num);
      }
      else if (num < root->num)
      {
        root->left = add(root->left, num);
      }
      else if (num > root->num)
      {
        root->right = add(root->right, num);
      }
      else
      {
        // Ignore duplicates
      }
    
      return root;
    }
    
    void print(link *root)
    {
      if (root == 0)
      {
        return;
      }
    
      print(root->left);
      cout<< root->num <<flush;
      print(root->right);
    }
    
    void destroy(link *root)
    {
      if (root == 0)
      {
        return;
      }
    
      destroy(root->left);
      destroy(root->right);
      delete root;
    }
    
    int main()
    {
      link *root = 0;
    
      for (int i = 0; i < 10; i++)
      {
        root = add(root, rand() % 10);
      }
    
      print(root);
      destroy(root);
    }
    *Cela*

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >now how do I go about making a binary tree? How about implementing it in a practical application?

    A practical application of binary trees is searching. Develop functions to create and maintain a binary tree and develop functions to perform actions like searching on it.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Huffman codes are good, also make a priority queue using a tree.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Binary trees are a common data structure. You should have no problem finding examples or discussion on the internet or in university libararies.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  2. Binary Trees
    By wvu2005 in forum C Programming
    Replies: 7
    Last Post: 10-15-2005, 04:59 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM