python建立二叉树并且可视化

来源:互联网 发布:淘宝缴纳保证金 编辑:程序博客网 时间:2024/06/05 11:56

定义节点

class node:    def __init__(self, id, people_number, distance, left_id=None, right_id=None):        self.id = id        self.people_number = people_number        self.distance = distance        self.left_id = left_id        self.right_id = right_id

从数据库中查找数据构建树

import cx_Oracle import treenode as tndef connect_oracle():    connect = cx_Oracle.connect('nyns/nyns@192.168.0.11/orcl')    return connectdef build_tree():    cyzddm = 'G45.901'    sql = 'select id,left_id,right_id,zrs,distance from cluster_result where cyzddm=''\''+cyzddm+'\''    print(sql)    connect = connect_oracle()    cursor = connect.cursor()    cursor.execute(sql)    result = cursor.fetchall()    map={}    for id in result:        node=tn.node(id[0],id[3],round(id[4],3),id[1],id[2])        map[id[0]]=node    return map

画出图片

import pygraphviz as pgvimport connectorcle as comap = co.build_tree()root = str(len(map) * 2 + 1)# 进行树的遍历A = pgv.AGraph(directed=True, strict=True)nodes = []nodes.append(root)while len(nodes) != 0: son_node = [] for node in nodes:   if map[node].left_id in map and map[map[node].left_id].people_number > 100 and map[node].distance > 0.45:      father = str(map[node].id) + '_' + str(map[node].people_number) + '_' + str(map[node].distance)      left_son = str(map[map[node].left_id].id) + '_' + str(map[map[node].left_id].people_number) + '_' + str(map[map[node].left_id].distance)      A.add_edge(father, left_son)      son_node.append(map[node].left_id)   if map[node].right_id in map and map[map[node].right_id].people_number > 100 and map[node].distance > 0.45:      father = str(map[node].id) + '_' + str(map[node].people_number) + '_' + str(map[node].distance)      right_son = str(map[map[node].right_id].id) + '_' + str(map[map[node].right_id].people_number) + '_' + str(map[map[node].right_id].distance)      A.add_edge(father, right_son)      son_node.append(map[node].right_id) nodes = son_nodeA.graph_attr['epsilon'] = '0.01'print (A.string())  # print dot file to standard outputA.write('fooOld.dot')A.layout('dot')  # layout with dotA.draw('D:/python_noregularweightpathway/tia/b.png')  # write to file

实验结果:

