Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Demonstrate mastery of object oriented design and programming in Java

ID: 3569470 • Letter: O

Question

Objective:

Demonstrate mastery of object oriented design and programming in Java

Demonstrate mastery in implementation of binary tree data structure

Demonstrate mastery in understanding and implementation of binary search trees

Develop familiarity with the computational and space complexity

Description:
Create a generic binary search tree that contains a <T> bNode class, which describes a single node of a binary tree, and a bsTree class that uses bNode object to define the object binary tree with all of its features. A binary search tree, or BST, is a binary tree whose nodes are arranged such that for every node n, all of the nodes in n's left subtree have a value less than n, and all nodes in n's right subtree have a value greater than n. In the average case BSTs offer log2 n asymptotic time for inserts, deletes, and searches. (log2 n is often referred to as sublinear because it outperforms linear asymptotic times.) For more information on BSTs, refer to the following link: http://msdn.microsoft.com/en-us/library/ms379573(v=vs.80).aspx

You can also refer to the following link to interact with a visualization tool for building a well-balanced BST (AVL tree): http://www.cs.usfca.edu/~galles/visualization/Algorithms.html

Project requirements:

Your program should contain two objects with the following criteria:

Program should have comments: (+3 pts)

A generic object named bNode that describes a single node in the binary tree. (+10 pts)

The bNode object should be generic and receive its data type from the user or other objects. (+2 pts)

The bNode object should contain a default constructor. (+2 pts)

The bNode object should contain an alternate constructor that initializes the objects content variable. (+2 pts)

All instance variables should be private. (+2 pts)

Should make proper use of getters and setters. (+2 pts)

An generic object named bsTree that utilizes bNode object in order to define a binary tree. (+50 pts)

The bsTree object should be generic and receive its data type from the user or other objects. (+3 pts)

Should contain a default constructor. (+3 pts)

Should contain an alternate constructor bsTree(type[] anArray) that allows the user to create a bsTree from an array in a manner that creates a well balanced Binary Search Tree. (+3 pts)

Should contain a void add(Type value) method that inserts a node with content of value to an appropriate position within the tree in order to maintain balance of the tree. (+5 pts)

Note that it is important to maintain a well-balanced tree, otherwise the benefits of searching a binary tree will not be observed.

Should contain an Type remove(Type value) method that removes the first instance of a node with value of value. (+5 pts)

The removed value is returned by the method.

If the node does not exist, then do nothing and return null.

The children of the removed node should be transferred to the parent of the node such that the parent-child relationship is preserved. Example: If a node that is being removed is the left-child of its parent, then its left children should be transferred as the left child of the parent. If a node that is being removed is the right child of the parent, the the right children of the removed node should become the right children of the parent.

The tree should remain balanced after removing the node.

Should contain boolean contains(Type value) method. (+2 pts)

Should contain a void print(String traversal) method that prints all elements of the bTree based on the traversal method in one row delimited by one space. The argument traversal may be one of the following three: preorder, inorder, or postorder. (+5 pts)

Should contain an int height() method that returns depth of the tree. (+2 pts)

The height of a tree is defined as the length of the longest path starting at the root.

Should contain a boolean isBalanced() method that returns true for a balanced tree and false for an imbalanced tree. (+2 pts)

BST balancing:

Make sure to check to make sure your tree is balanced after every insertion. If not, then immediately correct it and do not let imbalances accumulate.

Implement one of the following four scenarios when discovering an imbalance: (+5 pts each)

RR

LL

RL

LR

Submission Notes:
Your final submission should consists of at least two .java files: bsTree.java and bNode.java. You are free to include whatever main.java files that you used to test your work.

Explanation / Answer

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

42

43

44

45

46

47

48

49

50

51

52

#include<stdio.h>

#define M 50

struct state {

  char name[50];

  long int population;

  float literacyRate;

  float income;

} st[M]; /* array of structure */

int main() {

  int i, n, ml, mi, maximumLiteracyRate, maximumIncome;

  float rate;

  ml = mi = -1;

  maximumLiteracyRate = maximumIncome = 0;

  printf("Enter how many states:");

  scanf("%d", &n);

  for (i = 0; i < n; i++) {

     printf(" Enter state %d details :", i);

     printf(" Enter state name : ");

     scanf("%s", &st[i].name);

     printf(" Enter total population : ");

     scanf("%ld", &st[i].population);

     printf(" Enter total literary rate : ");

     scanf("%f", &rate);

     st[i].literacyRate = rate;

     printf(" Enter total income : ");

     scanf("%f", &st[i].income);

  }

  for (i = 0; i < n; i++) {

     if (st[i].literacyRate >= maximumLiteracyRate) {

       maximumLiteracyRate = st[i].literacyRate;

       ml++;

     }

     if (st[i].income > maximumIncome) {

       maximumIncome = st[i].income;

       mi++;

     }

  }

  printf(" State with highest literary rate :%s", st[ml].name);

  printf(" State with highest income :%s", st[mi].name);

  return (0);

}

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

42

43

44

45

46

47

48

49

50

51

52

#include<stdio.h>

#define M 50

struct state {

  char name[50];

  long int population;

  float literacyRate;

  float income;

} st[M]; /* array of structure */

int main() {

  int i, n, ml, mi, maximumLiteracyRate, maximumIncome;

  float rate;

  ml = mi = -1;

  maximumLiteracyRate = maximumIncome = 0;

  printf("Enter how many states:");

  scanf("%d", &n);

  for (i = 0; i < n; i++) {

     printf(" Enter state %d details :", i);

     printf(" Enter state name : ");

     scanf("%s", &st[i].name);

     printf(" Enter total population : ");

     scanf("%ld", &st[i].population);

     printf(" Enter total literary rate : ");

     scanf("%f", &rate);

     st[i].literacyRate = rate;

     printf(" Enter total income : ");

     scanf("%f", &st[i].income);

  }

  for (i = 0; i < n; i++) {

     if (st[i].literacyRate >= maximumLiteracyRate) {

       maximumLiteracyRate = st[i].literacyRate;

       ml++;

     }

     if (st[i].income > maximumIncome) {

       maximumIncome = st[i].income;

       mi++;

     }

  }

  printf(" State with highest literary rate :%s", st[ml].name);

  printf(" State with highest income :%s", st[mi].name);

  return (0);

}