Python Binary Search


Python Program for First Binary Tree Search - implementing Recursion

This is a Python program to perform depth-first search on a binary tree using recursion. Problem Description The program creates a binary tree and presents a menu to the user to perform operations on the tree including depth-first search. Problem Solution

1. Create a class BinaryTree with instance variables key, left and right.
2. Define methods set_root, insert_left, insert_right, depth_first and search.
3. The method set_root takes a key as argument and sets the variable key equal to it.
4. The methods insert_left and insert_right insert a node as the left and right child respectively.
5. The method depth_first displays a depth first traversal of the tree.
6. The method search returns a node with a specified key.

Python Program for First Binary Tree Search - without using Recursion

This is a Python program to perform depth-first search on a binary tree without using recursion. Problem Description The program creates a binary tree and presents a menu to the user to perform operations on the tree including depth-first search. Problem Solution

1. Create a class Stack to implement a stack.
2. The class Stack will have methods is_empty, push and pop.
3. Create a class BinaryTree with instance variables key, left and right.
4. Define methods set_root, insert_left, insert_right, preorder_depth_first and search.
5. The method set_root takes a key as argument and sets the variable key equal to it.
6. The methods insert_left and insert_right insert a node as the left and right child respectively.
7. The method search returns a node with a specified key.
8. The method preorder_depth_first displays a preorder DFS traversal of the tree.
9. The preorder traversal is implemented using a stack to avoid recursion.

Python Program to Find Nth Node in the Inorder Traversal of a Binary Tree

This is a Python program to find the Nth node in the in-order traversal of a binary tree. Problem Description The program creates a binary tree and presents a menu to the user to perform operations on the tree including printing the Nth term in its in-order traversal. Problem Solution

1. Create a class BinaryTree with instance variables key, left and right.
2. Define methods set_root, insert_left, insert_right, inorder_nth, inorder_nth_helper and search.
3. The method set_root takes a key as argument and sets the variable key equal to it.
4. The methods insert_left and insert_right insert a node as the left and right child respectively.
5. The method search returns a node with a specified key.
6. The method inorder_nth displays the nth element in the in-order traversal of the tree. It calls the recursive method inorder_nth_helper.