GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Python Data Structure - Binary Search Tree

Python Data Structure Tutorial
  • Python - DS Home
  • Python - DS Introduction
  • Python - DS Environment
  • Python - Arrays
  • Python - Lists
  • Python - Tuples
  • Python - Dictionary
  • Python - 2-D Array
  • Python - Matrix
  • Python - Sets
  • Python - Maps
  • Python - Linked Lists
  • Python - Stack
  • Python - Queue
  • Python - Dequeue
  • Python - Advanced Linked list
  • Python - Hash Table
  • Python - Binary Tree
  • Python - Search Tree
  • Python - Heaps
  • Python - Graphs
  • Python - Algorithm Design
  • Python - Divide and Conquer
  • Python - Recursion
  • Python - Backtracking
  • Python - Sorting Algorithms
  • Python - Searching Algorithms
  • Python - Graph Algorithms
  • Python - Algorithm Analysis
  • Python - Big-O Notation
  • Python - Algorithm Classes
  • Python - Amortized Analysis
  • Python - Algorithm Justifications

Python Data Structures Useful Resources

  • Python - DS Quick Guide
  • Python - DS Useful Resources
  • Python - DS Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Python Data Structure - Binary Search Tree



Previous
Quiz
Next

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties.The left sub-tree of a node has a key less than or equal to its parent node's key.The right sub-tree of a node has a key greater than to its parent node's key.Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree

left_subtree (keys)  >  node (key)  >  right_subtree (keys)

Search for a value in a B-tree

Searching for a value in a tree involves comparing the incoming value with the value exiting nodes. Here also we traverse the nodes from left to right and then finally with the parent. If the searched for value does not match any of the exiting value, then we return not found message, or else the found message is returned.

main.py

class Node:
   def __init__(self, data):
      self.left = None
      self.right = None
      self.data = data
# Insert Node
   def insert(self, data):
      if self.data:
         if data < self.data:
            if self.left is None:
               self.left = Node(data)
            else:
               self.left.insert(data)
         elif data > self.data:
               if self.right is None:
                  self.right = Node(data)
               else:
                  self.right.insert(data)
      else:
         self.data = data

# findval method to compare the value with nodes
   def findval(self, lkpval):
      if lkpval < self.data:
         if self.left is None:
            return str(lkpval)+" Not Found"
         return self.left.findval(lkpval)
      elif lkpval > self.data:
            if self.right is None:
               return str(lkpval)+" Not Found"
            return self.right.findval(lkpval)
      else:
            print(str(self.data) + ' is found')
# Print the tree
   def PrintTree(self):
      if self.left:
         self.left.PrintTree()
      print( self.data),
      if self.right:
         self.right.PrintTree()
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)
print(root.findval(7))
print(root.findval(14))

Output

When the above code is executed, it produces the following result −

7 Not Found
14 is found
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.