AM - Python - Unit 1b
AM - Python - Unit 1b
Different tree data structures allow quicker and easier access to the data as it is
a non-linear data structure.
Tree Terminologies
Node
A node is an entity that contains a key or value and pointers to its child nodes.
The last nodes of each path are called leaf nodes or external nodes that do not
contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.
Root
It is the topmost node of a tree.
Height of a Node
The height of a node is the number of edges from the node to the deepest leaf
(ie. the longest path from the node to a leaf node).
Depth of a Node
The depth of a node is the number of edges from the root to the node.
Height of a Tree
The height of a Tree is the height of the root node or the depth of the deepest
node.
Height and depth of each node in a tree
Degree of a Node
The degree of a node is the total number of branches of that node.
Forest
A collection of disjoint trees is called a forest.
Creating forest
from a tree
You can create a forest by cutting the root of a tree.
Tree Traversal
In order to perform any operation on a tree, you need to reach to the specific
node. The tree traversal algorithm helps in visiting a required node in the tree.
Tree traversal
Let's think about how we can read the elements of the tree in the image shown
above.
Although this process is somewhat easy, it doesn't respect the hierarchy of the
tree, only the depth of the nodes.
Two subtrees
Remember that our goal is to visit each node, so we need to visit all the nodes in
the subtree, visit the root node and visit all the nodes in the right subtree as
well.
Preorder traversal
1. Visit root node
Postorder traversal
1. Visit all the nodes in the left subtree
Inorder Traversal: 7 9 4 2 5 1 3 6 8
Preorder Traversal: 1 2 4 7 9 5 3 6 8
Postorder Traversal: 9 7 4 5 2 8 6 3 1
Most popular databases use B-Trees and T-Trees, which are variants of the tree
structure we learned above to store their data
Compilers use a syntax tree to validate the syntax of every program you write.
Types of Tree
1. Binary Tree
2. AVL Tree
3. Binary Search Tree
4. Balanced Tree
1. Binary Tree
A binary tree is a tree data structure in which each parent node can have at
most two children. For example,
Binary Tree
2. Put the second element as a left child of the root node and the third
element as the right child. (no. of elements on level-II: 2)
3. Put the next two elements as children of the left node of the second
level. Again, put the next two elements as children of the right node
of the second level (no. of elements on level-III: 4) elements).
2. AVL Trees
What if the input to binary search tree comes in a sorted (ascending or descending)
manner? It will then look like this −
It is observed that BST's worst-case performance is closest to linear search
algorithms, that is Ο(n). In real-time data, we cannot predict data pattern and their
frequencies. So, a need arises to balance out the existing BST.
Named after their inventor Adelson, Velski & Landis, AVL trees are height
balancing binary search tree. AVL tree checks the height of the left and the right
sub-trees and assures that the difference is not more than 1. This difference is
called the Balance Factor.
Here we see that the first tree is balanced and the next two trees are not balanced −
In the second tree, the left subtree of C has height 2 and the right subtree has
height 0, so the difference is 2. In the third tree, the right subtree of A has height 2
and the left is missing, so it is 0, and the difference is 2 again. AVL tree permits
difference (balance factor) to be only 1.
BalanceFactor = height(left-subtree) − height(right-subtree)
If the difference in the height of left and right sub-trees is more than 1, the tree is
balanced using some rotation techniques.
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −
Left rotation
Right rotation
Left-Right rotation
Right-Left rotation
The first two rotations are single rotations and the next two rotations are double
rotations. To have an unbalanced tree, we at least need a tree of height 2. With this
simple tree, let's understand them one by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the
right subtree, then we perform a single left rotation −
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left
subtree. The tree then needs a right rotation.
As depicted, the unbalanced node becomes the right child of its left child by
performing a right rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained versions of
rotations. To understand them better, we should take note of each action performed
while rotation. Let's first check how to perform Left-Right rotation. A left-right
rotation is a combination of left rotation followed by right rotation.
State Action
A node has been inserted into the right subtree of the left subtree. This
makes C an unbalanced node. These scenarios cause AVL tree to
perform left-right rotation.
We first perform the left rotation on the left subtree of C. This makes A,
the left subtree of B.
We shall now right-rotate the tree, making B the new root node of this
subtree. C now becomes the right subtree of its own left subtree.
The tree is now balanced.
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right
rotation followed by left rotation.
State Action
A node has been inserted into the left subtree of the right subtree. This
makes A, an unbalanced node with balance factor 2.
Node A is still unbalanced because of the right subtree of its right subtree
and requires a left rotation.
A left rotation is performed by making B the new root node of the
subtree. A becomes the left subtree of its right subtree B.
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above
two properties
A tree having a right subtree with one value smaller than the root is shown to demonstrate
that it is not a valid binary search tree
2. In a binary search tree, the value of all the nodes in the left sub-tree is less
than the value of the root.
3. Similarly, value of all the nodes in the right sub-tree is greater than or equal
to the value of the root.
4. This rule will be recursively applied to all the left and right sub-trees of the
root.
A Binary search tree is shown in the above figure. As the constraint applied on the
BST, we can see that the root node 30 doesn't contain any value greater than or
equal to 30 in its left sub-tree and it also doesn't contain any value less than 30 in
its right sub-tree.
2. Read the next element, if it is lesser than the root node element, insert it as
the root of the left sub-tree.
The process of creating BST by using the given elements, is shown in the image
below.
Operations on Binary Search Tree
There are many operations which can be performed on a binary search tree.
SN Operation Description
1 Searching Finding the location of some specific element in a binary
in BST search tree.
B-tree
Why B-tree?
The need for B-tree arose with the rise in the need for lesser time in
accessing the physical storage media like a hard disk. The secondary
storage devices are slower with a larger capacity. There was a need for
such types of data structures that minimize the disk accesses.
Other data structures such as a binary search tree, avl tree, red-black tree,
etc can store only one key in one node. If you have to store a large number
of keys, then the height of such trees becomes very large and the access
time increases.
However, B-tree can store many keys in a single node and can have
multiple child nodes. This decreases the height significantly allowing faster
disk accesses.
Operations
Searching Example
B-tree
2. k is not found in the root so, compare it with the root key.
B Tree Applications
databases and file systems
multilevel indexing
6. Now, there are elements greater than its limit. So, split at the median.
7. Push the median key upwards and make the left keys as a left child
and the right keys as a right child.
Insertion Example
Let us understand the insertion operation with the illustrations below.
The elements to be inserted are 8, 9, 10, 11, 15, 16, 17, 18, 20, 23.
Inserting elements into a B-tree
Algorithm for Inserting an Element
Inserting an element on a B-tree consists of two events:
1.searching the appropriate node to insert the element
2.and splitting the node if required.Insertion operation always takes place in
the bottom-up approach.
Insertion Operation
1. If the tree is empty, allocate a root node and insert the key.
6. Now, there are elements greater than its limit. So, split at the median.
7. Push the median key upwards and make the left keys as a left child
and the right keys as a right child.
The elements to be inserted are 8, 9, 10, 11, 15, 16, 17, 18, 20, 23.
Inserting elements into a B-tree