Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Tree : Top View

def topView(root):
    d = {}
    def traverse(root, key, level):
        if root:
            if key not in d:
                d[key] = [root, level]
            elif d[key][1] > level:
                d[key] = [root, level]           
            
            traverse(root.left, key-1, level +1)
            traverse(root.right, key+1, level + 1)
            
    traverse(root, 0, 0)
    for key in sorted(d):
        print(d[key][0], end = " ")
Comment

Tree : Top View

def topView(root):
    d = {}
    def traverse(root, key, level):
        if root:
            if key not in d:                # when new key is added
                d[key] = [root, level]
            elif d[key][1] > level:         # for movement inside tree
                d[key] = [root, level]           
            
            traverse(root.left, key-1, level +1)
            traverse(root.right, key+1, level + 1)
            
    traverse(root, 0, 0)
    for key in sorted(d):
        print(d[key][0], end = " ")
Comment

PREVIOUS NEXT
Code Example
Python :: fix the error when you close turtle screen in your own main loop 
Python :: subsetting a column and giving it a value using numpy 
Python :: django wsgi application could not be loaded error importing module 
Python :: python check if variable is module 
Python :: create list python 
Python :: how to unpack the whole list without index them individually python 
Python :: Select non-NaN rows and replace column value 
Python :: pyqt set widget size 
Python :: python loop through specific angle 
Python :: python sliding window maximum 
Python :: how to usepygame.sprite.spritecollide 
Python :: import mongodatetime flask 
Python :: python print top 5 results of array 
Python :: how to import discord in python rewrite vscode 
Python :: provide a script that prints the sum of every even numbers in the range [0; 100]. 
Python :: qlabel click python 
Python :: python change type of every element in a dictionary 
Python :: apply with sf 
Python :: import math print(m.cos(10)) 
Python :: run a python script with python and catch command line output 
Python :: how to get user id discord.py 
Python :: the dropping of sediment by water wind and ice or gravity is known as 
Python :: Sorts this RDD by the given keyfunc 
Python :: Prints out the schema in the tree format 
Python :: create a dict from variables and give name 
Python :: get the factorial of a number on python 
Python :: python sort list of tuples lexicographically 
Python :: problem with console writeline python 
Python :: urllib.error.HTTPError: HTTP Error 502 docker redis 
Python :: pip django graphql 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =