给定深度的Swift二进制树节点列表

时间:2017-02-16 04:01:43

标签: swift binary-tree

我正在为二叉树编写Swift算法。我的目标是在特定深度创建一个节点列表,例如

func listNodeAt(_n: Int) --> [T] {

}

这是我的树类

public class BinaryTreeNode<T:Comparable> {

    //Value and children vars
    public var value:T
    public var leftChild:BinaryTreeNode?
    public var rightChild:BinaryTreeNode?
    public weak var parent:BinaryTreeNode?

    //Initialization
    public convenience init(value: T) {
        self.init(value: value, left: nil, right: nil, parent:nil)
    }

    public init(value:T, left:BinaryTreeNode?, right:BinaryTreeNode?, parent:BinaryTreeNode?) {
        self.value = value
        self.leftChild = left
        self.rightChild = right
        self.parent = parent
    }
}

我已经构建了一个辅助函数来计算节点的深度

//Depth
    public func depth() -> Int {
        guard var node = parent else {
            return 0
        }

        var depth = 1
        while let parent = node.parent {
            depth = depth + 1
            node = parent
        }

        return depth
    }

我们如何实现理想的功能?

1 个答案:

答案 0 :(得分:2)

$fulloutput=@()

foreach ($File in GCI C:\*.txt){
$output=$null
$output+=$file.name + ","
    foreach ($result in (gc $file)){
        $output+=$result | ?{$_ -like "OS details*" -or $_ -like "Aggressive OS*"}
    }
    $fulloutput+=$output
}
$fulloutput | Export-Csv C:\nmap.csv

这是解决方案之一。假设self是根节点。

相关问题