insertion into a binary tree

This is a discussion on insertion into a binary tree within the C++ Programming forums, part of the General Programming Boards category; //I'm having trouble inserting an array of integers into a binary //search tree. The function below is what my last ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    insertion into a binary tree

    //I'm having trouble inserting an array of integers into a binary
    //search tree. The function below is what my last attempt at
    //this looked like. Only the first node will print. Any help
    //would be greatly appreciated.
    //If this code is confusing let me know and I will clarify.

    void build_tree(int size, LIST_I data, TREE& t)
    {
    PT_NODE ptr = t; //local pointer to run through tree
    int ct = 0;
    PT_NODE p = new NODE;
    p->info.i = data[ct];
    p->left = NULL;
    p->right = NULL;
    t = p;

    for (ct = 1; ct < size; ++ct) {
    ptr = t;
    PT_NODE p = new NODE;
    p->info.i = data[ct];
    p->left = NULL;
    p->right = NULL;
    while (ptr)
    if (p->info.i > ptr->info.i)
    ptr = ptr->right;
    else
    ptr = ptr->left;
    ptr = p;
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    1. read the FAQ / rules etc
    In particular
    http://www.cprogramming.com/cboard/m...bbcode#buttons

    if (p->info.i > ptr->info.i)
    ptr = ptr->right;
    else
    ptr = ptr->left;
    ptr = p;

    The last statement just wipes away the result of your if/else decision.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 04:15 PM
  2. 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
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  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. 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