Binary Search Trees: Assigned Problems #1

 

 

Problem #1:

Write a method which takes the address of the root and an integer value as the input and prints all the values in the path from the root down to the input value. If the input value does not exist in the binary search tree, the method will simply print, “Input value does not exist”.

 

Example:

 

       100                 49

                  40            

                   23

20

                 10

       5

                 4

  

  

1 - Insert a Value

2 - Print the Tree

3 - Root to Leaves

4 - Strictly Binary

5 – Quit

3

Input value?

10

Path :

20  5  10 

 

1 - Insert a Value

2 - Print the Tree

3 - Root to Leaves

4 - Strictly Binary

5 - Quit

3

Input value?

40

Path:

20 100 40

 

 

 

Problem #2:

strictly binary node in a binary search tree is a node which has both a left node and a right node. Write a (void) method which checks if all the non-leaf nodes of a binary search tree are strictly binary or not and prints the following:

 

1)     If none of the non-leaf nodes are strictly binary, your method should print a line stating, “None of the leaf nodes are strictly binary”.

2)     If all non-leaf nodes are not strictly binary, your method should print a count of the number of non-leaf nodes that are strictly binary. 

3)     If all the non-leaf nodes are strictly binary, your method should print a line stating, “All non-leaf nodes are strictly binary".

 

Solution

 

  

ExamplesL

 

#1:

 

                        

            

23

                        

            11

                                    

                        1

 

None of the leaf nodes are strictly binary

 

 

#2:

 

                        67

            45

23

                        13

            11

                                    9

                        1

 

 

2 non-leaf nodes that are strictly binary

 

 

#3:

 

                        67

            45

                        33

23

                        13

             11

                                    9

                        1

 

3 non-leaf nodes that are strictly binary.

 

 

 

#4:

 

 

                         67

             45

                        33

23

                        13

            11

                                    9

                        1

                                     0

 

 

All non-leaf nodes are strictly binary.