| |

VerySource

 Forgot password?
 Register
Search
View: 846|Reply: 9

A problem with a binary tree

[Copy link]

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-15 17:40:01
| Show all posts |Read mode
The program enters integers one by one. When the input integer is not the end flag -9999, it is inserted into the binary sorting tree. If the value node is found to exist in the binary sorting tree during the insertion process, the repeatability is counted The count of this node. Until the end of all input.

#include <stdio.h>
#include <stdlib.h>
typedef struct Binary
{
     int val, count;
     struct Binary * left * right;
} Binary;

void binaryTree (Binary ** t, int data)
{
    Binary * ptr, * p;
    int d;
    p = NULL;
    ptr = _______ (1) _______;
    while (_______ (2) _______)
    {
        if (data == ptr-> val)
           _______ (3) _______;
        else
        {
           p = ptr;
           ptr = _______ (4) _______;
     }

    ptr = (Binary *) malloc (sizeof (Binary));
    ptr-> right = ptr-> left = NULL;
    _______ (5) _______;
    if (_______ (6) _______)
       _______ (7) _______ = ptr;
    else if (data> p-> val)
         p-> right = ptr;
         else
         p-> left = ptr;
}
     
void main ()
{
   Binary * root = NULL;
   int d;
   scanf ("% d",&d);
   while (d! =-9999)
   {
      binaryTree (_______ (8) _______);
      scanf ("% d",&d);
    }
}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-19 16:27:02
| Show all posts
My answer is (1) * t
          (2) ptr! = NULL
          (3) ptr-> count ++; return;
          (4)
          (5) ptr-> val = data;
          (6) * t == NULL
          (7) * t
          (8)&root, d
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 09:09:01
| Show all posts
The source code is incorrect, and {? Is still the case. . . .

4 ptr-> right? Ptr-> left: data> ptr-> val} (perverted)
5 ptr-> val = data; ptr-> count = 1;
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-20 15:36:01
| Show all posts
while (_______ (2) _______)
    {
        if (data == ptr-> val)
           _______ (3) _______;
        else
        {
           p = ptr;
           ptr = _______ (4) _______;
         }
     }
Change it. Missed one here)
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-20 16:09:02
| Show all posts
gwf17445Thank you (4) for the answer.
But is it wrong? Because it is data <ptr-> val? Ptr-> right: ptr-> left
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 16:18:01
| Show all posts
Upstairs 4 seems to write a bit of syntax error:
4 data> ptr-> val? ptr-> right: ptr-> left;}
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-20 16:27:01
| Show all posts
The other answers are correct
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 16:45:01
| Show all posts
gwf17445answer is good, like!
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-20 18:45:01
| Show all posts
mark
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-20 19:27:01
| Show all posts
Top. Brothers are really better than one. I'm too soiled
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list