Professional Documents
Culture Documents
Linear Data Structure
Linear Data Structure
STRUCTURES- TREES
Ms Swati Mali
swatimali@somaiya.edu
These class notes are a compilation and edition from many sources. The instructor
does not claim intellectual property or ownership of the lecture notes.
Linear and nonlinear data
structures
• Stack • Tree
• Queue • Graph
• Linked lists
TREES
Nature View of a Tree
leaves
branche
s root
Computer Scientist’s View
root
leaves
branches
nodes
Definition of Tree
● A tree is a finite set of one or more nodes
such that:
● There is a specially designated node
called
the root.
● The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of
these sets is a tree.
● We call T1, ..., Tn the subtrees of the root.
Tree
• Tree defined recursively
• A tree is a collection of nodes. The collection can
be empty; otherwise, a tree consists of a
distinguished node r, called the root, and zero or
more non-empty (sub) trees T1, T2, …, Tk each of
whose roots are connected by a directed edge
from r.
• A tree is a collection of N nodes, one of which is
the root and N-1 edges.
Source:Mark Allen
Weiss
What is a Tree
• A tree is a finite nonempty
set of elements.
• It is an abstract model of a
hierarchical structure. Computers”R”Us
• consists of nodes with a
parent-child relation.
• Applications:
Sales Manufacturing R&D
• Organization charts
• File systems
• Programming environments
US International Laptops Desktops
I J K
subtree
Tree Properties
Property Value
A Number of nodes
Height
Root Node
B C Leaves
Interior nodes
Ancestors of H
Descendants of B
D E F
Siblings of E
Right subtree of A
Degree of this tree
H I
Become Rich
• List Representation
• ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
• The root comes first, followed by a list of links to sub-trees
A D F
B
A D F
∅ ∅
C E
C E
Types of trees
• Binary tree
• Binary search tree
• AVL tree
• Threaded binary trees
• B trees
• B+ trees
• Heap
• Red Black trees
• A tree is an undirected graph, but a graph isn’t necessarily
a tree.
BINARY TREE
CHAPTER 5 17
Binary Trees
● A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
● Any tree can be transformed into binary
tree.
– by left child-right sibling representation
● The left subtree and the right subtree are
distinguished.
CHAPTER 5 18
B C B C
D E F G D E F G
H I J K L M N O
H I
Complete binary tree Full binary tree of depth 4
CHAPTER 5 20
Samples of Trees
Complete Binary
Tree
A A 1 A
B B 2 B C
C Skewed Binary
Tree 3 D E F G
D
4 H I
E 5
CHAPTER 5 23
Binary Trees
● A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
● Any tree can be transformed into binary
tree.
– by left child-right sibling representation
● The left subtree and the right subtree are
distinguished.
*Figure 5.6: Left child-right child tree representation of a tree
(p.191)
A
E C
K F G
D
L H
M I
J
CHAPTER 5 25
H I
Binary Tree Representations
● If a complete binary tree with n nodes
(depth =
log n + 1) is represented sequentially, then
for
any node with index i, 1<=i<=n, we have:
– parent(i) is at i/2 if i!=1. If i=1, i is at the root
and has no parent.
– left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has
no left child.
– right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.
CHAPTER 5 33
Linked Representation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
dat
a
left_chil dat right_chil
d a d
left_chil right_chil
d d
Binary Search Trees (BSTs)
In a BST, the
value stored at the
root of a subtree
is greater than any
value in its left
subtree and less
than any value in
its right subtree!
BST
• Creation
• Traversal
• Operations/ implementation
BST Insertion
void inserBinSTreet(Treetype root, If(parent==NULL)
DataType data) { Treetype parent, Root=temp
current, temp;
createnode temp
Else if (temp->data < parent->data)
Temp->data= data
Parent->left= temp
Temp->left=NULL Else
Temp->right= NULL Parent->right=temp
Parent= NULL
}//void insertBinSTree
Current=root
While(current<>Null)
{
parent= current
If(temp->data < current->data)
current= current->left
Else
Current= current->right
}
Binary Search Tree searching
(Recursive)
Treetype BinSTreeSearch(treetype root, eletype key)
{
if (root = NULL or key = root->data)
then return root
if (key < root->data)
then return BinSTreeSearch (root->left, key)
else return BinSTreeSearch (root->right, key)
}
Binary Search Tree searching
(Iterative)
Treetype BinSTreeSearch(treetype root, eletype key)
{
if (root = NULL or key = root->data)
then return root
Current=root
while (current<> NULL and key<> current->data)
{ parent= current
if key < current->data then current= current->left
else current = current->right
}
return current
}
BST Deletion
AVL-TREES
AVL trees
• Adelson-Velskii-Landis proposed in 1962.
A binary search tree is said to be AVL balanced if:
• The difference in the heights between the left and right sub-trees is
at most 1, and
• Both sub-trees are themselves AVL trees
Need of AVL Balanced binary tree
• The disadvantage of a binary search tree is that its height
can be as large as N-1
• This means that the time needed to perform insertion and
deletion and many other operations can be O(N) in the
worst case
• We want a tree with small height
• A binary tree with N node has height at least Θ(log N)
• Thus, our goal is to keep the height of a binary search
tree O(log N)
• Such trees are called balanced binary search trees.
Examples are AVL tree, red-black tree.
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
AVL property
violated here
Rotation cases
• Left-Left case
• Left-Right case
• Right-Right case
• Right-Left case
Balancing AVL trees
• Starting from leaf node (w), travel up and find the first
unbalanced node.
• Let z be the first unbalanced node, y be the child of z that
comes on the path from w to z and x be the grandchild of
z that comes on the path from w to z.
• Balance the tree by performing appropriate rotations on
the subtree rooted with z.
Left-Left Case Right Rotation
Right-Right Case Left Rotation
Let-Right Case Left-Right Rotation
Right-Left Case Right-Left Rotation
Left Rotation
Left Rotation
EXPRESSION TREE
EXPRESSION TREE
• A binary expression tree is a specific kind of a binary tree
used to represent expressions.
• The leaves of the binary expression tree are operands,
such as constants or variable names, and
• the internal nodes contain operators.
Expression Tree Example
Postfix to Expression Tree Rules
• Terms: Append x to y means add child x to node y.
• Rule1: Operators can have children but operands can't.
• Rule2: Nodes can only have 2 children.
• Rule3: When appending nodes, always append to right
first. If right is occupied, then append to left.
Postfix to Expression Tree
Algorithm
1. Get the last symbol (rightmost) of postfix notation,
create a
node for it and designate the new node as the root.
1. Set the root node as current node.
2. For each element from right to left (excluding the last):
i. Create a node for it.
ii. If current node cannot have more children, search for the first
parent/grandparent that can have more children and set it as the
current node.
iii. Append the new node to the current node.
iv. Set new node as current node.
Example
• Postfix expression- 2, 6, *, 3, 8, /, +
Threaded Binary Trees (1/10)
• Threads
• Do you find any drawback of the above tree?
• Too many null pointers in current representation of
binary trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1) = n+1
• Solution: replace these null pointers with some useful
“threads”
Threaded Binary Trees (2/10)
• Rules for constructing the threads
• If ptr->left_child is null,
replace it with a pointer to the node that would be visited
before ptr in an inorder traversal
• If ptr->right_child is null,
replace it with a pointer to the node that would be visited
after ptr in an inorder traversal
Amir Kamil 8/8/02 61
3 8
1
1 5 7
1 1
9
3
Start at leftmost node, print it
Amir Kamil 8/8/02 64
3 8
1
1 5 7
1 1
9
3
Follow thread to right, print node
Amir Kamil 8/8/02 65
9
3
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 70