select id,left_id,right_id,zrs,distance from cluster_result where cyzddm='G45.901'strict digraph {    graph [epsilon=0.01];    "12545_6273_0.967" -> "12544_6270_0.923";    "12544_6270_0.923" -> "12543_6268_0.879";    "12543_6268_0.879" -> "12542_6262_0.867";    "12542_6262_0.867" -> "12541_6241_0.84";    "12541_6241_0.84" -> "12537_6238_0.789";    "12537_6238_0.789" -> "12535_6236_0.779";    "12535_6236_0.779" -> "12534_6232_0.777";    "12534_6232_0.777" -> "12533_6149_0.769";    "12533_6149_0.769" -> "12531_6148_0.766";    "12531_6148_0.766" -> "12529_6144_0.755";    "12529_6144_0.755" -> "12527_6139_0.754";    "12527_6139_0.754" -> "12525_6137_0.75";    "12525_6137_0.75" -> "12523_6136_0.744";    "12523_6136_0.744" -> "12521_6135_0.742";    "12521_6135_0.742" -> "12520_6132_0.74";    "12520_6132_0.74" -> "12516_6131_0.73";    "12516_6131_0.73" -> "12515_6129_0.727";    "12515_6129_0.727" -> "12513_6126_0.721";    "12513_6126_0.721" -> "12511_6125_0.719";    "12511_6125_0.719" -> "12510_6122_0.719";    "12510_6122_0.719" -> "12507_6117_0.715";    "12507_6117_0.715" -> "12504_6115_0.713";    "12504_6115_0.713" -> "12501_6087_0.705";    "12501_6087_0.705" -> "12500_6085_0.704";    "12500_6085_0.704" -> "12497_6078_0.701";    "12497_6078_0.701" -> "12495_6048_0.7";    "12495_6048_0.7" -> "12494_6036_0.699";    "12494_6036_0.699" -> "12493_6034_0.697";    "12493_6034_0.697" -> "12491_6022_0.694";    "12491_6022_0.694" -> "12488_6018_0.693";    "12488_6018_0.693" -> "12487_6007_0.693";    "12487_6007_0.693" -> "12482_5964_0.69";    "12482_5964_0.69" -> "12481_5959_0.687";    "12481_5959_0.687" -> "12477_5955_0.684";    "12477_5955_0.684" -> "12476_5954_0.684";    "12476_5954_0.684" -> "12475_5951_0.683";    "12475_5951_0.683" -> "12472_5949_0.681";    "12472_5949_0.681" -> "12466_5945_0.678";    "12466_5945_0.678" -> "12465_5940_0.678";    "12465_5940_0.678" -> "12463_5935_0.675";    "12463_5935_0.675" -> "12462_5929_0.674";    "12462_5929_0.674" -> "12461_5923_0.674";    "12461_5923_0.674" -> "12459_5921_0.674";    "12459_5921_0.674" -> "12457_5911_0.673";    "12457_5911_0.673" -> "12456_5910_0.672";    "12456_5910_0.672" -> "12455_5907_0.672";    "12455_5907_0.672" -> "12454_5904_0.672";    "12454_5904_0.672" -> "12450_5891_0.669";    "12450_5891_0.669" -> "12443_5887_0.665";    "12443_5887_0.665" -> "12441_5872_0.661";    "12441_5872_0.661" -> "12437_5861_0.659";    "12437_5861_0.659" -> "12429_5853_0.656";    "12429_5853_0.656" -> "12428_5845_0.656";    "12428_5845_0.656" -> "12425_5842_0.654";    "12425_5842_0.654" -> "12424_5841_0.653";    "12424_5841_0.653" -> "12420_5839_0.653";    "12420_5839_0.653" -> "12417_5836_0.651";    "12417_5836_0.651" -> "12416_5746_0.651";    "12416_5746_0.651" -> "12414_5744_0.649";    "12414_5744_0.649" -> "12413_5733_0.649";    "12413_5733_0.649" -> "12411_5730_0.648";    "12411_5730_0.648" -> "12408_5710_0.646";    "12408_5710_0.646" -> "12405_5697_0.646";    "12405_5697_0.646" -> "12402_5681_0.645";    "12402_5681_0.645" -> "12399_5675_0.644";    "12399_5675_0.644" -> "12398_5656_0.643";    "12398_5656_0.643" -> "12393_5637_0.641";    "12393_5637_0.641" -> "12391_5622_0.639";    "12391_5622_0.639" -> "12387_5548_0.638";    "12387_5548_0.638" -> "12385_5533_0.638";    "12385_5533_0.638" -> "12383_5515_0.637";    "12383_5515_0.637" -> "12378_5513_0.636";    "12378_5513_0.636" -> "12374_5509_0.635";    "12374_5509_0.635" -> "12373_5476_0.635";    "12373_5476_0.635" -> "12370_5472_0.633";    "12370_5472_0.633" -> "12366_5457_0.632";    "12366_5457_0.632" -> "12363_5441_0.631";    "12363_5441_0.631" -> "12352_5395_0.629";    "12352_5395_0.629" -> "12350_5389_0.628";    "12350_5389_0.628" -> "12345_5381_0.627";    "12345_5381_0.627" -> "12338_5364_0.624";    "12338_5364_0.624" -> "12336_5303_0.624";    "12336_5303_0.624" -> "12320_5295_0.619";    "12320_5295_0.619" -> "12319_5226_0.618";    "12319_5226_0.618" -> "12318_5218_0.618";    "12318_5218_0.618" -> "12316_5209_0.618";    "12316_5209_0.618" -> "12312_5201_0.615";    "12312_5201_0.615" -> "12300_5145_0.612";    "12300_5145_0.612" -> "12292_5138_0.609";    "12292_5138_0.609" -> "12291_5117_0.609";    "12291_5117_0.609" -> "12290_5112_0.609";    "12290_5112_0.609" -> "12286_5085_0.607";    "12286_5085_0.607" -> "12284_5074_0.607";    "12284_5074_0.607" -> "12280_5058_0.606";    "12280_5058_0.606" -> "12275_4968_0.605";    "12275_4968_0.605" -> "12271_4962_0.604";    "12271_4962_0.604" -> "12265_4948_0.603";    "12265_4948_0.603" -> "12262_4921_0.602";    "12262_4921_0.602" -> "12238_4911_0.596";    "12238_4911_0.596" -> "12237_4895_0.596";    "12237_4895_0.596" -> "12236_4860_0.596";    "12236_4860_0.596" -> "12224_4833_0.592";    "12224_4833_0.592" -> "12221_4822_0.591";    "12221_4822_0.591" -> "12212_4813_0.589";    "12212_4813_0.589" -> "12205_4797_0.588";    "12205_4797_0.588" -> "12204_4758_0.588";    "12204_4758_0.588" -> "12197_4744_0.586";    "12197_4744_0.586" -> "12192_4728_0.585";    "12192_4728_0.585" -> "12190_4715_0.585";    "12190_4715_0.585" -> "12172_4707_0.582";    "12172_4707_0.582" -> "12171_4562_0.581";    "12172_4707_0.582" -> "12143_145_0.577";    "12171_4562_0.581" -> "12150_4540_0.578";    "12143_145_0.577" -> "12107_142_0.57";    "12150_4540_0.578" -> "12142_205_0.577";    "12150_4540_0.578" -> "12134_4335_0.576";    "12107_142_0.57" -> "12038_109_0.559";    "12142_205_0.577" -> "12104_189_0.569";    "12134_4335_0.576" -> "12130_3754_0.575";    "12134_4335_0.576" -> "12102_581_0.569";    "12104_189_0.569" -> "12076_185_0.564";    "12130_3754_0.575" -> "12114_3730_0.572";    "12102_581_0.569" -> "12077_505_0.564";    "12076_185_0.564" -> "11987_162_0.551";    "12114_3730_0.572" -> "12110_3726_0.571";    "12077_505_0.564" -> "12022_386_0.556";    "12077_505_0.564" -> "11956_119_0.547";    "12110_3726_0.571" -> "12074_3595_0.564";    "12110_3726_0.571" -> "12040_131_0.559";    "12022_386_0.556" -> "12010_383_0.555";    "11956_119_0.547" -> "11875_107_0.537";    "12074_3595_0.564" -> "12067_3581_0.563";    "12040_131_0.559" -> "11973_109_0.55";    "12010_383_0.555" -> "12001_377_0.553";    "12067_3581_0.563" -> "12062_3331_0.562";    "12067_3581_0.563" -> "12059_250_0.562";    "12001_377_0.553" -> "11984_363_0.551";    "12062_3331_0.562" -> "12056_3295_0.561";    "12059_250_0.562" -> "11967_245_0.548";    "11984_363_0.551" -> "11980_353_0.55";    "12056_3295_0.561" -> "12052_3282_0.56";    "11967_245_0.548" -> "11899_240_0.539";    "11980_353_0.55" -> "11949_348_0.546";    "12052_3282_0.56" -> "12047_3248_0.56";    "11899_240_0.539" -> "11838_188_0.533";    "11949_348_0.546" -> "11881_295_0.537";    "12047_3248_0.56" -> "12039_3196_0.559";    "11838_188_0.533" -> "11792_145_0.528";    "11881_295_0.537" -> "11852_293_0.535";    "12039_3196_0.559" -> "12023_3173_0.557";    "11792_145_0.528" -> "11557_140_0.505";    "11852_293_0.535" -> "11825_285_0.532";    "12023_3173_0.557" -> "12021_3155_0.556";    "11557_140_0.505" -> "11509_103_0.5";    "11825_285_0.532" -> "11693_269_0.518";    "12021_3155_0.556" -> "12014_3113_0.555";    "11693_269_0.518" -> "11651_243_0.514";    "12014_3113_0.555" -> "12013_3092_0.555";    "11651_243_0.514" -> "11582_236_0.508";    "12013_3092_0.555" -> "11988_3065_0.552";    "11582_236_0.508" -> "11539_199_0.503";    "11988_3065_0.552" -> "11986_3032_0.551";    "11539_199_0.503" -> "11534_194_0.503";    "11986_3032_0.551" -> "11981_3027_0.551";    "11534_194_0.503" -> "11320_176_0.487";    "11981_3027_0.551" -> "11957_3011_0.547";    "11320_176_0.487" -> "11166_135_0.476";    "11957_3011_0.547" -> "11952_2788_0.546";    "11957_3011_0.547" -> "11928_223_0.542";    "11166_135_0.476" -> "11053_129_0.467";    "11952_2788_0.546" -> "11905_2744_0.54";    "11928_223_0.542" -> "11868_157_0.536";    "11053_129_0.467" -> "10888_125_0.458";    "11905_2744_0.54" -> "11773_2725_0.526";    "11868_157_0.536" -> "11830_141_0.532";    "10888_125_0.458" -> "10414_104_0.43";    "11773_2725_0.526" -> "11741_1792_0.523";    "11773_2725_0.526" -> "11724_933_0.522";    "11830_141_0.532" -> "11768_138_0.526";    "11741_1792_0.523" -> "11730_379_0.522";    "11741_1792_0.523" -> "11705_1413_0.52";    "11724_933_0.522" -> "11714_871_0.52";    "11768_138_0.526" -> "11713_128_0.52";    "11730_379_0.522" -> "11694_372_0.518";    "11705_1413_0.52" -> "11658_1372_0.515";    "11714_871_0.52" -> "11704_752_0.519";    "11714_871_0.52" -> "11409_119_0.494";    "11713_128_0.52" -> "11569_122_0.507";    "11694_372_0.518" -> "11681_366_0.517";    "11658_1372_0.515" -> "11639_295_0.513";    "11658_1372_0.515" -> "11609_1077_0.509";    "11704_752_0.519" -> "11667_748_0.516";    "11569_122_0.507" -> "11356_118_0.49";    "11681_366_0.517" -> "11662_359_0.516";    "11639_295_0.513" -> "11612_285_0.51";    "11609_1077_0.509" -> "11574_1053_0.507";    "11667_748_0.516" -> "11640_725_0.513";    "11662_359_0.516" -> "11631_353_0.512";    "11612_285_0.51" -> "11570_278_0.507";    "11574_1053_0.507" -> "11544_1041_0.504";    "11640_725_0.513" -> "11562_618_0.506";    "11640_725_0.513" -> "11551_107_0.504";    "11631_353_0.512" -> "11579_331_0.507";    "11570_278_0.507" -> "11542_275_0.504";    "11544_1041_0.504" -> "11480_992_0.5";    "11562_618_0.506" -> "11456_515_0.498";    "11562_618_0.506" -> "11446_103_0.497";    "11551_107_0.504" -> "11512_105_0.5";    "11579_331_0.507" -> "11422_322_0.495";    "11542_275_0.504" -> "11465_271_0.499";    "11480_992_0.5" -> "11418_107_0.495";    "11480_992_0.5" -> "11411_885_0.494";    "11456_515_0.498" -> "11443_461_0.496";    "11422_322_0.495" -> "11342_307_0.489";    "11465_271_0.499" -> "11328_256_0.488";    "11418_107_0.495" -> "11189_101_0.477";    "11411_885_0.494" -> "11380_741_0.492";    "11411_885_0.494" -> "11281_144_0.484";    "11443_461_0.496" -> "11413_446_0.494";    "11342_307_0.489" -> "11321_294_0.488";    "11328_256_0.488" -> "11251_251_0.482";    "11380_741_0.492" -> "11289_722_0.485";    "11281_144_0.484" -> "11061_135_0.467";    "11413_446_0.494" -> "11349_436_0.489";    "11321_294_0.488" -> "11294_288_0.486";    "11251_251_0.482" -> "11185_250_0.477";    "11289_722_0.485" -> "11276_659_0.484";    "11061_135_0.467" -> "10785_112_0.452";    "11349_436_0.489" -> "11295_380_0.486";    "11294_288_0.486" -> "11255_277_0.482";    "11185_250_0.477" -> "11164_196_0.475";    "11276_659_0.484" -> "11176_649_0.476";    "11295_380_0.486" -> "11200_356_0.477";    "11255_277_0.482" -> "11246_271_0.481";    "11164_196_0.475" -> "11029_189_0.466";    "11176_649_0.476" -> "11110_610_0.471";    "11200_356_0.477" -> "11027_293_0.466";    "11246_271_0.481" -> "11116_264_0.471";    "11029_189_0.466" -> "10879_187_0.457";    "11110_610_0.471" -> "11099_580_0.47";    "11027_293_0.466" -> "10841_229_0.455";    "11116_264_0.471" -> "10790_249_0.452";    "10879_187_0.457" -> "10822_160_0.454";    "11099_580_0.47" -> "11058_544_0.467";    "10841_229_0.455" -> "10744_125_0.449";    "10841_229_0.455" -> "10716_104_0.448";    "10790_249_0.452" -> "10773_129_0.451";    "10790_249_0.452" -> "10683_120_0.446";    "10822_160_0.454" -> "10606_149_0.442";    "11058_544_0.467" -> "10952_528_0.461";    "10773_129_0.451" -> "10708_127_0.447";    "10952_528_0.461" -> "10914_478_0.459";    "10914_478_0.459" -> "10842_443_0.455";    "10842_443_0.455" -> "10815_419_0.453";    "10815_419_0.453" -> "10722_412_0.448";}
原创粉丝点击