human
4. 如何找到最佳的分割点序列，其最终分割子集总体最为纯净？
搜索全局最优解在目前还没有有效的方法，所以退一步，我们用贪婪的思想，在每次分割时取最优，希望由局部最优的分割序列能够达到全局最优的效果。
我们使用递归的方法由上而下依次计算，在处理节点顺序时使用深度优先方法组建出决策树。"
Please write code following the instructions in jupyter notebook style.
assistant
#Here is code to 4.

class BinaryNode:
    def __init__(self, samples, max_depth, measure=gini_index):
        self.samples = samples
        self.max_depth = max_depth
        self.measure = measure
        
        self.is_leaf = False
        self.class_ = None
        
        self.left = None
        self.right = None
        
        self.best_split = None
    
    def split(self, depth):
        if depth > self.max_depth:
            self.is_leaf = True
            self.class_ = self.samples["target"].value_counts().argmax()
            return
        
        best_split = find_best_split(self.samples, self.measure)
        if pd.isnull(best_split):
            self.is_leaf = True
            self.class_ = self.samples["target"].value_counts().argmax()
            return

        self.best_split = best_split
        left = self.best_split["child"]["left_nodes"]
        self.left = BinaryNode(left.drop(best_split["feature"], axis=1), self.max_depth)
        
        right = self.best_split["child"]["right_nodes"]
        self.right = BinaryNode(right.drop(best_split["feature"], axis=1), self.max_depth)

        # 先序深度优先
        self.left.split(depth+1)
        self.right.split(depth+1)

binaryNode = BinaryNode(samples, 3)
binaryNode.split(0)

def show(node, depth):
    if node.left:
        show(node.left, depth+1)
    
    if node.is_leaf:
        print("{}{}".format("\t"*(depth+2), node.class_))
        return 
    else:
        print("{}{}: {}".format("\t"*depth,
                                node.best_split["feature"],
                                node.best_split["threshold"]))
    if node.right:
        show(node.right, depth+1)

show(binaryNode, 0)