Java教程

Java从三元树创建双向链接列表

在此程序中,给定的三叉树将转换为相应的双向链表。
三元树是一个分层的数据结构,其中每个节点最多可以有三个孩子。这可以通过以一种顺序方式遍历三叉树来完成,即根->左子->中间子->右子。首先,遍历根节点并将其添加到列表中。然后,分别添加其左,中和右子树。
三叉树:
Java程序从三元树创建双向链接列表
对应的双向链接列表:
Java程序,用于从三元树创建双向链接列表

算法

定义一个Node类,该类代表三叉树中的一个节点。它具有四个属性: data,left,middle和right,其中left,middle和right代表节点的三个子代。 根将代表三元树的根。头和尾节点代表双向链表的头和尾。 convertTernaryToDLL()将给定的三叉树转换为相应的双向链表。 左,中和右节点代表给定节点的子节点。 如果节点不为空,则该节点将被插入列表。 如果列表为空,头和尾都将指向一个节点。 如果列表不为空,则该节点将插入列表的末尾。在这里,左,右指针将代表双向链表的上一个和下一个指针。中间不会指向任何内容,因此,只需将其设置为null。 将节点成功添加到列表中后,递归调用convertTernaryToDLL()以将给定节点的左,中和右子级添加到列表中。
a.displayDLL()将显示列表中存在的所有节点。
定义一个新节点"当前",该节点将指向头部。 打印current.data直到current指向null。 当前每次迭代将指向列表中的下一个节点。

程序:

public class TernaryTreeToDLL {
    //Represent a node of ternary tree
    public static class Node{
        int data;
        Node left;
        Node middle;
        Node right;
        public Node(int data) {
            this.data = data;
        }
    }
    //Represent the root of the ternary tree
    public Node root;
    //Represent the head and tail of the doubly linked list
    Node head, tail = null;
    //convertTernaryToDLL() will convert the given ternary tree to corresponding doubly linked list
    public void convertTernaryToDLL(Node node) {
        //Checks whether node is null
        if(node == null)
            return;
        //Keep three pointers to all three children
        Node left = node.left;
        Node middle = node.middle;
        Node right = node.right;
        //if list is empty then, add node as head of the list
        if(head == null) {
            //Both head and tail will point to node
            head = tail = node;
            node.middle = null;
            //head's left will point to null
            head.left = null;
            //tail's right will point to null, as it is the last node of the list
            tail.right = null;
        }
        //Otherwise, add node to the end of the list
        else {
            //node will be added after tail such that tail's right will point to node
            tail.right = node;
            //node's left will point to tail
            node.left = tail;
            node.middle = null;
            //node will become new tail
            tail = node;
            //As it is last node, tails right will point to null
            tail.right = null;
        }
        //Add left child of current node to the list
        convertTernaryToDLL(left);
        //Then, add middle child of current node to the list
        convertTernaryToDLL(middle);
        //Then, add right child of current node to the list
        convertTernaryToDLL(right);
    }
    //displayDLL() will print out the nodes of the list
    public void displayDLL() {
        //Node current will point to head
        Node current = head;
        if(head == null) {
            System.out.println("List is empty");
            return;
        }
        System.out.println("Nodes of generated doubly linked list: ");
        while(current != null) {
            //Prints each node by incrementing the pointer.
            System.out.print(current.data + " ");
            current = current.right;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        TernaryTreeToDLL tree = new TernaryTreeToDLL();
        //Add nodes to the ternary tree
        tree.root = new Node(5);
        tree.root.left = new Node(10);
        tree.root.middle = new Node(12);
        tree.root.right = new Node(15);
        tree.root.left.left = new Node(20);
        tree.root.left.middle = new Node(40);
        tree.root.left.right = new Node(50);
        tree.root.middle.left = new Node(24);
        tree.root.middle.middle = new Node(36);
        tree.root.middle.right = new Node(48);
        tree.root.right.left = new Node(30);
        tree.root.right.middle = new Node(45);
        tree.root.right.right = new Node(60);
        //Converts the given ternary tree to doubly linked list
        tree.convertTernaryToDLL(tree.root);
        //Displays the nodes present in the list
        tree.displayDLL();
    }
}
输出:
Nodes of generated doubly linked list:
5 10 20 40 50 12 24 36 48 15 30 45 60
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4