Binary Tree Implementations

Last updated: 19th Aug, 2020

Binary Tree Module

The Binary Tree module of the eduAlgo package contains some classic Binary Tree algorithms,

  • Print The Binary Tree -
  • Sum of Binary Tree Nodes in a Range -
  • Merge Two Binary Trees -
  • Checking If Two Trees are the Same -

Sum Of Binary Tree Nodes in a Range

Module: BinaryTreeAlgorithms()
Method : rangeSumBST(root,L,R)
Uses : To print the sum of the node values in a Tree which lies in a range of (L,R), both inclusive.
Parameters : root - root node of the tree, L - left integer range, R - right integer range,this method return the desired range sum as an int.

Python Code Example
from edualgo import BinaryTree as bt
root = bt.Node(12)
root.insert(6)
root.insert(7)
root.insert(5)
root.insert(14)
root.insert(13)
root.insert(11)

ping = bt.BinaryTreeAlgorithms()
print(ping.rangeSumBST(root,3,13))

Output:
54
      
Merge Two Binary Trees

Module: BinaryTreeAlgorithms()
Method : mergeTrees(root1,root2)
Uses : To merge two binary trees into one single binary tree.
Parameters : root1 - root node of the first tree, root2 - root node of the second tree, this method returns the root of the merged tree.

Python Code Example
from edualgo import BinaryTree as bt
root1 = bt.Node(12)
root1.insert(6)
root1.insert(7)
root1.insert(5)
root1.insert(14)


root2 = bt.Node(7)
root2.insert(6)
root2.insert(2)
root2.insert(9)
root2.insert(1)
root2.insert(10)
root2.insert(8)


ping = bt.BinaryTreeAlgorithms()

root3 = ping.mergeTrees(root1,root2)
ping.print_tree(root3)

Output:
19:L: 12 ,R: 23 ,

12:L: 7 ,R: 7 ,

7:L: 1 ,

1:

7:

23:L: 8 ,R: 10 ,

8:

10:
      
Merge Two Binary Trees

Module: BinaryTreeAlgorithms()
Method : mergeTrees(root1,root2)
Uses : To merge two binary trees into one single binary tree.
Parameters : root1 - root node of the first tree, root2 - root node of the second tree, this method returns the root of the merged tree.

Python Code Example
from edualgo import BinaryTree as bt
root1 = bt.Node(12)
root1.insert(6)
root1.insert(7)
root1.insert(5)
root1.insert(14)


root2 = bt.Node(7)
root2.insert(6)
root2.insert(2)
root2.insert(9)
root2.insert(1)
root2.insert(10)
root2.insert(8)


ping = bt.BinaryTreeAlgorithms()

root3 = ping.mergeTrees(root1,root2)
ping.print_tree(root3)

Output:
19:L: 12 ,R: 23 ,

12:L: 7 ,R: 7 ,

7:L: 1 ,

1:

7:

23:L: 8 ,R: 10 ,

8:

10:
      
Checking If Two Trees are the Same

Module: BinaryTreeAlgorithms()
Method : isSameTree(root1,root2)
Uses : To check if two trees are the same.
Parameters : root1 - root node of the first tree, root2 - root node of the second tree, this method returns a boolean.

Python Code Example
from edualgo import BinaryTree as bt
root1 = bt.Node(12)
root1.insert(6)
root1.insert(7)
root1.insert(5)
root1.insert(14)
root1.insert(13)
root1.insert(11)

root2 = bt.Node(12)
root2.insert(6)
root2.insert(7)
root2.insert(5)
root2.insert(14)
root2.insert(13)
root2.insert(11)

ping = bt.BinaryTreeAlgorithms()
print(ping.isSameTree(root1,root2))

Output:
True
      

Found Some Bugs ?

Mail The Developer here - abhijittripathy99@gmail.com

Are you an ambitious Python Developer ?

Instance Theme

Do you want to contribute to our project ? We welcome every python lover to come forwards and have some fun with our source code. Steps to contibute:

  • Send an email to the developer here regarding your willingness to contribute to the project.
  • Have a read of our "How To Contribute" documentation here.
Keep coding, keep loving python