如何使用给定的 class 从字符串数组实现二叉树,然后对其进行序列化、反序列化和遍历?
How to implement a binary tree from a string array using a given class and then serialize, deserialize, and traverse it?
我有一个数据结构编码项目class,但我不知道从哪里开始。作业是这样问的:
Input your binary tree as an array, using the array representation and node labels A, ..., J, as Strings. Label null stands for a non-existent node, not for a node having a value of null. Check the validity of your binary tree input: each node, excepting the root, should have a father. Generate the dynamic memory implementation of the tree, using only the nodes with labels different than null. Save the obtained BinaryTreeobject as a file, using serialization. Deserialize the file to restore the tree. Perform a preorder, a postorder, and an inorder tree traversal of the restored tree and list the labels of the visited nodes. Create unit tests and implement a test class.
给我一棵二叉树class:
public class BinaryTree<T> implements java.io.Serializable
{
private T data;
private BinaryTree<T> left;
private BinaryTree<T> right;
public BinaryTree(T data)
{
this.data = data;
left = null;
right = null;
}
public T getData()
{
return data;
}
public void attachLeft(BinaryTree<T> tree)
{
if (tree != null) left = tree;
}
public void attachRight(BinaryTree<T> tree)
{
if (tree != null) right = tree;
}
public BinaryTree<T> detachLeft()
{
BinaryTree<T> t = left;
left = null;
return t;
}
public BinaryTree<T> detachRight()
{
BinaryTree<T> t = right;
right = null;
return t;
}
public boolean isEmpty()
{
return data == null;
}
public void inOrder(BinaryTree <T> tree)
{
if ( tree != null)
{
inOrder(tree.left);
System.out.println(tree.getData());
inOrder(tree.right);
}
}
public void preOrder(BinaryTree <T> tree)
{
}
public void postOrder(BinaryTree <T> tree) {
}
}
我希望尽可能将其分解为更小的步骤,因为我不确定从哪里开始。另外,我没有序列化经验。
我不是要代码,只是一个指南。
假设字符串索引与节点的关系是left child = 2 * parent index + 1
和right child = 2 * parent index + 2
.
现在字符串以 "A, B, ..., J"
的形式给出,您可以将字符串拆分为一个数组,其中 arr[0] = A
和 arr[N] = J
每个元素本身就是一棵大小为1的树,它们是包含所有元素的大树的子树。
根据索引,迭代或递归添加到一棵大树中。例如,arr[0] = A = root
、arr[1] = left child = B // because 1 = 2 * 0 + 1
、arr[2] = right child = C // because 2 = 2 * 0 + 2
等。忽略空节点,现在你有了最终的树。
我有一个数据结构编码项目class,但我不知道从哪里开始。作业是这样问的:
Input your binary tree as an array, using the array representation and node labels A, ..., J, as Strings. Label null stands for a non-existent node, not for a node having a value of null. Check the validity of your binary tree input: each node, excepting the root, should have a father. Generate the dynamic memory implementation of the tree, using only the nodes with labels different than null. Save the obtained BinaryTreeobject as a file, using serialization. Deserialize the file to restore the tree. Perform a preorder, a postorder, and an inorder tree traversal of the restored tree and list the labels of the visited nodes. Create unit tests and implement a test class.
给我一棵二叉树class:
public class BinaryTree<T> implements java.io.Serializable
{
private T data;
private BinaryTree<T> left;
private BinaryTree<T> right;
public BinaryTree(T data)
{
this.data = data;
left = null;
right = null;
}
public T getData()
{
return data;
}
public void attachLeft(BinaryTree<T> tree)
{
if (tree != null) left = tree;
}
public void attachRight(BinaryTree<T> tree)
{
if (tree != null) right = tree;
}
public BinaryTree<T> detachLeft()
{
BinaryTree<T> t = left;
left = null;
return t;
}
public BinaryTree<T> detachRight()
{
BinaryTree<T> t = right;
right = null;
return t;
}
public boolean isEmpty()
{
return data == null;
}
public void inOrder(BinaryTree <T> tree)
{
if ( tree != null)
{
inOrder(tree.left);
System.out.println(tree.getData());
inOrder(tree.right);
}
}
public void preOrder(BinaryTree <T> tree)
{
}
public void postOrder(BinaryTree <T> tree) {
}
}
我希望尽可能将其分解为更小的步骤,因为我不确定从哪里开始。另外,我没有序列化经验。
我不是要代码,只是一个指南。
假设字符串索引与节点的关系是
left child = 2 * parent index + 1
和right child = 2 * parent index + 2
.现在字符串以
"A, B, ..., J"
的形式给出,您可以将字符串拆分为一个数组,其中arr[0] = A
和arr[N] = J
每个元素本身就是一棵大小为1的树,它们是包含所有元素的大树的子树。
根据索引,迭代或递归添加到一棵大树中。例如,
arr[0] = A = root
、arr[1] = left child = B // because 1 = 2 * 0 + 1
、arr[2] = right child = C // because 2 = 2 * 0 + 2
等。忽略空节点,现在你有了最终的树。