递归族谱与对象

时间:2018-11-27 17:53:56

标签: python-3.x object recursion family-tree

您好,我需要递归表示家谱的帮助,这是数据:

class RoundDisplay: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        arrayOfCellData.append(roundData(id : arrayOfCellData.count + 1, date : datePlayedFC, course : currentCourse, score : String(score1234) ))

        tableView.reloadData()

        print(arrayOfCellData)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        //Shows how many cells it should display; number of current cells
        return arrayOfCellData.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //Shows how many cells it should display; number of current cells
       return arrayOfCellData.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            //Defines what xib to use for cells
            let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell



            //Adds array data to each cell
            cell.DateLbl.text = arrayOfCellData[indexPath.row].date
            cell.CourseName.text = arrayOfCellData[indexPath.row].course
            cell.ScoreLbl.text = arrayOfCellData[indexPath.row].score

            return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //Shows how high to make cells; height of xib
        return 68
    }
}

我需要提到这些值是对象,因此我需要将它们称为children_and_parents [“ Maria”]。child-以获取['Patricia','Lisa']

我当前拥有的递归程序

children_and_parents = {
"Mary": ["Patricia", "Lisa"], 
"Patricia": ["Barbara", "Helen", "Maria"], 
"Maria": ["Keren", "Carol"], 
"Barbara": ["Betty"]
}

目前正在做什么

def draw_family_tree(person, level=0):
if person in children_and_parents:
    for i in range (len(children_and_parents[person].child)):
        print (" "*level, person)
        return draw_family_tree(children_and_parents[person].child[i], level+3) 

但结果应该类似于

Mary
   Patricia
      Barbara

因此,如果有人愿意提供帮助,我真的很想了解该程序的请求

粗略代码https://repl.it/repls/BlondCavernousExponents

1 个答案:

答案 0 :(得分:2)

找到树的根是进行单独操作的不错选择。在您的示例中,我们知道它是<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" charset="utf-8"> function doThisWhenLoaded(){ $("body").append("<p>Yes it did.</p>"); } </script> </head> <body onload="doThisWhenLoaded()"> <h1 style="font-family:sans-serif;">Did it work?</h1> </body> </html>,因此我们可以进行相应的迭代。如果我们不知道(并且假设dict中的第一项是根是不安全的),则可以编写:

"Mary"

对于实际的打印过程,请在遍历子节点之前尝试打印父节点。我还建议将树作为参数传递给函数,以保持封装并使其可重用(即,不依赖于调用范围中存在的名为def find_root(tree): children = set([x for y in tree.values() for x in y]) for k in tree.keys(): if k not in children: return k 的变量。

children_and_parents

输出:

def draw_family_tree(tree, root, level=0, gap=3):
    if root:
        print(" " * level + root)

        if root in tree:
            for child in tree[root]:
                draw_family_tree(tree, child, level + gap)

Try it!

正如前面的讨论中所提到的,我不建议为简单的Mary Patricia Barbara Betty Helen Maria Keren Carol Lisa 对使用类。它增加了很多没有功能的冗长性,并且实际上有点误导,因为<string, list>误导性暗示该人有父母(实际上是指对象所代表的人的名字)。如果您确实选择了这种方法,则需要将parent附加到所有数组访问中,并为您的类(或.child)编写一个__repr__(self)函数。