BST problem

I got a code for finding the single target ancestor in a BST. How can I change it to become finding all the ancestors wihtout any target in the BST. Basically just print out all the node that has sons. Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool BST<T>::printAncestor(T item)
{
	/* base cases */
	if (root == NULL)
		return false;

	if (root->data == target)
		return true;

	/* If target is present in either left or right subtree of this node,
	then print this node */
	if (printAncestors(root->left, target) ||
		printAncestors(root->right, target))
	{
		cout << root->data << " ";
		return true;
	}

	/* Else return false */
	return false;
}
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
template < typename T > struct BST
{
    struct node
    {
        T data ;
        node* left ;
        node* right ;
    };

    // ...

    void print_non_leaf() const { print_non_leaf(root) ; }

    // ...

private:

        node* root ;

        bool is_non_leaf( const node* p ) const
        { return p && ( p->left || p->right ) ; }

        void print_non_leaf( const node* p ) const
        {
            if( is_non_leaf(p) ) // print only if it is not a leaf node
            {
                // in-order
                print_non_leaf( p->left ) ;
                std::cout << p->data << '\n' ;
                print_non_leaf( p->right ) ;
            }
        }

        // ...
};
Last edited on
Im not using struct but class, i understand the step of your code but dn knw hw to change it and put inside my code cz struct...here my full code
> Im not using struct but class, ...

In a class declaration, the keywords class and struct are synonyms, except that:
With the keyword class, the default member-access (and the default base-class-access) is private
With the keyword struct, the default member-access (and the default base-class-access) is public

class A { public: /* ... */ }; and struct A { /* ... */ }; are equivalent.

class A { /* ... */ }; and struct A { private: /* ... */ }; are equivalent.

Using the keyword class, as in your code:

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
template < typename T > class BTNode
{
    public:
        T data ;
        BTNode* left ;
        BTNode* right ;
    // ...
};

template < typename T > class BST
{
    public:

        // ...

        void print_non_leaf() const { print_non_leaf(root) ; }

        // ...

    private:

        int count ;
        BTNode<T>* root ;

        // ...

        bool is_non_leaf( const BTNode<T>* p ) const
        { return p && ( p->left || p->right ) ; }

        void print_non_leaf( const BTNode<T>* p ) const
        {
            if( is_non_leaf(p) ) // print only if it is not a leaf node
            {
                // in-order
                print_non_leaf( p->left ) ;
                std::cout << p->data << '\n' ;
                print_non_leaf( p->right ) ;
            }
        }
        // ...
};
ohh okok, i understand now thank you very much.
Topic archived. No new replies allowed.