Insert elements in binary tree

This is a discussion on Insert elements in binary tree within the C Programming forums, part of the General Programming Boards category; How would you insert random int in a binary tree? eg 1,5,34,90,3,56,1. What is the logic behind inserting these nodes?...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    20

    Insert elements in binary tree

    How would you insert random int in a binary tree? eg 1,5,34,90,3,56,1. What is the logic behind inserting these nodes?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    http://en.wikipedia.org/wiki/Binary_Tree

    It depends upon which type of a binary tree you use. If you want the tree balanced, then the logic is different. If you just want to insert without it being balance, then if you accept the numbers that you gave in that order it would be like this:

    • 1 is made the root.
    • 5 is greater than 1, so inserted right.
    • 34 is greater than 1, so inserted right. 34 is greater than 5, so inserted right.
    • 90 is greater than 1, so inserted right. 90 is greater than 5, so inserted right. 90 is greater than 34, so inserted right.
    • 3 is greater than 1, so inserted right. 3 is less than 5, so inserted left.

      ...


    And so on and so forth.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    20
    Okay...it looks like a Binary search tree insertr. I wasn't sure if binary tree follows the same procedure. Thanks!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by lastrial View Post
    Okay...it looks like a Binary search tree insertr. I wasn't sure if binary tree follows the same procedure. Thanks!
    A binary tree just means that internal nodes never have more than two children. But a tree isn't much use unless it has SOME kind of ordering to it. You're right to make the distinction, because there is more than one possible useful order.

    A binary search tree is one order, which MacGyver described. A heap is another possible order. And there are things like binary Huffman trees where the order is actually a clever way of encoding information.

    So to answer your general question, you insert data in a binary tree by deciding where to insert it and then inserting it there To get a more specific answer you have to ask a more specific question.

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. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 08:11 AM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

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