简体   繁体   中英

printing common elements in a binary tree

I am new to c++ and i am trying to implement binary search tree data structure using c++.The following code is for printing the common elements in given two binary search trees.My approach to this problem is that i am creating two arrays of elements of binary search tree 1 and binary search tree 2, ie.. arr1[],arr2[].In the method create_Array() i am storing the values of both the binary trees in sorted order,so when i print values there it prints in sorted order,But when i try to print the values of arr1 & arr2 in method printcommon() it prints not in sorted order but in the order it is there in the tree.

sample input is:-

6

5 6 4 1 2 3

8

6 9 4 10 2 3 5 9

sample output is

Tree 1=1 2 3 4 5 6

Tree 2=2 3 4 5 6 9 10

Common Nodes=2 3 4 5 6

actual output is:

TREE 1=1 2 3 4 5 6

TREE 2=2 3 4 5 6 9 10

Common Nodes=

for more clarity the line which i have commented in method printcommon() prints arr1[0] as 5 but it should print 1,but in function create_Array() the commented line prints perfectly in sorted order,ie.. 1,2,3,4,5,6,2,3,4,5,6,9,10. So if arr1[0] value is 1 in method create_Array(),how it became as 5 from arr1[0] = 1.

    #include <iostream>
    using namespace std;
    struct BstNode
    {
        int data;
        BstNode *left;
        BstNode *right;
    };
    int n1, n2;
    BstNode* getNewNode(int element)
    {
        BstNode* newNode = new BstNode;
        newNode->data = element;
        newNode->left = NULL;
        newNode->right = NULL;
        return newNode;
    }
    BstNode* create_Tree(BstNode* root, int element)
    {
        if (root == NULL)
        {
            root = getNewNode(element);
        }
        else if (element < root->data)
        {
            root->left = create_Tree(root->left, element);
        }
        else if (element > root->data)
        {
            root->right = create_Tree(root->right, element);
        }
        return root;
    }
    void InOrder_Traversal(BstNode * root)
    {
        if (root == NULL) return;
        else
            InOrder_Traversal(root->left);
        cout << root->data << " ";
        InOrder_Traversal(root->right);
    }
    void create_Array(BstNode * root, int arr[], int k)
    {
        if (root == NULL) return;
        create_Array(root->left, arr, k);
        arr[k] = root->data;
        /*cout << arr[k] << endl;*             /*please focus on this line
                                                prints in sorted order*/
        k++;
        create_Array(root->right, arr, k);
    }
    void printCommon(BstNode *root1, BstNode *root2)
    {
        int *arr1 = new int[n1];
        int i = 0;
        create_Array(root1, arr1, i);
        int *arr2 = new int[n2];
        int j = 0;
        create_Array(root2, arr2, j);
        cout << "Common Nodes=";
        /*cout << arr1[0];*/  /* this dosent print in sorted order*/                           
        for (int i = 0; i < n1; i++)
        {
            for (int j = 0; j < n2; j++)
            {
                if (arr1[i] == arr2[j])
                {
                    cout << arr2[j] << " ";
                }
            }
        }
    }
    int main()
    {
        BstNode* root1 = NULL;
        BstNode* root2 = NULL;
        int n1, n2, ele;
        cin >> n1;
        for (int i = 0; i < n1; i++)
        {
            cin >> ele;
            root1 = create_Tree(root1, ele);
        }
        cin >> n2;
        for (int i = 0; i < n2; i++)
        {
            cin >> ele;
            root2 = create_Tree(root2, ele);
        }
        cout << "TREE 1=";
        InOrder_Traversal(root1);
        cout << endl;
        cout << "TREE 2=";
        InOrder_Traversal(root2);
        cout << endl;
        printCommon(root1, root2);
        return 0;
    }

Not sure if this covers all your problems but it's a bug that will make your code fail.

Here:

int n1, n2;  <<<<<<<< NOTICE
BstNode* getNewNode(int element)

you make global variables n1 and n2 that you want to use in some functions like printCommon

But here:

int main()
{
    BstNode* root1 = NULL;
    BstNode* root2 = NULL;
    int n1, n2, ele;    <<<<<<<<<<<<< NOTICE
    cin >> n1;

you make local variables with the same name and take the input into the local variables.

So when the function printcommon is called, it doesn't use the input values but uses the global variables that have the value zero.

You can see this by doing cout << "n1 is " << n1 << endl; inside the function and inside main (just after reading the input).

EDIT

The above didn't fix all the problems. Another problem is the create_array function where the index (aka k ) is wrong, ie it doesn't write to 0, 1, 2, 3, 4, ... but goes back to zero an overwrites previous elements.

A way to fix that problem is to use a pointer for the index. Something like:

void create_Array(BstNode * root, int arr[], int * k)
{
    if (root == NULL) return;
    create_Array(root->left, arr, k);
    arr[*k] = root->data;
    (*k)++;
    create_Array(root->right, arr, k);
}

and call it like

create_Array(root1, arr1, &i);
                          ^ notice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM