0% found this document useful (0 votes)
13 views57 pages

TREE New Print

The document provides an overview of tree data structures, including definitions of key terminologies such as nodes, degrees, and types of trees. It explains binary trees, their properties, and operations such as insertion, searching, and deletion. Additionally, it covers traversal methods and their applications in mathematical expressions.

Uploaded by

betheltadele7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views57 pages

TREE New Print

The document provides an overview of tree data structures, including definitions of key terminologies such as nodes, degrees, and types of trees. It explains binary trees, their properties, and operations such as insertion, searching, and deletion. Additionally, it covers traversal methods and their applications in mathematical expressions.

Uploaded by

betheltadele7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

TREE

Tree Terminologies
 node: the item of information plus the branches to each
node.
 degree: the number of subtrees of a node
 degree of a tree: the maximum of the degree of the
nodes in the tree.
 terminal nodes (or leaf): nodes that have degree zero
 nonterminal nodes: nodes that don’t belong to terminal
nodes.
 children: the roots of the subtrees of a node X are the
children of X
 parent: X is the parent of its children.
 Some Terminology (cont’d)
 siblings: children of the same parent are said to be
siblings.
 Ancestors of a node: all the nodes along the path
from the root to that node.
 The level of a node: defined by letting the root be at
level one. If a node is at level l, then it children are at
level l+1.
 Height (or depth): the maximum level of any node in
the tree
Introduction
 Example
A is the root node
B is the parent of D and E Property: (# edges) = (#nodes) -
C is the sibling of B 1
D and E are the children of B
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The level of E is 3 Level
The height (depth) of the tree is 4
A 1
The degree of node B is 2
The degree of the tree is 3
The ancestors of node I is A, C, H B C
2
The descendants of node C is F, G, H, I

D G H 3
E F

I 4
 There are two classifications of data structures:

Linear Data Structures:


A data structure is said to be linear, if its elements
form a sequence or linear list.
Example: Arrays, linked lists, Stacks, and Queues.

Non-linear Data Structures:


A Data Structure is said to be non-linear, if its
elements do not form a sequence.
Example: Trees and Graphs.
TREE
• A tree is a set of nodes and edges that connect
pairs of nodes.

• Rooted tree has the following structure:


 One node distinguished as root.
 Every node C except the root is connected from exactly
other node P. P is C's parent, and C is one of P's
children.
 There is a unique path from the root to each node.
 The number of edges in a path is the length of the
path.
Tree Terminologies
 Consider the following tree. ABEFGCDHIJKLM.

B E F G

C D H I J

K L M
Root: a node with out a parent.
A
Internal node: a node with at least one child.

A, B, F, I, J
External (leaf) node: a node without a child.

 C, D, E, H, K, L, M, G
Ancestors of a node: parent, grandparent, grand-
grandparent, etc of a node.

Ancestors of K  A, F, I
Descendants of a node: children, grandchildren,
grand-grandchildren etc of a node.
Descendants of F  H, I, J, K, L, M

Depth of a node: number of ancestors or length


of the path from the root to the node.
Depth of H  2
Height of a tree: depth of the deepest node.
3
Subtree: a tree consisting of a node and its
descendants.
A
F

B E F G
H I J
C D H I J
K L M
K L M
Binary tree: a tree in which each node has at
most two children called left child and right
child.
Binary Tree …(continued)

12
Full binary tree: a binary tree where each
node has either 0 or 2 children.
Balanced binary tree: a binary tree where each
node except the leaf nodes has left and right
children and all the leaves are at the same
level.
Complete binary tree: a binary tree in which
the length from the root to any leaf node is
either h or h-1.
 where h is the height of the tree.
The deepest level should also be filled from
left to right.
Binary search tree (ordered binary tree)
 A binary tree that may be empty, but if it is not empty
it satisfies the following.

 Every node has a key and no two elements have


the same key.

 The keys in the right subtree are larger than the


key in the root.

 The keys in the left subtree are smaller than the


key in the root.

 The left and the right subtrees are also binary


search trees.
Examples of Binary Search Tree.
10

6 15

4 8 14 18

7 12 16 19

11 13
Binary Search Tree …(continued)

• Here are some Binary Search Trees in which


each node just stores an integer key:

18
Binary Search Tree …(continued)

• These are not Binary Search Trees:

• In the left one 5 is not greater than 6. In the right


one 6 is not greater than 7.

19
Binary Search Tree …(continued)

• The reason Binary-Search Trees are


important is that the following operations
can be implemented efficiently using a
Binary Search Tree:
– Insert a key value.
– Determine whether a key value is in the
tree.
– Remove a key value from the tree.
– Print all of the key values in sorted
order. 20
Data Structure of a Binary Tree
Syntax: struct DataModel
{
Declaration of data fields
DataModel * Left, *Right;
};
DataModel *RootDataModelPtr=NULL;
Example:
struct Node
{
int Num;
Node * Left, *Right;
};
Node *RootNodePtr=NULL;
Operations on Binary Search Tree

 Consider the following definition of binary


search tree.
struct Node
{
int Num;
Node * Left, *Right;
};
Node *RootNodePtr=NULL;
Insertion

• When a node is inserted the definition of binary


search tree should be preserved.

• Suppose there is a binary search tree whose root


node is pointed by RootNodePtr.

• We want to insert a node (that stores 17) pointed by


InsNodePtr.
Case 1: There is no data in the tree:
(i.e. RootNodePtr is NULL)

 The node pointed by InsNodePtr should be made


root node.

RootNodePtr RootNodePtr
InsNodePtr

17
17
Case 2: If there is data in the tree:
 Search the appropriate position.

 Insert the node in that position.


RootNodePtr
InsNodePtr RootNodePtr

InsertBST(RootNodePtr, InsNodePtr) 
17 10 10

6 15 6 15

4 8 14 4 8 14
18 18

7 12 12
16 19 7 16 19

11 13 11 13 17
Traversing
• Binary search tree can be traversed in three ways.

 Preorder traversal:- traversing binary tree in the


order of parent, left and right.

 Inorder traversal:- traversing binary tree in the


order of left, parent and right.

 Postorder traversal:- traversing binary tree in


the order of left, right and parent.
Example: RootNodePtr

10

6 15

4 8 14 18

7 12
16 19

11 13 17
Preorder traversal:
10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19

Inorder traversal:
4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
• Used to display nodes in ascending order.

Postorder traversal:
4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
Application of binary tree traversal

• Store values on leaf nodes and operators on internal


nodes:
 Preorder traversal: - used to generate mathematical
expression in prefix notation.

 Inorder traversal: - used to generate mathematical


expression in infix notation.

 Postorder traversal: - used to generate mathematical


expression in postfix notation.
+
Example:
– +

A * D /

B C E F

Preorder traversal: + – A * B C + D / E F  Prefix notation


Inorder traversal: A – B * C + D + E / F  Infix notation
Postorder traversal: A B C * – D E F / + +  Postfix notation
Preorder traversal
1. Process the value in the root (e.g. print the root value).
2. Traverse the left subtree with a preorder traversal.
3. Traverse the right subtree with a preorder traversal.
Inorder traversal :- prints the node values in ascending order:
1. Traverse the left subtree with an inorder traversal.
2. Process the value in the root (e.g. print the root value).
3. Traverse the right subtree with an inorder traversal.
Postorder traversal
1. Traverse the left subtree with a postorder traversal.
2. Traverse the right subtree with a postorder traversal.
3. Process the value in the root (e.g. print the root value).
Implementation:
void Preorder (Node *RootNodePtr)

{
if(RootNodePtr ! = NULL)
{
cout<< RootNodePtr ->Num; // or any operation on the
node
Preorder(RootNodePtr ->Left);
Preorder(RootNodePtr ->Right);
}
}
void Inorder (Node * RootNodePtr)
{
if(RootNodePtr ! = NULL)
{
Inorder(RootNodePtr Left);
cout<< RootNodePtr Num; // or any operation on the
node

Inorder(RootNodePtr Right);
}
}
void Postorder (Node * RootNodePtr)
{
if(RootNodePtr ! = NULL)
{
Postorder(RootNodePtr Left);
Postorder(RootNodePtr Right);
cout<<RootNodePtr Num; //or any operation on the
node

}
}
Searching

• To search a node (whose Num value is X) in a


binary search tree (whose root node is pointed
by RootNodePtr).

• One of the three traversal methods can be used.


RootNodePtr

10

6 15

4 8 14 18

7 12
16 19

11 13 17
Implementation:
int SearchBST (Node *RootNodePtr, int X)
{
if(RootNodePtr == NULL)
return 0; // 0 means (false, not found).
else if(RootNodePtr Num == X)
return 1; // 1 means (true, found).
else if(RootNodePtr Num > X)
return(SearchBST(RootNodePtr Left, X));
else
return(SearchBST(RootNodePtr Right, X));
}
Finding Minimum value in a
Binary Search Tree
• We can get the minimum value from a
Binary Search Tree, by locating the left
most node in the tree.

• Then after locating the left most node, we


display the value of that node.
RootNodePtr

10

Minimum
6 15

4 8 14 18

7 12
16 19

11 13 17
Implementation:

int findMin(Node *RootNodePtr) {


if(RootNodePtr == NULL)
return -1;
else if(RootNodePtr ->Left == NULL)
return RootNodePtr ->Num;
else
return findMin(RootNodePtr ->Left);
}
Finding Maximum value in a
Binary Search Tree
• We can get the maximum value from a
Binary Search Tree, by locating the right
most node in the tree.

• Then after locating the right most node,


we display the value of that node.
RootNodePtr

10

6 15
Maximum
4 8 14 18

7 12
16 19

11 13 17
Implementation:

int findMax(Node *RootNodePtr) {


if(RootNodePtr == NULL)
return -1;
else if(RootNodePtr ->Right == NULL)
return RootNodePtr ->Num;
else
return findMax(RootNodePtr -
>Right);
}
Deleting
• There are a number of cases we may encounter when
deleting a node in a binary search tree. Among these
cases:
– Deleting a root node.
– Deleting a leaf node.
– Deleting a node having only left child.
– Deleting a node having only right child.
– Deleting a node having both left and right child nodes.
Example: Suppose we want to delete Node 6 from the
binary search tree shown in the next slide. (Node 6
has both left and right child nodes).
RootNodePtr

To be deleted 10

6 16

5 8 13 23

7 9
2 14
Implementation:
Node *temp = RootNodePtr;
Node *temp2;
temp = temp ->Left;
RootNodePtr ->Left = temp ->Right;
temp2 = temp ->Right;
temp2 = temp2 ->Left;
temp2 ->Left = temp ->Left;
Temp ->Left = NULL;
Temp ->Right = NULL;
delete temp;
The binary search tree after deleting Node 6.
RootNodePtr

10
Deleted
6 16

8 13 23
temp2
7 9
14

You might also like