Chapter 4 Trees and Graphs - 4.3
来源:互联网 发布:moto2008概预算软件 编辑:程序博客网 时间:2024/04/28 06:46
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.
The solution on the answer page constructs a binary search tree with recursion.
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 = Nonedef 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 caseif __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 = Nonedef 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 caseif __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"
- Chapter 4 Trees and Graphs - 4.3
- Chapter 4 Trees and Graphs - 4.1
- Chapter 4 Trees and Graphs - 4.2
- Chapter 4 Trees and Graphs - 4.4
- Chapter 4 Trees and Graphs - 4.5
- CareerCup Chapter 4 Trees and Graphs
- Chapter 4 | Trees and Graphs--判断有向图两节点之间是否存在路径
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- CC chapter 4 Tree and Graphs ----Tree
- Chapter 4 | Trees and Graphs--检查一棵树是否平衡以及判断一棵树是否为平衡二叉树
- 【CareerCup】Trees and Graphs—Q4.1
- 【CareerCup】Trees and Graphs—Q4.2
- 【CareerCup】Trees and Graphs—Q4.3
- Cracking the Coding Interview: Trees and Graphs
- Python Algorithms Learning Notes(2)—Implementing Graphs and Trees
- !!!Chapter 4 Trees BST/AVLT
- !!!Chapter 4 Trees ST/BT
- 在uitabbarviewcontroller的子视图间相互访问,以及在uitabbarviewcontroller中访问其子视图
- Android中调用外部地图程序
- Chapter 4 Trees and Graphs - 4.2
- Effective C++ 类与函数的设计和申明
- C#文件操作
- Chapter 4 Trees and Graphs - 4.3
- windows下android 环境搭建的软件版本
- How can I invoke functions on QObjects from another thread?
- 情感化设计
- SAP PI BIT400培训笔记
- 北京暴雨
- Chapter 4 Trees and Graphs - 4.4
- js+xml 省市级联以及修改默认选中
- cocos2d-x总结-win32环境下响应键盘消息