栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

binarytree构建二叉树堆,python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

binarytree构建二叉树堆,python

binarytree用来构建二叉树堆很便利,比如:

from binarytree import tree, Node, build, get_parent


def app():
    my_tree = tree(height=3, is_perfect=False)
    print(my_tree.pprint(index=True))

    print('-')
    root = Node(1)  # index: 0, value: 1
    root.left = Node(2)  # index: 1, value: 2
    root.right = Node(3)  # index: 2, value: 3
    root.left.right = Node(value=4, left=Node(5), right=Node(6))  # index: 4, value: 4
    root.pprint(index=True)

    print('--')
    print(root)

    print('---')
    root.left.left = Node(15)
    print(root)

    print('----')
    root.right.right = root.left
    print(root)

    print('-----')
    root2 = build([1, 2, 3, 4, None, 5])
    print(root2)
    print('------')
    print(get_parent(root2, root2.left.left))


if __name__ == '__main__':
    app()

输出:

       _________0-9_________________
      /                             
   _1-7_____                ________2-8_
  /                       /            
3-4        _4-6        __5-12__         6-10_
          /           /                     
        9-0         11-3      12-11          14-2

None
-

   ______________0-1_
  /                  
1-2_____             2-3
        
       _4-4_
      /     
    9-5     10-6

--

  ______1
 /       
2__       3
   
    4
   / 
  5   6

---

     ______1
    /       
  _2__       3
 /    
15     4
      / 
     5   6

----

     ______1
    /       
  _2__       3___
 /              
15     4         _2__
      /        /    
     5   6     15     4
                     / 
                    5   6

-----

    1__
   /   
  2     3
 /     /
4     5

------

  2
 /
4

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/488673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号