Lecture #10 - Tree
Lecture #10 - Tree
Lecture #10 - Tree
We're going to implement tree using node object and connecting them
through references.
Tree Node
The code to write a tree node would be similar to what is given below.
It has a data part and references to its left and right child nodes.
Search Operation
Whenever an element is to be searched, start searching from the
root node, then if the data is less than the key value, search for the
element in the left subtree. Otherwise, search for the element in the
right subtree. Follow the same algorithm for each node.
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their
values too. Because, all nodes are connected via edges (links) we always
start from the root (head) node. That is, we cannot randomly access a
node in a tree.There are three ways which we use to traverse a tree −
In-orderTraversal
Pre-orderTraversal
Post-orderTraversal
Thus, BST divides all its sub-trees into two segments; the left sub-tree
and the right sub-tree and can be defined as −
Representation
BST is a collection of nodes arranged in a way where they maintain BST
properties. Each node has a key and an associated value. While
searching, the desired key is compared to the keys in BST and if found,
the associated value is retrieved.
We observe that the root node key (27) has all less-valued keys on the
left sub-tree and the higher valued keys on the right sub-tree.
Basic Operations
Following are the basic operations of a tree −
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Pre-orderTraversal − Traverses a tree in a pre-order manner.
In-orderTraversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
Node
Define a node having some data, references to its left and right child
nodes.
Search Operation
Whenever an element is to be searched, start searching from the root
node. Then if the data is less than the key value, search for the element in
the left subtree. Otherwise, search for the element in the right subtree.
Follow the same algorithm for each node.
Insert Operation
Whenever an element is to be inserted, first locate its proper location.
Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data.
Otherwise, search for the empty location in the right subtree and insert
the data.
AVL Trees
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.
What if the input to binary search tree comes in a sorted (ascending or
descending) manner? It will then look like this −
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.
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 −
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 State Action
1. Min-Heap
2. Max-Heap
Min-Heap − Where the value Max-Heap − Where the value
of the root node is less than or of the root node is greater than
equal to either of its children. or equal to either of its
children.
Both trees are constructed using the same input and order of arrival.
Max Heap Construction Algorithm
We shall use the same example to demonstrate how a Max Heap is created.
The procedure to create Min Heap is similar but we go for min values instead
of max values.
We are going to derive an algorithm for max heap by inserting one element
at a time. At any point of time, heap must maintain its property. While
insertion, we also assume that we are inserting a node in an already heapified
tree.