0% found this document useful (0 votes)
7 views

Tree Data Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Tree Data Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

Tree Data Structures

Tree Data Structures


• There are a number of applications where
linear data structures are not appropriate.
• Consider a genealogy tree of a family.
Tree Data Structure
• A linear linked list will not be able to
capture the tree-like relationship with
ease.
• Shortly, we will see that for applications
that require searching, linear data
structures are not suitable.
Properties of Tree
• Each tree has one node designated as the root
of the tree.
• A unique path exist from root node to all other
nodes in the tree.
• Each node, other than the root, has a unique
predecessor. It is called the parent.
• Each node may have no, one or several
successor nodes. The successor node is called
the child.
Basic Terms
• Root node: The unique node in the tree that
has no parent node is called the root node.it
present the beginning of tree.
• Parent node: The node which generates links
for other nodes is known as parent node.
• Child node: The node that is directly
connected to a parent node is called child
node.
Basic Terms (cont.)
• Subtree: The child node of the root node that has its
own child nodes is called subtree.
• Terminal node: The node having no child is called
terminal node.
• Siblings: The node having a common parent are
called siblings.
• Empty tree: A tree that have no node is called
empty tree.
• Degree of node: Number of children of a node is
called degree of the node.
Basic Terms (cont.)
• Singleton tree:
The tree whose root is its only node is called the
singleton tree.
Types of Trees in Data Structures

• Binary Tree.
• Ternary Tree.
• N-ary Tree (Generic Tree)
• Binary Search Tree.
• AVL Tree.
• Red-Black Tree.
• B-Tree.
• B+ Tree.
Binary Tree
• A binary tree is a finite set of elements that is
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 subtrees.
• Each element of a binary tree is called a node of
the tree.
• Each node can have maximum two children.
Binary Tree
• Binary tree with 9 nodes.
A

B C

D E F

G H I
Binary Tree
root

B C

D E F

G H I

Left subtree Right subtree


Binary Tree
• Recursive definition
A
root

B C

D E F

Left subtree G H I

Right subtree
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree
Binary Tree
• Recursive definition
A

B C

D E F

root
G H I
Binary Tree
• Recursive definition
A
root

B C

D E F

G H I

Right subtree
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree Right subtree


Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Binary Tree: Terminology

parent
A

Left descendant B C Right descendant

D E F

G H I

Leaf nodes Leaf nodes


Binary Tree
• If every non-leaf node in a binary tree has non-empty left and right subtrees, the
tree is termed a strictly binary tree.

B C

D E J F

G K H I
Level of a Binary Tree Node
• The level of a node in a binary tree is
defined as follows:
 Root has level 0,
 Level of any other node is one more than the
level its parent (father).
• The depth of a binary tree is the maximum
level of any leaf in the tree.
Level of a Binary Tree Node

A 0 Level 0

B 1 C 1 Level 1

D 2 E 2 F 2 Level 2

G 3 H 3 I 3 Level 3
Complete Binary Tree
• A complete binary tree of depth d is the strictly
binary all of whose leaves are at level d.
0
A

B 1 C 1

D 2 E 2 F 2 G 2

H 3 I J 3 K L 3 M 3 N 3 O 3
Complete Binary Tree

A Level 0: 20 nodes

B C Level 1: 21 nodes

D E F G Level 2: 22 nodes

H I J K L M N O Level 3: 23 nodes
Complete Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of depth
d:
d
20+ 21+ 22 + ………. + 2d =  2j = 2d+1 – 1
j=0

• In a complete binary tree, there are 2d leaf


nodes and (2d - 1) non-leaf (inner) nodes.
Complete Binary Tree
• If the tree is built out of ‘n’ nodes then

n = 2d+1 – 1
or log2(n+1) = d+1
or d = log2(n+1) – 1
• I.e., the depth of the complete binary tree built
using ‘n’ nodes will be log2(n+1) – 1.
• For example, for n=1,000,000, log2(1000000) is
less than 20; the tree would be 20 levels deep.
Operations on Binary Tree
• There are a number of operations that can
be defined for a binary tree.
• If p is pointing to a node in an existing tree
then
 left(p) returns pointer to the left subtree
 right(p) returns pointer to right subtree
 parent(p) returns the father of p
 brother(p) returns brother of p.
 info(p) returns content of the node.
Operations on Binary Tree
• In order to construct a binary tree, the
following can be useful:
• setLeft(p,x) creates the left child node of p.
The child node contains the info ‘x’.
• setRight(p,x) creates the right child node
of p. The child node contains the info ‘x’.
Applications of Binary Trees
• A binary tree is a useful data structure
when two-way decisions must be made at
each point in a process.
• For example, suppose we wanted to find
all duplicates in a list of numbers:

14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Applications of Binary Trees
• One way of finding duplicates is to
compare each number with all those that
precede it.

14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Binary Search Tree
• A binary tree with the property that items in
the left subtree are smaller than the root
and items are larger or equal in the right
subtree is called a binary search tree
(BST).
• The tree we built for searching for
duplicate numbers was a binary search
tree.
• BST and its variations play an important
role in searching algorithms.
Searching for Duplicates
• If the list of numbers is large and is
growing, this procedure involves a large
number of comparisons.
• A linked list could handle the growth but
the comparisons would still be large.
• The number of comparisons can be
drastically reduced by using a binary tree.
• The tree grows dynamically like the linked
list.
Searching for Duplicates
• The binary tree is built in a special way.
• The first number in the list is placed in a
node that is designated as the root of a
binary tree.
• Initially, both left and right subtrees of the
root are empty.
• We take the next number and compare it
with the number placed in the root.
• If it is the same then we have a duplicate.
Searching for Duplicates
• Otherwise, we create a new tree node and
put the new number in it.
• The new node is made the left child of the
root node if the second number is less
than the one in the root.
• The new node is made the right child if the
number is greater than the one in the root.
Searching for Duplicates

14

14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

15 14

15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

15

15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

4 14

15

4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

9 14

4 15

9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

7 14

4 15

7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

18 14

4 15

18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

9 18

18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

3 14

4 15

9 18

3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

5 14

4 15

3 9 18

5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

16 14

4 15

3 9 18

16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

7 16

16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

4 14

4 15

3 9 18

7 16

4, 20, 17, 9, 14, 5


Searching for Duplicates

20 14

4 15

3 9 18

7 16

20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

7 16 20

20, 17, 9, 14, 5


Searching for Duplicates

17 14

4 15

3 9 18

7 16 20

17, 9, 14, 5
Searching for Duplicates

14

4 15

3 9 18

7 16 20

5 17

17, 9, 14, 5
Searching for Duplicates

14

4 15

3 9 18

7 16 20

5 17

9, 14, 5
C++ Implementation
#include <stdlib.h>
template <class Object>
class TreeNode {
public:
// constructors
TreeNode()
{
this->object = NULL;
this->left = this->right = NULL;
};
TreeNode( Object* object )
{
this->object = object;
this->left = this->right = NULL;
};
C++ Implementation
Object* getInfo()
{
return this->object;
};
void setInfo(Object* object)
{
this->object = object;
};
TreeNode* getLeft()
{
return left;
};
void setLeft(TreeNode *left)
{
this->left = left;
};
C++ Implementation
TreeNode *getRight()
{
return right;
};
void setRight(TreeNode *right)
{
this->right = right;
};

int isLeaf( )
{
if( this->left == NULL && this->right == NULL )
return 1;
return 0;
};
C++ Implementation
private:
Object* object;
TreeNode* left;
TreeNode* right;
}; // end class TreeNode
C++ Implementation
#include <iostream>
#include <stdlib.h>
#include "TreeNode.cpp"

int main(int argc, char *argv[])


{
int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17,
9, 14,5, -1};
TreeNode<int>* root = new TreeNode<int>();
root->setInfo( &x[0] );
for(int i=1; x[i] > 0; i++ )
{
insert(root, &x[i] );
}
}
C++ Implementation
void insert(TreeNode<int>* root, int* info)
{
TreeNode<int>* node = new TreeNode<int>(info);
TreeNode<int> *p, *q;
p = q = root;
while( *info != *(p->getInfo()) && q != NULL )
{
p = q;
if( *info < *(p->getInfo()) )
q = p->getLeft();
else
q = p->getRight();
}
C++ Implementation
if( *info == *(p->getInfo()) ){
cout << "attempt to insert duplicate: "
<< *info << endl;
delete node;
}
else if( *info < *(p->getInfo()) )
p->setLeft( node );
else
p->setRight( node );
} // end of insert
Trace of insert
p
17 q 14

4 15

3 9 18

7 16 20

17, 9, 14, 5
Trace of insert
p
17 14

4 q 15

3 9 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 p 15
q
3 9 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 p 15

3 9 q 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 p 18
q
7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 p 18

7 q 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 18

7 p 16 20
q
5

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 18

7 p 16 20

5 q

17, 9, 14, 5
Trace of insert

14

4 15

3 9 18

7 p 16 20

5 node 17

17, 9, 14, 5 p->setRight( node );


Cost of Search
• Given that a binary tree is level d deep.
How long does it take to find out whether a
number is already present?
• Consider the insert(17) in the example
tree.
• Each time around the while loop, we did
one comparison.
• After the comparison, we moved a level
down.
Cost of Search
• With the binary tree in place, we can write
a routine find(x) that returns true if the
number x is present in the tree, false
otherwise.
• How many comparison are needed to find
out if x is present in the tree?
• We do one comparison at each level of the
tree until either x is found or q becomes
NULL.
Cost of Search
• If the binary tree is built out of n numbers,
how many comparisons are needed to find
out if a number x is in the tree?
• Recall that the depth of the complete
binary tree built using ‘n’ nodes will be
log2(n+1) – 1.
• For example, for n=1,000,000,
log2(100000) is less than 20; the tree
would be 20 levels deep.
Cost of Search
• If the tree is complete binary or nearly
complete, searching through 1,000,000
numbers will require a maximum of 20
comparisons.
• Or in general, approximately log2(n).
• Compare this with a linked list of 100,000
numbers. The comparisons required could
be a maximum of n.
TRAVERSING
Traversing a Binary Tree
• Suppose we have a binary tree, ordered
(BST) or unordered.
• We want to print all the values stored in
the nodes of the tree.
• In what order should we print them?
Traversing a Binary Tree
• Ways to print a 3 node tree:

14

4 15

(4, 14, 15), (4,15,14)


(14,4,15), (14,15,4)
(15,4,14), (15,14,4)
Traversing a Binary Tree
• In case of the general binary tree:
N node

L left right R
subtree subtree

(L,N,R), (L,R,N)
(N,L,R), (N,R,L)
(R,L,N), (R,N,L)
Traversing a Binary Tree
• Three common ways
N node

left right
L subtree subtree R

Preorder: (N,L,R)
Inorder:
(L,N,R)
Postorder: (L,R,N)
Traversing a Binary Tree
14

4 15

3 9 18

7 16 20

5 17

Preorder: 14 4 3 9 7 5 15 18 16 17 20
Traversing a Binary Tree
14

4 15

3 9 18
20
7 16

5 17

Inorder: 3 4 5 7 9 14 15 16 17 18 20
Traversing a Binary Tree
14

4 15

3 9 18
20
7 16

5 17

Postorder: 3 5 7 9 4 17 16 20 18 15 14
Recursive Call
• Recall that a stack is used during function
calls.
• The caller function places the arguments
on the stack and passes control to the
called function.
• Local variables are allocated storage on
the call stack.
• Calling a function itself makes no
difference as far as the call stack is
concerned.
Stack Layout during a call
• Here is stack layout when function F calls
function F (recursively):
Parameters(F) Parameters(F) Parameters(F)
Local variables(F) Local variables(F) Local variables(F)
Return address(F) Return address(F) Return address(F)
sp
Parameters(F) Parameters(F)
sp
Local variables(F)

Return address(F)
sp
At point of call During execution of F After call
Recursion: preorder
14 preorder(14)
14
..preorder(4)
4 15 4
....preorder(3)
3
......preorder(null)
3 9 18 ......preorder(null)
....preorder(9)
9
7 16 20 ......preorder(7)
7
........preorder(5)
5 5
17 ..........preorder(null)
..........preorder(null)
........preorder(null)
......preorder(null)
Recursion: preorder
14 ..preorder(15)
15
....preorder(null)
4 15 ....preorder(18)
18
......preorder(16)
16
3 9 18 ........preorder(null)
........preorder(17)
17
7 16 20 ..........preorder(null)
..........preorder(null)
......preorder(20)
5 20
17 ........preorder(null)
........preorder(null)
Recursion: inorder
14 inorder(14)
..inorder(4)
....inorder(3)
4 15 ......inorder(null)
3
......inorder(null)
4
3 9 18 ....inorder(9)
......inorder(7)
........inorder(5)
7 16 20 ..........inorder(null)
5
..........inorder(null)
5 7
17 ........inorder(null)
9
......inorder(null)
14
Recursion: inorder
14 ..inorder(15)
....inorder(null)
15
4 15 ....inorder(18)
......inorder(16)
........inorder(null)
16
3 9 18 ........inorder(17)
..........inorder(null)
17
7 16 20 ..........inorder(null)
18
......inorder(20)
5 ........inorder(null)
17 20
........inorder(null)
Ternary Tree

• Each node in a ternary tree has three children (left,


middle, and right).
• Ternary trees are used in specific applications, like
Ternary Search Trees for efficient search operations
with strings.
N-ary Tree (Generic Tree)
• A generalization of a binary tree, where each
node can have N children.
• N-ary trees are commonly used in scenarios
where each node can have multiple branches,
such as a file system directory structure.
AVL Tree

• AVL (Adelson-Velskii and Landis) tree.


• An AVL tree is identical to a BST except
 height of the left and right subtrees can differ
by at most 1.
 height of an empty tree is defined to be (–1).
AVL Tree

• An AVL Tree level

5 0

2 8 1

1 4 7 2

3 3
AVL Tree

• Not an AVL tree level

6 0

1 8 1

1 4 2

3 5 3
Red-Black Tree
• Another type of self-balancing binary search
tree that uses color properties (red or black)
to enforce balance during insertions and
deletions.
• Widely used in memory management and
data compression due to its balanced nature.
If data is inserted in a non-random sequence,
the tree might become unbalanced, seriously
degrading its performance.
A red-black tree can fix this by ensuring that the
tree remains balanced at all times.
• Let’s understand the different scenarios of a
binary search tree.

In the above tree, if we want to search the 80. We will first compare
80 with the root node and so on. The above BST will take O(logn) time to
search the element.
The above tree shows the right-skewed BST. If we want to search
the 80 in the tree, we will compare 80 with all the nodes until we
find the element or reach the leaf node. So, the above right-
skewed BST will take O(N) time to search the element.

Therefore, we need a balanced tree, and the Red-Black tree is a self-balanced


binary search tree.
Properties of Red-Black tree
• It is a self-balancing Binary Search tree. Here, self-balancing means that it
balances the tree itself by either doing the rotations or recoloring the
nodes.
• This tree data structure is named as a Red-Black tree as each node is either
Red or Black in color. Every node stores one extra information known as a
bit that represents the color of the node.
• In the Red-Black tree, the root node is always black in color.
• In a binary tree, we consider those nodes as the leaf which have no child. In
contrast, in the Red-Black tree, the nodes that have no child are considered
the internal nodes and these nodes are connected to the NIL nodes that are
always black in color. The NIL nodes are the leaf nodes in the Red-Black
tree.
• If the node is Red, then its children should be in Black color. In other words,
we can say that there should be no red-red parent-child relationship.
Insertion in the Red-Black tree.

10, 18, 7, 15, 16, 30, 25, 40, 60


Step 1: Initially, the tree is empty, so we create a new node having
value 10. This is the first node of the tree, so it would be the root
node of the tree. As we already discussed, that root node must be
black in color, which is shown below
Step 2: The node is 18. As 18 is
greater than 10 so it will come at the
right of 10 as shown below.

We know the second rule of the Red Black tree that if the tree
is not empty then the newly created node will have
the Red color.
• Step 3: Now, we create the new node having
value 7 with Red color. As 7 is less than 10, so
it will come at the left of 10 as shown below.
• Step 4: The next element is 15, and 15 is
greater than 10, but less than 18, so the new
node will be created at the left of node 18.
The node 15 would be Red in color as the tree
is not empty.

As we can observe in the above figure, the parent's parent of a new


node is the root node, so we do not need to recolor it.
• Step 5: The next element is 16. As 16 is greater than
10 but less than 18 and greater than 15, so node 16
will come at the right of node 15. The tree is not
empty; node 16 would be Red in color, as shown in
the below figure:

In the above figure, we can observe that it


violates the property of the parent-child
relationship as it has a red-red parent-child
relationship. We have to apply some rules
to make a Red-Black tree. Since the new
node's parent is Red color, and the parent
of the new node has no sibling, so
rule 4a will be applied. The rule 4a says
that some rotations and recoloring would
be performed on the tree.
• Step 6: The next element is 30. Node 30 is
inserted at the right of node 18. As the tree is
not empty, so the color of node 30 would be
red.

You might also like