0% found this document useful (0 votes)
4 views

Assignment 2 - CSD201

The document outlines an assignment involving a binary search tree (BST) implementation for managing Car objects based on owner and price. It specifies various methods to be completed in the BSTree class, including insertion, traversal, deletion, and counting nodes. Additionally, it provides input and expected output examples for each method to guide the implementation.

Uploaded by

hieukxhe186118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 2 - CSD201

The document outlines an assignment involving a binary search tree (BST) implementation for managing Car objects based on owner and price. It specifies various methods to be completed in the BSTree class, including insertion, traversal, deletion, and counting nodes. Additionally, it provides input and expected output examples for each method to guide the implementation.

Uploaded by

hieukxhe186118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT 2 – CSD201

Input:

50 5 61 43 43 35 47 3 1 25

Tree

In this question you should complete some methods in BSTree.java files.

The class Car with 2 data members: owner and price is given and you do not need to
edit it. The BSTree class is a binary search tree of the Car object. The variable price
is the key of the tree. The following methods should be completed:

void f0_getInformation(){

System.out.println("HE123456"); //Replace HE123456 by your RollNumber

● void insert(string xOwner, int xPrice) - check if the second character of xOwner
equals 'X' or xPrice >100 then do nothing, otherwise insert a new car with
owner=xOwner, price=xPrice to the tree.

● void f1() – You do not need to edit this function. Your task is to complete the
insert(...) function above only.

[email protected] 1
Your selection (1 -> 10): 1

Input :(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

Output:(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

● void f2() – Perform in-order traversal from the root but only displays nodes with
prices in the range [3,50]..

Your selection (1 -> 10): 2

Input :(A1,1) (A2,3) (BA,5) (B2,25) (G1,35) (EB,43) (X2,47) (AB,50) (BD,61)

Output:(A2,3) (BA,5) (B2,25) (G1,35) (EB,43) (X2,47) (AB,50)

● void f3() – Perform postOrder traversal from the root and delete by copying the first
node that has 2 children and its price between [30,70]

Your selection (1 -> 10): 3

Input :(A1,1) (A2,3) (B2,25) (G1,35) (X2,47) (EB,43) (BA,5) (BD,61) (AB,50)

Output:(A1,1) (A2,3) (B2,25) (G1,35) (X2,47) (BA,5) (BD,61) (AB,50)

● void f4() – Perform inOrder traversal from the root and delete by Merging the first
node having left child and its price in the range [30,70].

Your selection (1 -> 10): 4

Input :(A1,1) (A2,3) (BA,5) (B2,25) (G1,35) (EB,43) (X2,47) (AB,50) (BD,61)

Output:(A1,1) (A2,3) (BA,5) (B2,25) (EB,43) (X2,47) (AB,50) (BD,61)

● void f5() – Suppose p is 4-th node when perform pre_Order traversal from the root
of the tree and delete by Copy node p.

Your selection (1 -> 10): 5

[email protected] 2
Input :(AB,50) (BA,5) (A2,3) (A1,1) (EB,43) (G1,35) (B2,25) (X2,47) (BD,61)

Output:(AB,50) (BA,5) (A2,3) (EB,43) (G1,35) (B2,25) (X2,47) (BD,61)

● void f6() – Suppose p is the 2-th node which has left node when performing
Bread_first traversal from the root of the tree then deletes by Merging node p.

Your selection (1 -> 10): 6

Input :(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

Output:(AB,50) (A2,3) (BD,61) (A1,1) (EB,43) (G1,35) (X2,47) (B2,25)

● void f7() – Perform preOrder traversal from the root and find the first node p having
right child and its price in the range [30,70]. Rotate p to left about its right child.

Your selection (1 -> 10): 7

Input :( AB,50) (BA,5) (A2,3) (A1,1) (EB,43) (G1,35) (B2,25) (X2,47) (BD,61)

Output:(BD,61) (AB,50) (BA,5) (A2,3) (A1,1) (EB,43) (G1,35) (B2,25) (X2,47)

● void f8() – Perform preOrder traversal from the root and find the first node p having
left child and its price in the range [30,70]. Rotate p to right about its left child.

Your selection (1 -> 10): 8

Input :(AB,50) (BA,5) (A2,3) (A1,1) (EB,43) (G1,35) (B2,25) (X2,47) (BD,61)

Output:(BA,5) (A2,3) (A1,1) (AB,50) (EB,43) (G1,35) (B2,25) (X2,47) (BD,61)

● void f9(): Suppose p is the 7-th node when performing the in-order traversal of the
tree. Calculate the height p then assign that value to the heightofNode variable;

[email protected] 3
Your selection (1 -> 10): 9

Input :(A1,1) (A2,3) (BA,5) (B2,25) (G1,35) (EB,43) (X2,47) (AB,50) (BD,61)

Output: 1

void f10() Suppose p is the 5th node when performing the in-order traversal of the
tree, count the number of nodes in the subtree with root is Node p then assign that
value to the numberofNode variable;

Your selection (1 -> 10): 10

Input :(A1,1) (A2,3) (BA,5) (B2,25) (G1,35) (EB,43) (X2,47) (AB,50) (BD,61)

Output: 2

int f11() count and return all number of internal Node

Your selection (1 -> 10): 11

Input :(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

output:5

int f12() count and return all number of external Node

Your selection (1 -> 10): 12

Input :(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

output:4

int f13() count and return all Node have both 2 children

Your selection (1 -> 10): 13

Input :(AB,50) (BA,5) (BD,61) (A2,3) (EB,43) (A1,1) (G1,35) (X2,47) (B2,25)

output:3

[email protected] 4

You might also like