简体   繁体   English

N-ary树插入和搜索的复杂性是什么?

[英]What is the Complexity of N-ary tree insertion and searching?

I am implementing an N-1ry tree in C#. 我在C#中实现了一个N-1ry树。 I am wondering how can I calculate the complexity of below methods. 我想知道如何计算以下方法的复杂性。 Here is my code: 这是我的代码:

Structure: 结构体:

public class Node
{
    public int Value { get; set; }
    public Node Children { get; set; }
    public Node Sibilings { get; set; }

}

This method for searching: 这种搜索方法:

public Node search(Node root, int data)
{
    if (root == null)
        return null;

    if (data == root.Value)
        return root;

    Node t = search(root.Children, data);
    if (t == null)
        t = search(root.Sibilings, data);
    return t;
}

This method for insertion: 这种插入方法:

public void Add(int[] data)
{
    Node temp = null;
    temp = search(ROOT, data[0]);

    if (temp == null)
        temp = new Node(data[0]);

    if (this.ROOT == null)
        ROOT = temp;
    Node parent = temp;

    for (int j = 1; j <= this.NoOfChildrens; j++)
    {
        // for first child
        if (j == 1)
        {
            parent.Children = new Node(data[j]);
            parent = parent.Children;
        }
        //for all other childs
        else
        {
            parent.Sibilings = new Node(data[j]);
            parent = parent.Sibilings;
        }

    }

}

Program entry point: 计划切入点:

static void Main(string[] args)
{
    NAryTree naryTree = new NAryTree(3);

    // 1st element in each row is node Value,>=2nd....=>value of child

    int[][] data = { new int[] { 1, 2, 3, 4 }, new int[] { 2, 1, 6,0 }, new int[] { 3, 8, 9, 10 }, new int[] { 4, 0, 0, 0 } };

    naryTree.Add(data[0]);
    naryTree.Add(data[1]);
    naryTree.Add(data[2]);
    naryTree.Add(data[3]);
    naryTree.Add(new int[] {10,3,6,4});

    naryTree.preorder(naryTree.ROOT);
    Console.ReadLine();
}

What is the bigO complexity of these methods? 这些方法的复杂性是什么?

Let's see what we have in Search method. 让我们看一下Search方法中的内容。 It is not a binary tree and we have recursion. 它不是二叉树,我们有递归。 So the Search method will call N times till we find a necessary value. 因此, Search方法将调用N次,直到找到必要的值。 So we can conclude that we have O(N) where N is the maximum(worst) number of iteration to find a value at the last iteration: 因此我们可以得出结论,我们有O(N),其中N是在最后一次迭代中找到值的最大(最差)迭代次数:

public Node search(Node root, int data)
{
    if (root == null)
        return null;

    if (data == root.Value)
        return root;

    Node t = search(root.Children, data);
    if (t == null)
        t = search(root.Sibilings, data);
    return t;
} 

For Addition method is simpler as we have for statement and no nested loops. 对于添加方法,因为我们有更简单for语句,并没有嵌套循环。 So we have O(N) for Addition method. 所以我们有O(N) Addition方法。

As Wisconsin university says: 威斯康星大学说:

So for loops for (i = 0; i < N; i++) { sequence of statements } The loop executes N times, so the sequence of statements also executes N times. 因此for循环(i = 0; i <N; i ++){语句序列}循环执行N次,因此语句序列也执行N次。 Since we assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall. 由于我们假设语句是O(1),for循环的总时间是N * O(1),总体上是O(N)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM