Lec7 PDF
Lec7 PDF
Introduction to trees
• Sofar we have discussed mainly linear data structures – strings, arrays,
lists, stacks and queues
Ashim Lamichhane 4
Ashim Lamichhane 5
Ashim Lamichhane 6
Some Key Terms:
• Root − Node at the top of the treeis 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 itsnext child node
is at level 1, its grandchild is at level 2 and soon.
• keys − Key represents a value of a node based on which a search operation is to be carried out for anode.
Ashim Lamichhane 7
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 giventree
• 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 ofthe root
• Depth of atree:
• Depth of a tree is the max level of any leaf in the tree
Ashim Lamichhane 8
Ashim Lamichhane 9
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
Ashim Lamichhane 10
Application
• 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 storingrouter-tables.
• used in compression algorithms, such as those used by the .jpeg and .mp3 file-
formats.
Ashim Lamichhane 11
Introduction ToBinary Trees
• Abinary tree, is a tree in which no node can have more than two
children.
• Consider a binary tree T,here ‘A’ is the root node of the binary tree T.
Ashim Lamichhane 13
Binary Tree
• Abinary tree is a finite set of elements that are either empty or is
partitioned into three disjointsubsets.
• 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 originaltree.
Ashim Lamichhane 14
The following figure shows a binary tree with 9 nodes where A is the root
Ashim Lamichhane 15
Binary Tree
• The root node of this binary tree isA.
• The left sub tree of the root node, which we denoted by LA, is theset
LA= {B,D,E,G} and the right sub tree of the root node, RAis the set
RA={C,F,H}
• The root node of LAis node B, the root node of RAis Cand so on
Ashim Lamichhane 16
Binary TreeProperties
• If a binary tree contains m nodes at level L, it contains atmost 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.
Ashim Lamichhane 17
Types of BinaryTree
• Complete binary tree
• Strictly binary tree
• Almost complete binary tree
Ashim Lamichhane 18
Strictly binary tree
• If every non-leaf node in a binary tree has nonempty leftand 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 degreeone.
Ashim Lamichhane 19
Complete binary tree
• Acomplete 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.
• Acomplete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d.
• Acomplete binary tree has 2d nodes at every depthd and 2d -1 non leaf nodes
Ashim Lamichhane 20
Almost complete binary tree
• An almost complete binary tree is a tree where for a right child, thereis always a
left child, but for a left child there may not be a right child.
Ashim Lamichhane 21
Ashim Lamichhane 22
Ashim Lamichhane 23
Treetraversal
• 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 theroot
(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.
Ashim Lamichhane 24
Pre-order, In-order, Post-order
• Pre-order
<root><left><right>
• In-order
<left><root><right>
• Post-order
<left><right><root>
Ashim Lamichhane 25
Pre-order Traversal
• The preorder traversal of a nonempty binary tree is defined asfollows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
Ashim Lamichhane 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);
Preorder(root->right);
}
Ashim Lamichhane 27
In-order traversal
• The in-order traversal of a nonempty binary tree is defined asfollows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder
Ashim Lamichhane 28
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);
}
Ashim Lamichhane 29
Post-order traversal
• The in-order traversal of a nonempty binary tree is defined asfollows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node
Ashim Lamichhane 30
Post-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Postorder(Node *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root->data);
}
Ashim Lamichhane 31
Binary SearchTree(BST)
• The left and right sub-trees of the root are again binary search trees
Ashim Lamichhane 32
Binary SearchTree(BST)
Ashim Lamichhane 33
Binary SearchTree(BST)
• Abinary search tree is basically a binary tree, and therefore it can be
traversed in inorder, preorder and postorder.
Ashim Lamichhane 34
Why Binary SearchTree?
• Let us consider a problem of searching a list.
Ashim Lamichhane 35
Why Binary SearchTree?
• So we may think of using a linked list because it permits insertion
and deletion to be carried outby adjusting only few pointers.
Ashim Lamichhane 36
Binary SearchTree(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)
Ashim Lamichhane 37
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 returnfalse.
• Insert(k, T): Insert a new node with value k in the info field in the tree Tsuch
that the property of BSTis maintained.
• Delete(k, T):Delete a node with value k in the info field from the tree Tsuch
that the property of BSTis maintained.
Ashim Lamichhane 39
Ashim Lamichhane 40
Insertion of a node inBST
• Toinsert a new item in a tree, we must first verify that itskey is different
from those of existingelements.
• 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 noleft
or right subtree.
• By the moment a place for insertion is found, we can say for sure, thata
new value has no duplicate in the tree.
Ashim Lamichhane 41
Algorithm for insertion in BST
• Check, whether value in current node and a new value are equal. If so,
duplicate is found. Otherwise,
Ashim Lamichhane 42
Ashim Lamichhane 43
Ashim Lamichhane 44
Deleting a node from theBST
• 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 itsparents those side
at which this deleted nodeexist.
Ashim Lamichhane 45
Deleting a node from theBST
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
Ashim Lamichhane 46
Deleting a node from theBST
Ashim Lamichhane 47
Ashim Lamichhane 48
Ashim Lamichhane 49
Splay Trees
z x has a grandparent x
10 30
y y
20 20
x z
T1 30 10 T4
T2 T3
T3 T4 T1 T2
z x has a grandparent x
10 20
y
30
x z y
T1 20 10 30
T4
T2 T3 T1 T2 T3 T4
T2
T3 T4 T1 T2 T3 T4
44
Splay(78) 50
17 88
32 65 97
zig-zag
28 54 82
z
29 76
y
80
x 78
Complete Example
44
Splay(78) 50
17 88
32 65 97
zig-zag
28 54 82
x
29 78
z 76 80 y
Complete Example
44
Splay(78) 50
17 88
32 z 65 97
zig-zag
y
28 54 82
29 78 x
76 80
Complete Example
44
Splay(78) 50
17 88
32 x 78 97
zig-zag
28 z 65 y 82
29 54 76 80
Complete Example
44
z
Splay(78) 50
17 88 y
32 x 78 97
zig-zag
28 65 82
29 54 76 80
Complete Example
44
x
Splay(78) 78
17 50 z 88 y
82
32 97
zig-zag 65
80
28
54 76
29
Complete Example
y 44
x
Splay(78) 78
17 50 88 w
82
32 97
zig 65
80
28
54 76
29
Complete Example
78 x
y 44
Splay(78)
17 50 88 w
82
32 97
zig 65
80
28
54 76
29
AVL Tree is…
1. Sub-trees of each
node can differ by
at most 1 in their
height
2. Every sub-trees is
an AVL tree
AVL tree?
YES NO
Each left sub-tree has Left sub-tree has height 3,
height 1 greater than each but right sub-tree has height
right sub-tree 1
AVL tree
Height of a node
● The height of a leaf is 1. The height of a null
pointer is zero.
● The height of an internal node is the maximum
height of its children plus 1
5 20 5 20
3 3 43
1
2
1 3
AVL Trees
12
8 16
4 10 14
2 6
AVL Tree
-1 0
0 0 -1 1
0 0 0 0
AVL Tree -2 AVL Tree
1 0
0 -1
Number of Keys 4
Number of Pointers 5