Heap class
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
    Posts
    1

    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 {
    public:
    Node();
    ~Node();
    private:
    int data;
    Node *parent;
    Node *lchild;
    Node *rchild;

    friend class PointerHeap;
    };

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

    //PointerHeap class
    class PointerHeap
    {
    public:
    PointerHeap()
    {
    sz = 0;
    root = NULL;
    }
    void insert(int elem);
    private:
    int sz;
    Node *root;
    };

    //this function is not completed having problems implementing it.
    void PointerHeap::insert(int elem)
    {
    sz++;
    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,329

    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
    Posts
    27,426

    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.

    Regards,

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center