Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

my code: public String serialize() { return serialize(root); } private String serialize(BTNode node) { if (node == null) { return X(NULL); } if (node.getLeft() ==

image text in transcribed my code:

public String serialize() { return serialize(root); }

private String serialize(BTNode node) { if (node == null) { return "X(NULL)"; } if (node.getLeft() == null && node.getRight() == null) { return "L(" + node.getKey() + ")"; } if (node.getLeft() == null || node.getRight() == null) { return "X(" + node.getKey() + ")," + serialize(node.getLeft() != null ? node.getLeft() : node.getRight()); } String left = serialize(node.getLeft()); String right = serialize(node.getRight()); return "I(" + node.getKey() + ")," + left + "," + right; } Tester:

package cs1501_p1;

public class BST> implements BST_Inter { private BTNode root = null;

public void put(T key) // Add a new key to the BST { BTNode node = root; newNode.setLeft(reverse(reversed.getRight())); newNode.setRight(reverse(reversed.getLeft())); return newNode; } } output:

Successfully built an empty tree! FAILED Expected: R(10),I(5),L(2),X(NULL),I(37),X(NULL),L(45) Actual: X(45),X(37),X(10),X(2),L(5) please fix and help!!!

serialize() : Perform a pre-order traversal of the BST in order to produce a String representation of the BST. The reprsentation should be a comma separated list where each entry represents a single node. Each entry should take the form: type(key). You should track 4 node types: R : The root of the tree - I: An interior node of the tree (e.g., not the root, not a leaf) - L: A leaf of the tree - x : A stand-in for a null reference For each node, you should list its left child first, then its right child. You do not need to list children of leaves. The x type is only for nodes that have one valid child. The key for an x node should be NULL (all caps). Consider the following

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions