Verification: a143cc29221c9be0

Php build tree from array

Содержание

C++

#include

using namespace std;

struct Node

{

    int data;

    Node* left, * right;

};

Node* newNode(int data)

{

    Node* node = (Node*)malloc(sizeof(Node));

    node->data = data;

    node->left = node->right = NULL;

    return (node);

}

Node* insertLevelOrder(int arr[], Node* root,

                       int i, int n)

{

    if (i

    {

        Node* temp = newNode(arr[i]);

        root = temp;

        root->left = insertLevelOrder(arr,

                   root->left, 2 * i + 1, n);

        root->right = insertLevelOrder(arr,

                  root->right, 2 * i + 2, n);

    }

    return root;

}

void inOrder(Node* root)

{

    if (root != NULL)

    {

        inOrder(root->left);

        cout data " ";

        inOrder(root->right);

    }

}

int main()

{

    int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };

    int n = sizeof(arr)/sizeof(arr[0]);

    Node* root = insertLevelOrder(arr, root, 0, n);

    inOrder(root);

}

Java

public class Tree {

    Node root;

    static class Node {

        int data;

        Node left, right;

        Node(int data)

        {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }

    public Node insertLevelOrder(int[] arr, Node root,

                                                int i)

    {

        if (i

            Node temp = new Node(arr[i]);

            root = temp;

            root.left = insertLevelOrder(arr, root.left,

                                             2 * i + 1);

            root.right = insertLevelOrder(arr, root.right,

                                               2 * i + 2);

        }

        return root;

    }

    public void inOrder(Node root)

    {

        if (root != null) {

            inOrder(root.left);

            System.out.print(root.data + " ");

            inOrder(root.right);

        }

    }

    public static void main(String args[])

    {

        Tree t2 = new Tree();

        int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };

        t2.root = t2.insertLevelOrder(arr, t2.root, 0);

        t2.inOrder(t2.root);

    }

}

Python3

class newNode:

    def __init__(self, data):

        self.data = data

        self.left = self.right = None

def insertLevelOrder(arr, root, i, n):

    if i

        temp = newNode(arr[i])

        root = temp

        root.left = insertLevelOrder(arr, root.left,

                                     2 * i + 1, n)

        root.right = insertLevelOrder(arr, root.right,

                                      2 * i + 2, n)

    return root

def inOrder(root):

    if root != None:

        inOrder(root.left)

        print(root.data,end=" ")

        inOrder(root.right)

if __name__ == '__main__':

    arr = [1, 2, 3, 4, 5, 6, 6, 6, 6]

    n = len(arr)

    root = None

    root = insertLevelOrder(arr, root, 0, n)

    inOrder(root)

C#

using System;

public class Tree

{

    Node root;

    public class Node

    {

        public int data;

        public Node left, right;

        public Node(int data)

        {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }

    public Node insertLevelOrder(int[] arr,

                            Node root, int i)

    {

        if (i

        {

            Node temp = new Node(arr[i]);

            root = temp;

            root.left = insertLevelOrder(arr,

                            root.left, 2 * i + 1);

            root.right = insertLevelOrder(arr,

                            root.right, 2 * i + 2);

        }

        return root;

    }

    public void inOrder(Node root)

    {

        if (root != null)

        {

            inOrder(root.left);

            Console.Write(root.data + " ");

            inOrder(root.right);

        }

    }

    public static void Main(String []args)

    {

        Tree t2 = new Tree();

        int []arr = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };

        t2.root = t2.insertLevelOrder(arr, t2.root, 0);

        t2.inOrder(t2.root);

    }

}