【问题标题】:Converting binary search tree to linked list将二叉搜索树转换为链表
【发布时间】:2017-03-30 01:26:55
【问题描述】:

我正在尝试将二叉搜索树转换为链表。链表应该在前面有较小的数字,在后面有较大的数字(从小到大)。我需要创建一个接收二叉搜索树并输出链表的函数。

        4
      /   \
     2     6
   /  \   / \
  1   3  5   7 

这是我的二叉搜索树。我需要这样制作链表:1、2、3、4、5、6、7。这是我当前的代码:

node<int>* makeLinkedList(binary_tree_node<int>* root_ptr);

int main()
{
    binary_tree_node<int> *s1 = sample1();  //Creates the tree

    node<int>* node1 = makeLinkedList(s1);  //invokes function to make a linked list

    //Loop to print out the values of the linked list
    for (node1; node1 != NULL; node1 = node1->link()){
        cout<<node1->data()<<endl;
    }
}

node<int>* makeLinkedList(binary_tree_node<int>* root_ptr){
    node<int>* left;

    if (root_ptr == NULL) {
            return NULL;
    }

    else{

        left = makeLinkedList(root_ptr->left()); 
        list_head_insert(left, root_ptr->data());  //list_head_insert inserts a new entry at the head of the linked list
        return left;
    }
}

当我运行我的代码时,输​​出是 4、2、1。我只是不明白如何将这个二叉搜索树转换为从最小到最大的链表。我尝试将 list_head_insert 函数放在递归调用之上,但输出什么都没有,列表为空。

【问题讨论】:

    标签: c++ recursion binary-tree binary-search-tree


    【解决方案1】:
    
    class HeadAndTail<T> {
        LinkedListNode<T> head;
        LinkedListNode<T> tail;
    
        public HeadAndTail() {
            this.head = null;
            this.tail = null;
        }
    
    }
    
    public class Solution {
    
         public static LinkedListNode<Integer> constructLinkedList(BinaryTreeNode<Integer> root) {
            return construct(root).head;
        }
    
        public static HeadAndTail<Integer> construct(BinaryTreeNode<Integer> root) {
    
            if(root == null) {
                return new HeadAndTail<>();
            }
    
            if(isLeafNode(root)) {
                LinkedListNode<Integer> node = new LinkedListNode<>(root.data);
                HeadAndTail<Integer> headAndTail = new HeadAndTail<>();
                headAndTail.head = node;
                headAndTail.tail = node;
                return headAndTail;
            }
    
            LinkedListNode<Integer> node = new LinkedListNode<>(root.data);
            HeadAndTail<Integer> forLeftPart = construct(root.left);
            HeadAndTail<Integer> forRightPart = construct(root.right);
    
            node.next = forRightPart.head;
            HeadAndTail<Integer> headAndTail = new HeadAndTail<>();
    
            //if left part is null
            if(forLeftPart.tail != null) {
                forLeftPart.tail.next = node;
                headAndTail.head = forLeftPart.head;
            } else{
                headAndTail.head = node;
            }
    
            //if right part is null
            if(forRightPart.tail != null) {
                headAndTail.tail = forRightPart.tail;
            } else {
                headAndTail.tail = node;
            }
    
            return headAndTail;
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      所以换句话说,你希望它在你的链表中以排序的形式出现。

      有一种遍历方式:按顺序遍历。见here 有关遍历的更多信息。

      那么如何做到这一点呢?作为提示,我将为您提供一个按顺序“打印”此 BST 内容的函数。那应该让你滚动。 (在弄清楚如何按顺序获取树的内容之后,您所要做的就是调用一个插入函数到您的列表中)

      void print_in_order(Node* t) {
          if(!t)
              return;
          print_in_order(t->left);
          cout << t->data;
          print_in_order(t->right);
      }
      

      【讨论】:

      • 我了解顺序遍历以及如何打印树的内容,但我不知道如何将这些节点的内容插入到我的链表中。
      • 这很简单。使用上面的方法,经过一些修改,您可以将元素添加到列表中。只需在以这种递归方式获取元素后,调用一个有助于将元素插入列表的函数。作为一个额外的提示,你什么时候真正从树中获得一个元素?你真的必须一直走到 t 为空吗?什么时候可以停下来?
      • makeLinkedList(root_ptr-&gt;left()); list_head_insert(list2, root_ptr-&gt;data()); makeLinkedList(root_ptr-&gt;right()); return list2; 这就是我现在所拥有的,程序可以编译,但由于某些问题它退出了。这就是你所说的吗?
      • 是的,应该可以。确保检查收到的指针是否为空。
      • 我有一个基本情况语句来检查指针是否为空,如下所示:if (root_ptr == NULL) { return NULL; }这是你的意思吗?
      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多