Chapter 4 Trees and Graphs - 4.3

这篇博客介绍了如何根据有序数组创建一棵具有最小高度的二叉树。提供了两种构造方法,一种使用广度优先搜索(BFS),另一种使用递归实现。在每种方法中,都展示了如何将数组元素转化为二叉树节点,并通过层次遍历展示生成的二叉树。
摘要由CSDN通过智能技术生成
Problem 4.3 Given a sorted (increasing) array, write an algorithm to create a binary tree with minimal height.

Seems that we should construct a complete binary tree.
from queue import *
class binary_tree_node:
    def __init__(self, value = None):
        self.value = value
        self.left = None
        self.right = None
def construct_min_btree(array):
    # Keep track of current element in the array
    i = 0
    # Construct while doing BFS
    q = queue()
    root = binary_tree_node(array[i])
    i += 1
    q.enqueue(root)
    while not q.is_empty():
        n = q.dequeue()
        # Construct left child
        n.left = binary_tree_node(array[i])
        i += 1
        if i == len(array):
            break
        q.enqueue(n.left)
        # Construct right child
        n.right = binary_tree_node(array[i])
        i += 1
        if i ==  len(array):
            break
        q.enqueue(n.right)
    return root


# Test case
if __name__ == "__main__":
    array = [i for i in range(0, 80)]
    root = construct_min_btree(array)
    # Print the binary tree in level-order
    q = queue()
    q.enqueue(root)
    current_level = 0
    num_in_current_level = 2**current_level
    while not q.is_empty():
        n = q.dequeue()
        print n.value,
        if n.left != None:
            q.enqueue(n.left)
        if n.right != None:
            q.enqueue(n.right)
        num_in_current_level -= 1
        # End of a level
        if num_in_current_level == 0:
            current_level += 1
            num_in_current_level = 2**current_level
            print "\n"

The solution on the answer page constructs a binary search tree with recursion.
from queue import *
class binary_tree_node:
    def __init__(self, value = None):
        self.value = value
        self.left = None
        self.right = None
def construct_min_btree(array, start, end):
    if start > end:
        return
    mid = (start + end)/2
    n = binary_tree_node(array[mid])
    n.left = construct_min_btree(array, start, mid - 1)
    n.right = construct_min_btree(array, mid + 1, end)
    return n

# Test case
if __name__ == "__main__":
    array = [i for i in range(0, 8)]
    root = construct_min_btree(array, 0, 7)
    # Print the binary tree in level-order
    q = queue()
    q.enqueue(root)
    current_level = 0
    num_in_current_level = 2**current_level
    while not q.is_empty():
        n = q.dequeue()
        print n.value,
        if n.left != None:
            q.enqueue(n.left)
        if n.right != None:
            q.enqueue(n.right)
        num_in_current_level -= 1
        # End of a level
        if num_in_current_level == 0:
            current_level += 1
            num_in_current_level = 2**current_level
            print "\n"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值