searchtree.py

import sys
import platform
import os
import createtree as t

# --- which Linux version are we running? -----------------

def RunningPython3():
    if sys.version_info[0] == 3:
        return True
    return False

# --- prompt the user for input ---------------------------

def GetUserInput(prompt,py3):
    if py3:
        return input(prompt)
    else:
        return raw_input(prompt)

# --- search the tree for a value -------------------------

def searchTree(tree,i):

    def searchSubTree(node,i):
        value = node.getData()
        ##print('searching node {} for {}'.format(value,i))
        if value == i:
            print('Found Value {}'.format(i))
        elif value > i:
            if node.getLess() == None:
                print('{} not found'.format(i))
            else:
                searchSubTree(node.getLess(),i)
        elif value < i:
            if node.getMore() == None:
                print('{} not found'.format(i))
            else:
                searchSubTree(node.getMore(),i)
        return

    if tree == None:
        print('Value {} not found'.format(i))
        return

    searchSubTree(tree.getRoot(),i)

    return
    
# ---------------------------------------------------------
# main
# ---------------------------------------------------------

py3 = RunningPython3()

theTree = t.Tree()

t.createTree(theTree)

while True:

    print('')
    value = GetUserInput('Enter search value: ',py3)

    sval = value.strip()

    if sval == '':
        break

    if sval.isdigit() != True:
        print('')
        print('Illegal value entered({})'.format(sval))
        continue

    ival = int(sval)

    searchTree(theTree,ival)