Docsity

lab for data structure in lab tree in binary search tree BST, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Lab 7
(Binary Search Trees)
Exercise 1
Create a class BST using the self-referential class BSTNode having key , data, left, and right data fields.
The class BST should have the data field root of type BSTNode and size. It should implement the
following methods:
1. BSTNode<Key,T> getRoot ()
2. T find ( Key K )
3. void insert ( Key k , T e )
4. T remove ( Key k )
5. void inOrder ( BSTNode<Key,T> p )
6. boolean isInternal ( BSTNode<Key,T> p )
7. void setRoot ( BSTNode<Key,T> p )
8. BSTNode<Key,T> parent ( BSTNode<Key,T> p )
Returns the reference of p’s parent (or null if p is the root).
9. BSTNode<Key,T> getMax ( BSTNode<Key,T> p )
returns the node that has the maximum key in the binary search tree.( or null if p is null)
10. int toSortedArray ( BSTNode<Key,T> p , Key[] a , int i )
Puts the keys of the binary search tree having a root p in the array a in a increasing order. The
parameter i is used as an index to the array for recursive calls.
11. boolean deleteLeaves ( BSTNode<Key,T> p )
Deletes the leaf nodes from the binary search tree.
Outside the BST class:
•void incrementByOne ( BSTNode<Key,T> p )
Increments stored data in all nodes by 1.
•void toBST ( int[] a , BST b , int l , int h)
Puts the keys of the sorted array a in the binary search tree b. The parameters l and h are used as
an index to the array for recursive calls.
•in the main () , Test all previous methods.

Partial preview of the text

Download lab for data structure in lab tree in binary search tree BST and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Lab 7

(Binary Search Trees)

Exercise 1

Create a class BST using the self-referential class BSTNode having key , data , left , and right data fields. The class BST should have the data field root of type BSTNode and size. It should implement the following methods:

1. BSTNode<Key,T> getRoot ()

2. T find ( Key K )

3. void insert ( Key k , T e )

4. T remove ( Key k )

5. void inOrder ( BSTNode<Key,T> p )

6. boolean isInternal ( BSTNode<Key,T> p )

7. void setRoot ( BSTNode<Key,T> p )

8. BSTNode<Key,T> parent ( BSTNode<Key,T> p )

Returns the reference of p’s parent (or null if p is the root).

9. BSTNode<Key,T> getMax ( BSTNode<Key,T> p )

returns the node that has the maximum key in the binary search tree.( or null if p is null)

10. int toSortedArray ( BSTNode<Key,T> p , Key[] a , int i )

Puts the keys of the binary search tree having a root p in the array a in a increasing order. The parameter i is used as an index to the array for recursive calls.

11. boolean deleteLeaves ( BSTNode<Key,T> p )

Deletes the leaf nodes from the binary search tree.

Outside the BST class:

• void incrementByOne ( BSTNode<Key,T> p )

Increments stored data in all nodes by 1.

• void toBST ( int[] a , BST b , int l , int h)

Puts the keys of the sorted array a in the binary search tree b. The parameters l and h are used as an index to the array for recursive calls.

  • in the main () , Test all previous methods.