CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Heap class

  1. #1
    Join Date
    Feb 2013

    Resolved Heap class

    Im new wiith handling trees, ive been trying to implement this heap class with just 2 memeber functions, Insert() and DeleteMin() using pointers but i dont know how to implement it keeping the complexity of logn for insert. amy ideas of how this should be implmented it? i know that u should add a new node to the last position in the tree and then bubble up swaping till i reach the position where the elemn must reside.
    however, im hacving trouble putting that into code using pointers.

    so far i have this.

    //Define a node object
    class Node {
    int data;
    Node *parent;
    Node *lchild;
    Node *rchild;

    friend class PointerHeap;

    //Node methods
    Node::Node() {}
    Node::~Node() {}

    //PointerHeap class
    class PointerHeap
    sz = 0;
    root = NULL;
    void insert(int elem);
    int sz;
    Node *root;

    //this function is not completed having problems implementing it.
    void PointerHeap::insert(int elem)
    int position = sz;
    Node *temp;
    temp->parent->data = elem;
    temp->lchild = NULL;
    temp->rchild = NULL;

    if(root == NULL)root = temp;
    if(position % 2 == 1)


  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Heap class

    Never mind trying to write it straight into c++ code! Always design the program first, then code according to the design. If you first write how trees works on paper by drawing boxes with links showing insertion, deletion, etc. and describe in English (psedu-code) how this works you can then write the code according to the design - and if the design is correct then the program will be. If you can't produce a paper plan of what is needed and the necessary pseudo-code then it's no good trying to code what you don't know.

    Please if you post code again, format it properly and use code tags so it displays correctly. Go Advanced, highlight code, click '#'

  3. #3
    Join Date
    Apr 1999

    Re: Heap class

    And may I add to what was already stated:

    If you design it on paper first, and you code from the design, and the program does not do what you expect, please debug your code first before asking further questions.

    If you have a plan laid out, and the program takes a different path than what you designed, then you debug your code to see where the program goes against what you designed. Once you find where things diverge, then you diagnose the problem by making corrections to your program to follow your design, or you take a step back and determine if your design on paper is flawed in some way.

    If all beginner programmers took the approach that 2kaud and myself have mentioned, there is seldom ever a need to ask questions on a forum *unless* it is purely a C++ language issue.


    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.