简体   繁体   中英

Unsorted Binary Search Trees, Traversal, Size

  1. Create a function traverse which takes 4 args the first one being the tree, the second is empty_fn() , leaf_fn(arg) , third is node_fn(arg0,arg1,arg2) if tree is empty the function empty_fn() which takes 0 args should called. And so if we encounter a leaf the leaf_fn(arg) should be run, and if we have a subtree the function node_fn(arg0,arg1,arg2) should be run.

  2. Create the function contains_key which calls traverse() and checks if a key exists in the a given binary tree. This function is essentially intended to create the three functions that traverse uses as arguments.

  3. create a new function tree_size() which calls the traverse() function and returns the size of a tree.

Note: the tree doesn't necessarily have to be sorted in a correct way for example [[3,5,4],1,0] is a valid tree

Example

def empty_fn():
    return 0


def leaf_fn(key):
    return key**2


def node_fn(key, left_value, right_value): 
    return key + left_value
>>> traverse([6, 7, 8], inner_node_fn, leaf_fn, empty_tree_fn)
43

Here are my attempts to solve the problem, given the sample of the program running given by the specifications:

def traverse(tree,empty_tree_fn,leaf_fn, inner_node_fn ):
    if is_empty_tree(tree):
        return empty_tree_fn()
    else:
        if is_leaf(tree[0]):
            tree[0]=leaf_fn(tree[0])

        elif is_leaf(tree[2]):
            tree[2]=leaf_fn(tree[2])
        return inner_node_fn(tree[1],tree[0],tree[2])

if i run it against the input given by the example i get the same output which means this is the way to do it right? it however gets more complicated once we jump into the second part of the problem as i hade to create a new version of traverse() namley traverse2_0 to meet the requirements of question 1. Here is my code:

def traverse2_0(tree,empty_tree_fn,leaf_fn, inner_node_fn ):
    if is_empty_tree(tree):
        return empty_tree_fn()
    else:
        """if is_leaf(tree[0]) and is_leaf(tree[2]):
            return leaf_fn(tree[0]) or leaf_fn(tree[2])""" #lazy mechanism
        if is_leaf(tree[0]):
            if leaf_fn(tree[0]):
                return True
        if is_leaf(tree[2]):
            if leaf_fn(tree[2]):
                return True
        else:
            return inner_node_fn(tree[1],tree[0],tree[2])
    return False
def contains_key(key, tree):
    #print (tree)
    def empty_fn(tree):
        return not is_empty_tree(tree)
    def leaf_fn(side):
        return side==key
    def inner_node_fn(k,left,right):
        if  isinstance(left,list) and isinstance(right,list):
            return contains_key(key, left) or contains_key(key, right)
        elif  isinstance(left,list):
            return contains_key(key,left)
        elif isinstance(right,list):
            return contains_key(key, right)
    if key==tree[1]:
        return True
    else:
        return traverse2_0(tree,empty_fn,leaf_fn,inner_node_fn)

And once we get to the third is even more complicated if I wanna use traverse() so i had to solve it recursively instead. HOWEVER neither of my solutions except for the first one meets the requirements for given by the questions according to my LI I feel like there is no way to meet all three requirements given the example.

def tree_size(tree):
    if not tree: #corresponds to empty_tree_fn
        return 0
    if isinstance(tree[0],list): #corresponds to inner_node_fn
        return tree_size(tree[0])+tree_size(tree[1:])
    else:
        return 1+tree_size(tree[1:]) #corresponds to leaf_fn
print (tree_size( [[0,1,2],2,[1,3,2]]))

This is a very long question, I am aware of that and thankful for any relevant answer.

from tree import *
def traverse(bst,empty_fn,leaf_fn,inner_node_fn):
    if is_empty_tree(bst):
        return empty_fn()
    else:
        left,root,right= bst[0],bst[1],bst[2]
        if is_leaf(left):
            left= leaf_fn(left)
        if is_leaf(right):
            right=leaf_fn(right)
        return inner_node_fn(root,left, right)
def contains_key(key, tree):
    def empty_fn():
        return not is_empty_tree(tree)
    def leaf_fn(side):
        return side==key
    def inner_node_fn(k,left,right):
        if k==key:
            return True
        if isinstance(left,list) and isinstance(right,list):
            return contains_key(key,left) or contains_key(key,right)
        elif isinstance(right,list):
            return contains_key(key,right)
        elif isinstance (left,list):

            return contains_key(key,left)
        else:
            return right or left

    return traverse(tree,empty_fn,leaf_fn,inner_node_fn)

print(contains_key("m", [["m",6,4], 7, [[2, 3, 4], 0, []]]))

The solution wont work for nested subtrees though, would appreciate any feedback though. I am leaving the question open to hopefully receive more answers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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