Trees
Trees
Unit-4
1
So far we discussed Linear data structures
like
stack
2
Introduction to trees
• So far we have discussed mainly linear data structures – strings, arrays,
lists, stacks and queues
3
4
Tre
e
• A tree is an abstract model of a hierarchical structure that consists of
nodes with a parent-child relationship.
• Tree is a sequence of nodes
5
6
7
Some Key
•Terms:
Root − Node at the top of the tree is called root.
• Parent − Any node except root node has one edge upward to a node called parent.
• Child − Node below a given node connected by its edge downward is called its child node.
• Leaf − Node which does not have any child node is called leaf node.
• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child
node is at level 1, its grandchild is at level 2 and so on.
• keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
8
Some Key
•Terms:
Degree of a node:
• The degree of a node is the number of children of that node
• Degree of a Tree:
• The degree of a tree is the maximum degree of nodes in a given tree
• Path:
• It is the sequence of consecutive edges from source node to destination node.
• Height of a node:
• The height of a node is the max path length form that node to a leaf node.
• Height of a tree:
• The height of a tree is the height of the root
• Depth of a tree:
• Depth of a tree is the max level of any leaf in the tree
9
10
Characteristics of trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in ordered array
• Insertion and deletion as fast as in linked
list
• Simple and fast
11
Applicatio
n
• Directory structure of a file store
• Structure of an arithmetic expressions
• Used in almost every 3D video game to determine what objects need to be
rendered.
• Used in almost every high-bandwidth router for storing router-tables.
• used in compression algorithms, such as those used by the .jpeg and .mp3 file-
formats.
• Consider a binary tree T, here ‘A’ is the root node of the binary
tree T.
14
Binary
Tree
•A binary tree is a finite set of elements that are either empty or is
partitioned into three disjoint subsets.
• The first subset contains a single element called the root of the
tree.
• The other two subsets are themselves binary trees called the left and right
sub-trees of the original tree.
16
Binary
•Tree
The root node of this binary tree is A.
• The left sub tree of the root node, which we denoted by LA, is the set
LA = {B,D,E,G} and the right sub tree of the root node, RA is the set
RA={C,F,H}
17
Binary Tree
Properties
• If a binary tree contains m nodes at level L, it contains at most
2m nodes at level L+1
• Since a binary tree can contain at most 1 node at level 0 (the root), it
contains at most 2L nodes at level L.
18
Types of Binary
Tree
• Complete binary tree
• Strictly binary tree
• Almost complete binary tree
19
Strictly binary
tree
• If every non-leaf node in a binary tree has nonempty left and right sub-trees, then
such a tree is called a strictly binary tree.
• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degree one.
20
Complete binary tree
• A complete binary tree is a binary tree in which every level, except possibly the last, is
completely
filled, and all nodes are as far left as possible.
• A complete binary tree of depth d is called strictly binary tree if all of whose leaves are at level
d.
• A complete binary tree has 2d nodes at every depth d and 2d -1 non leaf nodes
21
Almost complete binary tree
• An almost complete binary tree is a tree where for a right child, there is always a
left child, but for a left child there may not be a right child.
22
23
24
Tree
•traversal
Traversal is a process to visit all the nodes of a tree and may print their
values too.
• All nodes are connected via edges (links) we always start from the
root (head) node.
• Generally we traverse a tree to search or locate given item or key in the tree
or to print all the values it contains.
25
Pre-order, In-order, Post-order
• Pre-order
<root><left><right
>
• In-order
<left><root><right
>
• Post-order
<left><right><root 26
Pre-order
Traversal
• The preorder traversal of a nonempty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
27
Pre-order Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Preorder(Node
*root)
{
if (root==NULL) return;
printf (“%c”, root-
>data);
Preorder(root->left); 28
In-order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder
29
In-order Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Inorder(Node
*root)
{
if (root==NULL) return;
Inorder(root->left);
printf (“%c”, root->data);
Inorder(root->right); 30
Post-order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node
31
Post-order Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Postorder(Nod
e *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root- 32
Binary Search
Tree(BST)
• A binary search tree (BST) is a binary tree that is either empty or in
which every node contains a key (value) and satisfies the following
conditions:
• All keys in the left sub-tree of the root are smaller than the key in the root
node
• All keys in the right sub-tree of the root are greater than the key in the root
node
• The left and right sub-trees of the root are again binary search trees
33
Binary Search
Tree(BST)
34
Binary Search
Tree(BST)
• A binary search tree is basically a binary tree, and therefore it can be
traversed in inorder, preorder and postorder.
35
Why Binary Search
Tree?
• Let us consider a problem of searching a list.
36
Why Binary Search
Tree?
• So we may think of using a linked list because it permits insertion
and deletion to be carried out by adjusting only few pointers.
• But in an n-linked list, there is no way to move through the list other
than one node at a time, permitting only sequential access.
37
Binary Search
Tree(BST)
Time Complexity
Array Linked List BST
Search O(n) O(n) O(logn)
Insert O(1) O(1) O(logn)
Remove O(n) O(n) O(logn)
38
Operations on Binary Search Tree
(BST)
•Following operations can be done in BST:
• Search(k, T): Search for key k in the tree T. If k is found in some node of
tree then return true otherwise return false.
• Insert(k, T): Insert a new node with value k in the info field in the tree T
such
that the property of BST is maintained.
• Delete(k, T):Delete a node with value k in the info field from the tree T
such that the property of BST is maintained.
40
41
Insertion of a node in
BST
•To insert a new item in a tree, we must first verify that its key is
different from those of existing elements.
• If a new value is less, than the current node's value, go to the left subtree,
else go to the right subtree.
• Following this simple rule, the algorithm reaches a node, which has no
left or right subtree.
• By the moment a place for insertion is found, we can say for sure, that a
new value has no duplicate in the tree.
42
Algorithm for insertion in
BST
• Check, whether value in current node and a new value are equal. If so,
duplicate is found. Otherwise,
43
44
45
Deleting a node from the
BST
• While deleting a node from BST, there may be three cases:
1. The node to be deleted may be a leaf node:
• In this case simply delete a node and set null pointer to its parents those side
at which this deleted node exist.
46
Deleting a node from the
BST
2. The node to be deleted has one child
• In this case the child of the node to be deleted is appended to its parent node.
Suppose node to be deleted is 18
47
Deleting a node from the
BST
48
49
50
Huffman
Algorithm
• Huffman algorithm is a method for building an extended binary tree
with a minimum weighted path length from a set of given weights.
51
Huffman
Algorithm
•1951, David Huffman found the “most efficient method of representing
numbers, letters, and other symbols using binary code”. Now standard
method used for data compression.
• Its value and the previously calculated sum of the tree are used to form the
new node which in turn becomes their parent.
52
Huffman
•Algorithm
Let us take any four characters and their frequencies, and sort this list by
increasing frequency.
• Since to represent 4 characters the 2 bit is sufficient thus take initially two
bits for each character this is called fixed length character.
character frequencies Character frequencies code
E 10 A 3 00
sort
T 7 O 5 01
O 5 E 7 10
A 3 T 10 11
• Here before using Huffman algorithm the total number of bits required is:
nb=3*2+5*2+7*2+10*2 =06+10+14+20 =50bits
53
54
Character frequencies code
A 3 110
O 5 111
T 7 10
E 10 0
• Thus after using Huffman algorithm the total number of bits required is
nb=3*3+5*3+7*2+10*1 =09+15+14+10 =48bits
i.e
(50-48)/50*100%=4%
Since in this small example we save about 4% space by using Huffman algorithm. If we take
large example with a lot of characters and their frequencies we can save a lot of space
55
Huffman Algorithm
• Lets say you have a set of numbers and their frequency of use and
want to create a huffman encoding for them
Value Frequencies
1 5
2 7
3 10
4 15
5 20
6 45
56
Huffman
•Algorithm
Creating a Huffman tree is simple. Sort this list by frequency and make the two-lowest
elements into leaves, creating a parent node with a frequency that is the sum of the two lower
element's frequencies:
12:*
/ \
5:1 7:2
• The two elements are removed from the list and the new parent node, with frequency 12, is
inserted into the list by frequency. So now the list, sorted by frequency, is:
10:3
12:*
15:4
20:5
45:6
57
Huffman
•Algorithm
You then repeat the loop, combining the two lowest elements. This results in:
22:*
/ \
10:3 12:*
/ \
5:1
7:2
• The two elements are removed from the list and the new parent node, with frequency 12, is
inserted into the list by frequency. So now the list, sorted by frequency, is:
15:4
20:5
22: *
45:6 58
Huffman
Algorithm
59
Assignment
s
• Slides at myblog
http://www.ashimlamichhane.com.np/2016/07/tree-slide-for-data-
structure-and-algorithm/
• Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/As
signments/assignment_7
60
Reference
• https://www.siggraph.org/education/materials/HyperGraph/video/mp
eg/mpegfaq/huffman_tutorial.html
• https://en.wikipedia.org/wiki/Binary_search_tree
• https://www.cs.swarthmore.edu/~newhall/unixhelp/Java_bst.pdf
• https://www.cs.usfca.edu/~galles/visualization/BST.html
• https://www.cs.rochester.edu/~gildea/csc282/slides/C12-bst.pdf
• http://www.tutorialspoint.com/data_structures_algorithms/tree_data
_structure.htm
61