Binary Tree Class

stack class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstdlib>

template<class type>
struct node{
    type data;
    node<type> *link;
};

template<class type>
class stack{
public:
    /*
     */
    stack();
    /*
     */
    ~stack();
    bool empty() const;
    void push(const type & element);
    type pop();
    
private:
    node<type> *first;
    node<type> *last;
};

template<class type>
stack<type>::stack(){
    first=last=NULL;
}

template<class type>
stack<type>::~stack(){
    node<type> *temp;
    while(first!=NULL){
        temp=first;
        first=first->link;
        delete temp;
    }
    first=last=NULL;
}

template<class type>
bool stack<type>::empty() const{
    return (first==NULL);
}

template<class type>
void stack<type>::push(const type& element){
    node<type> *temp;
    temp=new node<type>;
    temp->data=element;
    if(empty()){
        first=temp;
        last=temp;
        temp->link=NULL;
    }
    else{
        temp->link=first;
        first=temp;
    }
}

template<class type>
type stack<type>::pop(){
    node<type> *temp;
    if (!empty()){
        temp=first;
        first=first->link;
        return temp->data;
    }
}




binary tree class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
template<class type>
class BinaryTree{


private:
    struct Tree_Node{
        type Info;
        BinaryTree<type> *left; 
        BinaryTree<type> *right;
    };

    Tree_Node *root;
    
public:
    /*Creates and empty Binary Tree.
      Children are set to NULL
     */
    BinaryTree();
    /*Creates a Binary Tree with only one node;
      the other points to NULL
      */
    BinaryTree(const type & element);
    
    BinaryTree(const type & a, const type & b, const type & c);
};

template<class type>
BinaryTree<type>::BinaryTree(){
    root=NULL;
    node<type> *temp;
    temp=new node<type>;
    temp->data=*this;
    
    
}

template<class type>
BinaryTree<type>::BinaryTree(const type & element){
    Tree_Node *temp;
    temp=new Tree_Node;
    temp->Info=element;
    temp->left=NULL;
    temp->right=NULL;
    root=temp;
}


i have stack.h included dont worry just didnt paste it :b

the default constructor creates an empty tree and pushes it onto stack. just wondering if the "*this" assignment would create a tree and push it onto the stack or if this is logically incorect
The default constructor is incorrect. You create a local variable (temp) inside the constructor that will be destructed at the end of the function, causing the allocated memory to be inaccessible (memory leak).
Furthermore, inside the ctor you don't have a stack object to begin with, so it's impossible to push the tree into it, unless you create it locally (but then it would be destructed at the end of the function).

just wondering if the "*this" assignment would create a tree

It will most likely cause a compilation error. Let's say you have a BinaryTree<int>, then you create a node<int> with a int data member and try to assign a BinaryTree<int> to an int variable.
Last edited on
okay wait i got the stack class figured out, i can even push a binarytree obj onto it, but how would i pop it??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template<class type>
void stack<type>::push(const type & element){
    node<type> *temp;
    temp=new node<type>;
    temp->data=element;
    if(empty()){
        first=temp;
        last=temp;
        temp->link=NULL;
    }
    else{
        temp->link=first;
        first=temp;
    }
}

template<class type>
type stack<type>::pop(){
    node<type> *temp;
    if (!empty()){
        temp=first;
        first=first->link;
        return temp->data;
    }
}
The STL std::stack doesn't return the element when popping it, it just deletes the top of the stack.
1
2
3
4
5
6
7
8
9
template<class type>
void stack<type>::pop(){
    node<type> *temp;
    if (!empty()){
        temp=first;
        first=first->link;
        delete temp;
    }
}


It's better to make a separate function to read the top of the stack.
You could return the data by value, but that creates copies even in cases where you don't need them after popping. If you store large data structures it is quite inefficient.
Topic archived. No new replies allowed.