Trees and Tree Algorithms
Binary Heap
A complete binary tree is a tree in which each level has all of its nodes.
To find the parent of any node in the tree, we can simply use Python’s integer division. Given that a node is at position in the list, the parent is at position .

The Heap Order Property
In a heap, for every node with parent , the key in is smaller than or equal to the key in .
Binary Tree Applications
Parse trees can be used to represent real-world constructions like sentences or mathematical expressions.

Tree Traversals
preorder: In a preorder traversal, we visit the root node first, then recursively do a preorder traversal of the left subtree, followed by a recursive preorder traversal of the right subtree.
inorder: In an inorder traversal, we recursively do an inorder traversal on the left subtree, visit the root node, and finally do a recursive inorder traversal of the right subtree.
postorder: In a postorder traversal, we recursively do a postorder traversal of the left subtree and the right subtree followed by a visit to the root node.

Suppose that you wanted to read this book from front to back. The preorder traversal gives you exactly that ordering. Starting at the root of the tree (the Book node) we will follow the preorder traversal instructions. We recursively call preorder on the left child, in this case Chapter 1. We again recursively call preorder on the left child to get to Section 1.1. Since Section 1.1 has no children, we do not make any additional recursive calls. When we are finished with Section 1.1, we move up the tree to Chapter 1.
Binary Tree and its traversal using python.
Binary tree are the tree where one node can have only two child and cannot have more than two. Traversal means visiting all the nodes of the Binary tree. There are three types of traversal.
Lets take the below tree for example.

Inorder traversal
In order traversal means visiting first left, then root and then right.
So the traversal of above tree would be 4 2 5 1 3
Pre Order Traversal
In this traversal we first visit root, then left, then right
It will be something like this 1 2 4 5 3
Post Order traversal
Here we first visit left, then right and then root.
It will be something like this 4 5 2 3 1
These were the traversal now lets code it in python
So this is how you write the binary tree in Python.
Lets have a look at the script.
We made a class Node which represent the node of Binary tree. We initialize the node with data and left and right child as None.
When we add new child we simple use root.left or root.left.left to add a new child.
Now lets have a look at the traversal functions.
In all these functions we took advantage of recursion and passed the subtrees in the functions again to print in proper order. In Inorder function, we first pass the left subtree to Inorder then print the node and then pass the right subtree. Thus made left, root and right. Similarly we did for the other two.
Here we took a look into how to code binary Tree and its traversal using python.
Algorithms: Mirror a Binary tree using python
Binary tree are the tree where one node can have only two child and cannot have more than two. Traversal means visiting all the nodes of the Binary tree.
Lets take the below tree for example.

So we will be writing python code for changing the tree to its mirror.
Thus you can see the mirror trees inorder traversal. It was really simple we just traversed the tree and changed the right subtree with left and left subtree with right subtree.
Level order traversal of a binary tree.
Level order traversal means that we visit the nodes level by level. Like for below tree the level order traversal will be

Its Level order traversal will be
1 2 3 4 5
Here is the code for doing that.
Algorithms: Binary Search Tree and its functionality in python
Back in the algorithms section with python we are going to see how we can code Binary Search Tree and its functionality in python. Binary search tree are binary tree where the left child is less than root and right child is greater than root. We will be performing insertion, searching, traversal, min and other functions. Read more about them here.Level order traversal of a binary tree.

Binary Search Tree and its functionality in python
Lets look at the code below.
What we did here:
Node class to make the nodes of binary search tree.
Then insert functions, what this function is doing is checking if the data is lesser than node then moves to left child else moves to right child and place it at the end.
Search function again works same as insert functions. The only difference is in this we are getting and telling if the data is present while in insert we are adding node to the true.
In order traversal to visit all the node. Do you know in order traversal of binary search tree is in sorted order.
Get min and Get max functions to get the minimum and maximum of the Binary Search Tree.
Delete I am leaving delete function for you. If you are stuck comment below and I will write a delete function for you.
Rosettacode.org
Procedural
Sample output:
Class based
Subclasses a namedtuple adding traversal methods that apply a visitor function to data at nodes of the tree in order
Output:
As above.
Python: Composition of pure (curried) functions
Currying by default is probably not particularly 'Pythonic', but it does work well with higher-order functions – giving us more flexibility in compositional structure. It also often protects us from over-proliferation of the slightly noisy lambda keyword. (See for example the use of the curried version of map in the code below).
The approach taken here is to focus on the evaluation of expressions, rather than the sequencing of procedures. To keep evaluation simple and easily rearranged, mutation is stripped back wherever possible, and 'pure' functions, with inputs and outputs but, ideally, with no side-effects (and no sensitivities to global variables) are the basic building-block.
Composing pure functions also works well with library-building and code reuse – the literature on functional programming (particularly in the ML / OCaml / Haskell tradition) is rich in reusable abstractions for our toolkit. Some of them have already been absorbed, with standard or adjusted names, into the Python itertools module. (See the itertools module preface, and the takewhile function below).
Here, for example, for the pre-, in- and post- orders, we can define a very general and reusable foldTree (a catamorphism over trees rather than lists) and just pass 3 different (rather simple) sequencing functions to it.
This level of abstraction and reuse brings real efficiencies – the short and easily-written foldTree, for example, doesn't just traverse and list contents in flexible orders - we can pass all kinds of things to it. For the sum of all the numbers in the tree, we could write:
and for various other metrics, and the traversal sequences:
Works with: Python version 3
Output:
Last updated