Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Python Online

# Hello World program in Python
    
vals = [7,4,10,3,5,6,12,15]

tree = []
for i in range (0,len(vals)):
    tree.append([vals[i],None, None, None])

#sort out the links
for i in range(1,len(tree)):
    #pointer position and previous
    ptr = 1
    pos = 0
    pre = 0
    while ptr != None:
        if tree[i][0] < tree[pos][0]:
            #go left
            ptr = tree[pos][1]
            tree[i][3]=pre
            pre=i
        else:
            #go right
            ptr = tree[pos][2]
            tree[i][3]=pre
            pre=i

        if ptr != None:
            pos = ptr

    if tree[i][0] < tree[pos][0]:
        # go left
        tree[pos][1] = i
    else:
        # go right
        tree[pos][2] = i

print(tree)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.