HND in Computing and Software Engineering: Lesson 12 - Tree Data Structure
HND in Computing and Software Engineering: Lesson 12 - Tree Data Structure
Engineering
SEC5213: Data Structures and Algorithm
Level: 5
Credit Value: 20
1
Lecturer: Ms. Sathananthy 06/04/2022
Learning Outcomes 04
• Apply algorithms and data structures to solve programming problems.
2 06/04/2022
Outline
Fundamentals of tree DS
Binary tree
Develop algorithm
Implementation
7 06/04/2022
Tree Data Structure
A Tree is a recursive data structure containing the set of one or more data nodes where one node is
designated as the root of the tree while the remaining nodes are called as the children of the root.
The nodes other than the root node are partitioned into the non empty sets where each one of them is
to be called sub-tree.
Nodes of a tree either maintain a parent-child relationship between them or they are sister nodes.
In a general tree, A node can have any number of children nodes but it can have only a single parent.
The following image shows a tree, where the node A is the root node of the tree while the other
A data structure accessed beginning at the root node Each node is either a leaf
node or an internal node. Internal nodes have one or more child nodes and is
called the parent of its children.
All the children of the same parent are siblings. Contrary to a physical tree, the
root is usually depicted at the top of the structure, and the leaves are at the
bottom.
A tree is either empty (with no nodes), or a root and zero or more sub-trees
5 06/04/2022
Trees
A tree is either empty (with no nodes), or a root and zero or more sub-
trees.
6 06/04/2022
Basic Terminology
Root Node :- The root node is the topmost node in the tree hierarchy. In other
words, the root node is the one which doesn't have any parent.
Sub Tree :- If the root node is not null, the tree T1, T2 and T3 is called sub-trees
node. Leaf node is the bottom most node of the tree. There can be any number of
leaf nodes present in a general tree. Leaf nodes can also be called external nodes.
Path :- The sequence of consecutive edges is called path. In the tree shown in
root to that node. The root node doesn't have any ancestors. In the tree shown in
the above image, the node F have the ancestors, B and A.
Degree :- Degree of a node is equal to number of children, a node have. In the
tree shown in the above image, the degree of node B is 2. Degree of a leaf node
is always 0 while in a complete binary tree, degree of each node is equal to 2.
Level Number :- Each node of the tree is assigned a level number in such a way
that each node is present at one level higher than its parent. Root node of the tree
is always present at level 0
8 06/04/2022
Binary Trees
A binary tree is made of nodes
Each node contains a left pointer and a right pointer and data element
The root pointer points to the top most node in the tree
The left and right pointers recursively points to the left and right sub-trees on
either side
A null pointer represents a binary tree with no nodes.(an empty tree)
A binary tree is either empty or is made of a single node, where the left and
10 06/04/2022
Binary Trees
Binary Tree is a special type of generic tree in which, each node can have
at most two children. Binary tree is generally partitioned into three disjoint
subsets.
Root of the node
11 06/04/2022
Binary Trees
A type of binary tree where all the nodes are arranged in order
For each node, all elements in its left sub-tree are less than or equal to the
node, and all the elements in its right sub-tree are greater than the node
13 06/04/2022
Binary Search Trees (BSTs)
14 06/04/2022
Binary Search Trees (BSTs)
On average, a BST algorithm can locate a node in an N node tree in order log(N)
BSTs are good for dictionary problem where the code inserts and looks up
15 06/04/2022
Binary Search Trees (BSTs)
A major advantage of BSTs is the related sorting algorithms and search algorithms
BSTs are fundamental DS used as sets, multi sets and associative arrays
If a BST doesn’t allow duplicate values, then it represent a set with unique values
16 06/04/2022
Binary Search Trees (BSTs)
Operations on BST
Creating a tree
Inserting
Removing
Searching
Traversing
In order traversal:
Traverse the left sub-tree first, and then traverse the root and the right sub-tree
respectively. This procedure will be applied to each sub-tree of the tree recursively.
Algorithm:
18 06/04/2022
Binary Search Trees (BSTs)
Traverse the left sub-tree and then traverse the right sub-tree and root
recursively.
Algorithm:
Traverse the root first then traverse into the left sub-tree and right sub-tree
respectively. This procedure will be applied to each sub-tree of the tree recursively.
Algorithm:
20 06/04/2022
Binary Search Trees (BSTs)
Insert algorithm
If value we want to insert < key of the node, we have to go to the left sub-tree
If current node is empty create a new node with value we are inserting and place
21 06/04/2022
Binary Search Trees (BSTs)
Inserting 10 into the BST
22 06/04/2022
Binary Search Trees : Deletion
Delete algorithm
Similar to the insert function, after deletion of a node, the property of the BST
must be maintained
23 06/04/2022
Binary Search Trees : Deletion
Delete algorithm
Similar to the insert function, after deletion of a node, the property of the BST
must be maintained
24 06/04/2022
Binary Search Trees : Deletion
There are 3 possible cases
Replace the node with its child and make the parent of the deleted node to be the parent
Replace the value of the node to be deleted by the minimum value whose node was
26 06/04/2022
Binary Search Trees (BSTs)
Node to be deleted has no children
27 06/04/2022
Binary Search Trees (BSTs)
Node to be deleted has only one child
28 06/04/2022
Binary Search Trees (BSTs)
29 06/04/2022
Binary Search Trees (BSTs)
30 06/04/2022
Complexity Analysis : Binary Tree
Searching: For searching element 2, we have to traverse all elements
elements. Therefore, insertion in binary tree has worst case complexity of O(n).
31 06/04/2022
Complexity Analysis : BST
Searching: For searching element 1, have to traverse all
elements (in order 3, 2, 1). Therefore, searching in binary
search tree has worst case complexity of O(n). In general, time
complexity is O(h) where h is height of BST.
Insertion: For inserting element 0, it must be inserted as left
child of 1. Therefore, we need to traverse all elements (in order
3, 2, 1) to insert 0 which has worst case complexity of O(n). In
general, time complexity is O(h).
Deletion: For deletion of element 1, we have to traverse all
elements to find 1 (in order 3, 2, 1). Therefore, deletion in
binary tree has worst case complexity of O(n). In general, time
32 06/04/2022
complexity is O(h).
Full Binary Tree
which every node other than the leaves has two children.
A full binary tree of a given height h has 2 ^ h -1 nodes
33 06/04/2022
Numbering nodes in a full binary tree
1
Number nodes 1 through 2^h-1
Left child of node i is node 2i, unless 2i>n, where n is the number of nodes
35 06/04/2022
Complete Binary Tree with n Nodes
here.
36 06/04/2022
Binary Tree representation
Linked Representation
37 06/04/2022
Array Representation
Number the nodes using the numbering scheme for a full binary tree.
1 a
2 b 3 c
4 d 5 e 6 f 7 g
h i j a b c d e f g h i j
38 8 9 10 0 06/04/2022
10
Right- Skewed Binary Tree
An n node binary tree needs an array whose length is between n+1
and 2^n
1
a
3
b
7
c
a - b - - - c - - - - - - - d d 15
39 06/04/2022
Linked Representation
Each binary tree node has a left pointer, right pointer and data field
node)
40 06/04/2022
Linked Representation
root a
b c
d e
f g
41 06/04/2022
Implementation
42 06/04/2022
END
43 06/04/2022