2進検索ツリーから再帰を使用して2重リンクリストを作成しています。 しかし、私はBSTのためにそれを実行しようとしましたが、動的に取り込まれて、私はBSTのルートノードに子を挿入するとすぐに私にStackOverFlowErrorを与えます。 ここでは(Javaで)私は2進検索ツリーから2重リンクリストへ
public class BSTtoDLL {
/* Binary Search Tree to Doubly Linked List conversion*/
// head --> Pointer to head node of created doubly linked list
static BTNode head;
// Initialize previously visited node as NULL. This is
// static so that the same value is accessible in all recursive
// calls
static BTNode prev = null;
/* BSTtoDLL Construtor */
public BSTtoDLL(){
head = null;
prev = null;
}
// A simple recursive function to convert a given Binary tree
// to Doubly Linked List
// root --> Root of Binary Tree
void BinaryTree2DoubleLinkedList(BTNode root)
{
// Base case
if (root == null)
return;
// Recursively convert left subtree
if(root.left!=null)
BinaryTree2DoubleLinkedList(root.left);
// Now convert this node
if (prev == null){
head = root;
}
else
{
prev.right = root;
root.left = prev;
}
prev = root;
// Finally convert right subtree
BinaryTree2DoubleLinkedList(root.right);
}
とコンソールレスポンス書いたコードです:
Binary Tree Test
Converting to a DLL
Data--34--Left is Null----Right is Null---
Binary Tree Test Converting to a DLL Exception in thread "main" java.lang.StackOverflowError
at com.techwealth.BSTtoDLL.BinaryTree2DoubleLinkedList(BSTtoDLL.java:32)
at com.techwealth.BSTtoDLL.BinaryTree2DoubleLinkedList(BSTtoDLL.java:32)
at com.techwealth.BSTtoDLL.BinaryTree2DoubleLinkedList(BSTtoDLL.java:32)
at com.techwealth.BSTtoDLL.BinaryTree2DoubleLinkedList(BSTtoDLL.java:32)
at com.techwealth.BSTtoDLL.BinaryTree2DoubleLinkedList(BSTtoDLL.java:32)