The Binary Tree module of the eduAlgo package contains some classic Binary Tree algorithms,
Module: BinaryTreeAlgorithms()
Method : print_tree(root)
Uses : To print the tree in a descriptive
manner where each line denotes the Left and Right
node as L: and R: respectively.
Parameters : root - root node of the tree
this method prints the tree.
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()
ping.print_tree(root)
Output:
12:L: 6 ,R: 14 ,
6:L: 5 ,R: 7 ,
5:
7:R: 11 ,
11:
14:L: 13 ,
13:
The above output can be visualized as below,
The blue nodes are known as Left Leaves and the red nodes are known as Right Leaves.
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.
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
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.
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:
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.
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:
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.
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
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:
Keep coding, keep loving python