Algorithms For Dummies



Download 7,18 Mb.
Pdf ko'rish
bet236/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   232   233   234   235   236   237   238   239   ...   651
Bog'liq
Algorithms

  Structuring Data 

     127


isn’t any error trapping. The 

nodeData


 entry can contain any value. The following 

code shows how to use the 

binaryTree

  class  to  build  the  tree  shown  in 

Figure 6-1:

tree = binaryTree("Root")

BranchA = binaryTree("Branch A")

BranchB = binaryTree("Branch B")

tree.left = BranchA

tree.right = BranchB

  

LeafC = binaryTree("Leaf C")



LeafD = binaryTree("Leaf D")

LeafE = binaryTree("Leaf E")

LeafF = binaryTree("Leaf F")

BranchA.left = LeafC

BranchA.right = LeafD

BranchB.left = LeafE

BranchB.right = LeafF

You have many options when building a tree, but building it from the top down (as 

shown in this code) or the bottom up (in which you build the leaves first) are two 

common  methods.  Of  course,  you  don’t  really  know  whether  the  tree  actually 

works at this point. Traversing the tree means checking the links and verifying that 

they actually do connect as you think they should. The following code shows how 

to use recursion (as described in Chapter 5) to traverse the tree you just built.

def traverse(tree):

    if tree.left != None:

        traverse(tree.left)

    if tree.right != None:

        traverse(tree.right)

    print(tree.nodeData)

  

traverse(tree)



  

Leaf C


Leaf D

Branch A


Leaf E

Leaf F


Branch B

Root



128

 

   


  PART 2 


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   232   233   234   235   236   237   238   239   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish