You are on page 1of 36

JavaTM Education & Technology Services

Data Structures and Algorithms

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 1
Trees

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 2
Trees

– A tree is a collection of nodes with the following properties:


• The collection can be empty.

• Otherwise, a tree consists of a distinguished node r , called root, and zero


or more nonempty sub-trees T1, T2, … , Tk, each of whose roots are
connected by a directed edge from r.

– The root of each sub-tree is said to be child of r, and r is the parent of


each sub-tree root.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 3
Trees

– Terminology of Trees:
• Node (also called a Leaf): is any data item in the tree.

• Root Node(also called Parent Node) : is the first item in the tree.

• Subtree: is any piece (i.e., branch) of thy tree.

• Terminal Node: is a node that has no subtrees attached to it.

• Tree Height: is equal to the number of layers deep that its root grows.

• If a tree is a collection of N nodes, then it has N-1 edges.

N nodes = 6

N – 1 edges= 5
Tree Height = 3

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 4
Trees

– Terminology of Trees:
• A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk
such that ni is parent of ni+1 (1 ≤i < k)

• The length of a path is the number of edges on that path.

• There is a path of length zero from every node to itself.

• There is exactly one path from the root to each node.

• The depth of node ni is the length of the path from root to node ni

• The height of node ni is the length of longest path from node ni to a leaf.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 5
Trees

– A tree, with height and depth information for each Node


N nodes = 11 Node Height Depth
N – 1 edges= 10 A 3 0
Tree Height = 4 B 1 1
C 0 1
D 1 1
E 2 1
F 0 2
G 0 2
H 0 2
I 0 2
J 1 2
K 0 3

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 6
Binary Trees

– Binary Trees are special type of Trees because, when they are sorted,
they lend themselves to rapid searches, insertions, and deletions.

– Logically, one can think of binary trees as appearing in memory as they


do on paper, but remember that a tree is only a way to structure data in
memory, which is linear in form.

– The Binary Tree is a special form of Linked List:


• Items can be inserted, deleted, and accessed in any order.

• Also, the retrieval operation is nondestructive.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 7
Binary Trees

– Binary Tree – Representing Algebraic Expressions:


a–b a – b/c (a – b) * c

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 8
Binary Trees

– Height of Binary Tree

3 5 7

Binary trees with the same nodes but different heights

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 9
Binary Trees

– Full Binary Tree of height h, all nodes that are at a level less than h
have two children each.

– Each node in a full binary tree has left and right subtrees of the same
height.

– Among binary trees of height h, a full binary tree has as many leaves
as possible, and they all are at level h.

– Recursive definition of full binary tree:


• If T is not empty and has height h>0, T is a full binary tree if its root’s
subtrees are both full binary trees of height h-1.

– A binary tree is height balanced (or balanced), if the height of any node’s right
subtree differs from the height of the node’s left subtree by no more than 1.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 10
Binary Trees

– Represent these values in binary tree (5, 8, 2, 4, 10, 12, 7)

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 11
Binary Trees

– Traversing Binary Trees:


• is the process of accessing each node in a tree

– How a tree is ordered depends on how it is going to be accessed.


Generally, there are three ways to traverse a tree:
• Inorder: you visit the left subtree, the root, and then the right subtree.
• Preorder: you visit the root, the left subtree and then the right subtree.
• Postorder: you visit the left subtree, the right subtree, and then the root.

– Sorted Binary Tree: inorder traversing: is one where the subtree on


the left contains nodes that are less than or equal to the root, and those
on the right are greater than the root.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 12
Binary Trees

– Traversing Binary Trees:


• Inorder: you visit the left subtree, the root, and then the right subtree.
2,4,5,7,8,10,12

5
1

4 6
2

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 13
Binary Trees

– Traversing Binary Trees:


• Preorder: you visit the root, the left subtree and then the right subtree
5,2,4,8,7,10,12

4
2

3 5 6

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 14
Binary Trees

– Traversing Binary Trees:


• Postorder: you visit the left subtree, the right subtree, and then the root.
4,2,7,12,10,8,5

2 6

5
1 3

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 15
Binary Trees

– Operations of Binary Tree


• Creating a Tree (Inserting New Nodes)
– Case 1: insert root node.

– Case 2: insert a leaf node.

• Traversing the Tree


– Method 1: Inorder.

– Method 2: Preorder.

– Method 3: Postorder.

• Deleting Nodes
– Case 1: delete a leaf node.

– Case 2: delete a node with one subtree.

– Case 3: delete a node with two subtree.

• Searching for a particular Node.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 16
Binary Trees

– Building a Binary tree using Employee as tree node:

class Employee
{ :
:
int Code;
public:
Employee *pRight;
Employee *pLeft;

};

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 17
Binary Trees

– Building a Binary tree using Employee as tree node:

class BinaryTree{
Employee *pParent;
Employee *insert(Employee *pRoot, Employee *data);
void inOrder(Employee *pRoot);
void preOrder(Employee *pRoot);
void postOrder(Employee *pRoot);
Employee * deleteT(Employee *pRoot, int key );
int getHeight(Employee *pRoot);
public:
Binary(); { pParent = NULL; }
void insertNode(Employee *data);
Employee *searchTree(int code);
void inOrderTraverse();
void preOrderTraverse();
void postOrderTraverse();
void deleteNode(int key );
int getTreeHeight();
}

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 18
Binary Trees

– Building a Binary tree [Insert method]:

public void BinaryTree:: insertNode ( Employee * data){


pParent = insert(pParent , data);
}
private Employee * BinaryTree:: insert ( Employee * pRoot, Employee *data){
// 1. If the tree is empty , return a new single node as a root of the tree
if (pRoot == NULL){
data>pRight = NULL;
data>pLeft = NULL;
return (data);
}
// 2. otherwise , go down to insert it in the right place
else {
if (data->getCode() <= pRoot->getCode()) Add 5,8,2,4
pRoot->pLeft = insert(pRoot->pLeft,data);
else
pRoot->pRight= insert(pRoot->pRight,data);
return(pRoot); // return the unchanged pParent pointer
}
}
Java Education & Technology Services
TM

Copyright© Information Technology Institute http://jets.iti.gov.eg 19


Binary Trees
Insert 5,8,2,4
– Building a Binary tree [Insert method]:

Insert 5 Insert 8 Insert 2 Insert 4

5 5 5 5

8 2 8 2 8

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 20
Binary Trees

– Building a Binary tree [Search method]:

public Employee * BinaryTree:: searchTree( int code){


Employee *pRoot;
pRoot = pParent;
while (pRoot && code != pRoot->getCode())
{
if(code < pRoot ->getCode())
pRoot = pRoot -> pLeft ;
else
pRoot = pRoot -> pRight ;
Find 2
}
return pRoot;
}

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 21
Binary Trees

– Building a Binary tree [inOrderTraverse method]:

public void BinaryTree:: inOrderTraverse (){


inOrder (pParent);
}
private void BinaryTree:: inOrder ( Employee * pRoot){
if (pRoot)
{
inOrder(pRoot ->pLeft);
cout<<”Code : ” << pRoot ->getCode()<<endl;
inOrder(pRoot ->pRight);
}

}
Call
inOrderTraverse()

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 22
Binary Trees

– Building a Binary tree [preOrderTraverse method]:

public void BinaryTree:: preOrderTraverse (){


preOrder (pParent);
}
private void BinaryTree:: preOrder( Employee * pRoot){
if (pRoot)
{
cout<<”Code : ” << pRoot ->getCode()<<endl;
preOrder(pRoot ->pLeft);
preOrder(pRoot ->pRight);
}

}
Call
preOrderTraverse ()

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 23
Binary Trees

– Building a Binary tree [postOrderTraverse method]:

public void BinaryTree:: postOrderTraverse (){


postOrder (pParent);
}
private void BinaryTree:: postOrder ( Employee * pRoot){
if (pRoot)
{
postOrder(pRoot ->pLeft);
postOrder(pRoot ->pRight);
cout<<”Code : ” << pRoot ->getCode()<<endl;
}

}
Call
postOrderTraverse ()

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 24
Binary Trees

– Building a Binary tree [Tree Height method]:

public int BinaryTree:: getTreeHeight (){


return getHeight(pParent);
}
private int BinaryTree:: getHeight ( Employee * pRoot){
if (pRoot== NULL)
return 0;
else
{
int x= getHeight(pRoot->pLeft);
int y= getHeight(pRoot->pRight);
if (x>y)
return x+1;
else Call getTreeHeight ()
return y+1;
}

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 25
Binary Trees

– Building a Binary tree [Tree Height method]:

Y=4

5
X=2 Y=3

2 7
Y=1 Y=2
0 0
4 9
X=1
0
0 0
8
0 0

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 26
Binary Trees

– Building a Binary tree [Delete method]: 50


– There are three cases to consider
40 60
• Case 1: delete a leaf node.
– Replace the link to the deleted node by NULL.
30 45 70
• Case 2: delete a node with one subtree.
– The node can be deleted after its parent adjusts a link to bypass the node.

• Case 3: delete a node with two subtree.


– The deleted value must be replaced by an existing value that is either one of the
following:

» The largest value in the deleted node’s left subtree

» The smallest value in the deleted node’s right subtree.

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 27
Binary Trees

– Building a Binary tree [Delete method]:


Case 1: delete a leaf node [70].
Replace the link to the deleted node by NULL.

50 50

40 60 40 60

30 45 70 30 45

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 28
Binary Trees

– Building a Binary tree [Delete method]:


Case 2: delete a node with one subtree [60].
The node can be deleted after its parent adjusts a link to bypass the node.

50 50

40 60 40 70

30 45 70 30 45

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 29
Binary Trees

– Building a Binary tree [Delete method]:


Case 3: delete a node with two subtree [50].
The deleted value must be replaced by an existing value that is either one of the
following:
The largest value in the deleted node’s left subtree
The smallest value in the deleted node’s right subtree.

50 45 60

40 60 40 60 40 70

30 45 70 30 70 30 45

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 30
Binary Trees

– Building a Binary tree [Delete method]:

public void BinaryTree:: deleteNode ( int key){


pParent = deleteT(pParent , key);
}
private Employee * BinaryTree:: deleteT ( Employee * pRoot, int key){
// 1. Empty or Not found
Employee *p,*p2;
if(!pRoot)
return pRoot; /* Empty or Not found */
// 2. Delete the Root there is only one element in the tree
if(pRoot->getCode() == key)
{ 5
if(pRoot->pLeft == pRoot->pRight)
{
delete pRoot ;
return NULL;
}
……..// 1

}
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 31
Binary Trees

– Building a Binary tree [Delete method]:

private Employee * BinaryTree:: deleteT ( Employee * pRoot, int key){


// 3. has one subtree
……..// 1
else if (pRoot->pLeft == NULL) 50
{
p = pRoot->pRight;
delete pRoot; 60
return p;
}
else if(pRoot->pRight == NULL) 70
{
p = pRoot->pLeft; 50
delete pRoot ;
return p;
} 40

……..// 2
45
}
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 32
Binary Trees

– Building a Binary tree [Delete method]:

private Employee * BinaryTree:: deleteT ( Employee * pRoot, int key){


// 4. has two subtrees
50
……..// 2
else {
p2 = pRoot->pRight;
p = pRoot->pRight; 40 60
while(p->pLeft)
p = p->pLeft;
p->pLeft = pRoot->pLeft; 45 55 70
delete pRoot;
return p2;
} 60

……..// 3 55 70
}
40

45
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 33
Binary Trees

– Building a Binary tree [Delete method]:

private Employee * BinaryTree:: deleteT ( Employee * pRoot, int key){


……..// 3
if(pRoot->getCode() < key)
pRoot->pRight = DeleteNode(pRoot->pRight, key);
else
pRoot->pLeft = DeleteNode(pRoot->pLeft, key);
return pRoot;

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 34
Lab Exercise

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 35
Lab Exercise

• 1st Assignment :
• Implement the functions explained on Binary Tree in Lecture.
• Bonus:
– Implement DeleteNode () function on Binary Tree.
– Implement a function that will count the leaves of a binary tree.

• Search :
– Binary Search tree

JavaTM Education & Technology Services


Copyright© Information Technology Institute http://jets.iti.gov.eg 36

You might also like