Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

NONLINEAR DATA

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

Europe Asia Canada


Tree Terminology
• Root: node without parent (A) • Subtree: tree consisting of a
• Siblings: nodes share the same parent node and its descendants
• Internal node: node with at least one
child (A, B, C, F)
• External node (leaf ): node without
children (E, I, J, K, G, H, D) A
• Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
• Descendant of a node: child, grandchild,
grand-grandchild, etc. B C D
• Depth of a node: number of ancestors
• Height of a tree: maximum depth of any
node (3)
• Degree of a node: the number of its
children E F G H
• Degree of a tree: the maximum degree of
its node.

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

Force Others to be Rob


Poor Banks Stock
Fraud
Intuitive Representation of Tree Node

• 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

How many link fields are


needed in
such a representation?
Data Link 1 Link 2 … Link n
A Tree Representation
• A node is represented by an
object storing ∅
• Element
• Parent node B
• Sequence of children nodes
∅ ∅

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

Maximum Number of Nodes in BT


● The maximum number of nodes on level i of
a binary tree is 2i-1, i>=1.
● The maximum number of nodes in a binary
tree of depth k is 2k-1, k>=1.
Full BT VS Complete BT
● A full binary tree of kdepth k is a binary tree of
depth k having 2 -1 nodes, k>=0.
● A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes
numbered from 1 to n in the full binary tree of
depth k.
A A

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

Abstract Data Type Binary_Tree


structure Binary_Tree(abbreviated BinTree) is
objects: a finite set of nodes either empty or
consisting of a root node, left Binary_Tree,
and right Binary_Tree.
functions:
for all bt, bt1, bt2 ∈ BinTree, item ∈ element
Bintree Create()::= creates an empty binary
tree
Boolean IsEmpty(bt)::= if (bt==empty binary
tree) return TRUE else return FALSE
CHAPTER 5 21

BinTree MakeBT(bt1, item, bt2)::= return a binary tree


whose left subtree is bt1, whose right subtree is
bt2,
and whose root node contains the data item

Bintree Lchild(bt)::= if (IsEmpty(bt)) return error


else return the left subtree of bt

element Data(bt)::= if (IsEmpty(bt)) return error


else return the data in the root node of bt

Bintree Rchild(bt)::= if (IsEmpty(bt)) return error


else return the right subtree of bt
CHAPTER 5 22

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

Binary Tree Traversals


● Let L, V, and R stand for moving left, visiting
the node, and moving right.
● There are six possible combinations of trave
– LVR, LRV, VLR, VRL, RVL, RLV
● Adopt convention that we traverse left befor
right, only 3 traversals remain
– LVR, LRV, VLR
– inorder, postorder, preorder
CHAPTER 5 26

Inorder Traversal (recursive version)


void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
A/B*C*D+E
if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
CHAPTER 5 27

Preorder Traversal (recursive version)


void preorder(tree_pointer ptr)
/* preorder tree traversal */
{
+**/ABCDE
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
predorder(ptr->right_child);
}
}
CHAPTER 5 28

Postorder Traversal (recursive version)


void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
AB/C*D*E+
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}
CHAPTER 5 29
Iterative Inorder Traversal
(using stack)
void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node);/* add to stack */
node= delete(&top);
/* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node = node->right_child;
}
} O(n
CHAPTER 5 30
Level Order Traversal
(using queue)

void level_order(tree_pointer ptr)


/* level order tree traversal */
{
int front = rear = 0;
tree_pointer queue[MAX_QUEUE_SIZE];
if (!ptr) return; /* empty queue */
addq(front, &rear, ptr);
for (;;) {
ptr = deleteq(&front, rear);
CHAPTER 5
[1]
31
A
Sequential Representation [2] B
[3] C
(1) waste space
[4] D
(2)
A [1] A
[2] B insertion/deletion [5] E
problem [6] F
[3] -- [7]
B G
[4] C [8]
A H
[5] -- [9] I
C [6] --
[7] -- B C
D [8] D
[9] --
. . D E F G
E
[16] E

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

Note that this definition of height is different from the one


we defined previously (we defined the height of a leaf as
zero previously).
AVL tree
• An AVL tree is a binary search tree in which
• for every node in the tree, the height of the left and right subtrees
differ by at most 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

Threaded Tree Example


6
3 8
1
1 5 7
1 1
9
3
Amir Kamil 8/8/02 62

Threaded Tree Traversal


• We start at the leftmost node in the tree, print it, and
follow its right thread
• If we follow a thread to the right, we output the node and
continue to its right
• If we follow a link to the right, we go to the leftmost node,
print it, and continue
Amir Kamil 8/8/02 63

Threaded Tree Traversal


Output
6 1

3 8
1
1 5 7
1 1
9
3
Start at leftmost node, print it
Amir Kamil 8/8/02 64

Threaded Tree Traversal


Output
6 1
3

3 8
1
1 5 7
1 1
9
3
Follow thread to right, print node
Amir Kamil 8/8/02 65

Threaded Tree Traversal


Output
6 1
3
5
3 8
1
1 5 7
1 1
9
3
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 66

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1
1 5 7
1 1
9
3
Follow thread to right, print node
Amir Kamil 8/8/02 67

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1 7
1 5 7
1 1
9
3
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 68

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1
9
3
Follow thread to right, print node
Amir Kamil 8/8/02 69

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9

9
3
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 70

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9
11
9
3
Follow thread to right, print node
Amir Kamil 8/8/02 71

Threaded Tree Traversal


Output
6 1
3
5
3 8 6
1 7
1 5 7 8
1 1 9
11
9 13
3
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 72

Threaded Tree Modification


• We’re still wasting pointers, since half of our leafs’
pointers are still null
• We can add threads to the previous node in an inorder
traversal as well, which we can use to traverse the tree
backwards or even to do postorder traversals
Amir Kamil 8/8/02 73

Threaded Tree Modification


6
3 8
1
1 5 7
1 1
9
3
Threaded Binary Tree
• Advantages of threaded binary tree:
• Threaded binary trees have numerous advantages over
non-threaded binary trees listed as below:
• The traversal operation is more faster than that of its
unthreaded version, because with threaded binary tree
non-recursive implementation is possible which can run
faster and does not require the botheration of stack
management.
Threaded Binary Tree
• Advantages of threaded binary tree:
• The second advantage is more understated with a
threaded binary tree, we can efficiently determine the
predecessor and successor nodes starting from any
node. In case of unthreaded binary tree, however, this
task is more time consuming and difficult. For this
case a stack is required to provide upward pointing
information in the tree whereas in a threaded binary
tree, without having to include the overhead of using a
stack mechanism the same can be carried out with the
threads.
Threaded Binary Tree
• Advantages of threaded binary tree:
• Any node can be accessible from any other node. Threads are
usually more to upward whereas links are downward. Thus in a
threaded tree, one can move in their direction and nodes are in
fact circularly linked. This is not possible in unthreaded counter
part because there we can move only in downward direction
starting from root.
• Insertion into and deletions from a threaded tree are although
time consuming operations but these are very easy to
implement.
Threaded Binary Tree
• Disadvantages of threaded binary tree:
• Insertion and deletion from a threaded tree are very
time consuming operation compare to non-threaded
binary tree.
• This tree require additional bit to identify the threaded
link.

You might also like