Module No. 3 - Trees - Swe2001

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

1

DATA STRUCTURES AND ALGORITHMS


Course Code: SWE2001
MODULE – 3
TREES
Introduction to Non-Linear Data Structures 2
A data structure is said to be Non-Linear if its elements are not arranged in a sequential
order. The common examples of Non-Linear Data Structures are
1. Trees
2. Graphs
A tree is a collection of nodes connected by directed (or undirected) edges. A tree is
a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are
linear data structures. A tree can be empty with no nodes or a tree is a structure consisting
of one node called the root and zero or one or more subtrees. A tree has following
general properties:
 One node is distinguished as a root;
 Every node (exclude a root) is connected by a directed edge from exactly one other
node;
A direction is: parent -> children
Introduction to Trees: 3
A tree is a finite set of one or more nodes such that
1. There is a specially designated node called the root;
2. The remaining nodes are partitioned into n>= 0 disjoint sets T1, ...,Tn where each of
these sets is a tree. T1, ...,Tn are called the subtrees of the root.
 Degree of a node: It is the number of sub trees of node in a given tree. In the above tree
Degree of node A is 3 4
Degree of node B is 2
Degree of node C is 2
Degree of node D is 3
 Degree of a tree: The degree of a tree is the maximum degree of node in a given tree.
In the above tree Degree of tree is 4.
 Terminal Node or Leaf Node: A node with degree zero is called a terminal node or a leaf
node.
 Non terminal Node: Any node whose degree is not zero is called a non terminal node.
 Level of a node: The level of a node is defined by 1 + the number of connections between the
node and the root. The level of a root node is 0. If a node is at level l, then its children are at
level l + 1.
Level of node B is 1
Degree of node E is 2
Degree of node L is 3
 Height of the tree: Height of a tree is the maximum level of any node in a given tree.
Height of the above tree is 3.
Different types of TREES 5
There are various types of trees, and each of these trees has unique characteristics.
The Common Trees used are,
1. General Tree
2. Binary Tree
3. Binary Search Tree
4. Extended Binary Tree or 2-Trees
5. Threaded Binary Tree
6. AVL Trees
7. 2-3 Trees
8. Heap Trees
9. Red-Black Trees
10. B-Trees
General Tree 6

A general tree also called as tree is defined as a non-empty finite set of T of elements
called nodes such that
i. There is a specially designated node called the root;
ii. The remaining nodes are partitioned into n>= 0 disjoint sets T1, ...,Tn where
each of these sets is a tree. T1, ...,Tn are called the subtrees of the root.
Binary tree and Binary Search Tree 7
Binary Tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each
node in a tree can have utmost two child nodes. Here, utmost means whether the node has 0
nodes, 1 node or 2 nodes.

Binary Search Tree: A Binary Search Tree is a binary tree, which is either empty or satisfies the
following properties:
1. Every node has a value and no two nodes have the same value (i.e., all values are unique).
2. If there exists a left child or left sub tree then its value is less than the value of the root.
3. The value(s) in the right child or right sub tree is greater than the value of the root node.
Extended Binary Tree and Threaded Binary Tree 8
Extended Binary Tree: A binary tree can be converted to an extended binary tree by adding new
nodes to its leaf nodes, and to the nodes that have only one child. These new nodes are added in
such a way that all the nodes in the resultant tree have either zero or two children. The extended tree
is also known as a 2-tree. The nodes of the original tree are called Internal nodes and the new nodes
that are added to binary tree, to make it extended binary tree, are called external nodes.
Threaded Binary Tree: A threaded binary tree is a binary tree in which NULL pointers of leaf node
are replaced with pointers that point to inoder successor. This facilitates traversal of the tree in inorder.

Threaded Binary Tree


AVL Trees 9
AVL Trees: AVL tree is a height balanced tree. It is a self-balancing Binary Search
Tree (BST). When Binary Search Tree (BST) is constructed if the difference
between heights of left and right subtrees is more than one then the tree is adjusted
such that the heights of left and right subtrees is not more than one for all nodes.
2-3 Trees 10
2-3 Trees: 2-3 tree is a tree data structure in which every internal node (non-leaf node) has either
one data element and two children or two data elements and three children. If a node contains
one data element leftVal, it has two subtrees (children) namely left and middle. Whereas if a
node contains two data elements leftVal and rightVal, it has three subtrees namely left, middle
and right. The insertion and deletion in an AVL tree involves many rotations to make it height
balanced tree. This makes the entire operation complicated. To eliminate this complication 2-3
trees are used.
Heap Trees
11
Heap Trees: A Heap is a special Tree which is a complete binary tree. There are two types of Heap
Trees. They are
1. Max-Heap Tree
2. Min-Heap Tree
Max-Heap: In a Max-Heap the value present at the root node must be greatest among the values
present at all of its children. The same property must be recursively true for all sub-trees in that
Heap Tree.
Min-Heap: In a Min-Heap the value present at the root node must be minimum among the values
present at all of its children. The same property must be recursively true for all sub-trees in that
Heap Tree.
Red-Black Trees 12
Red-Black Trees: A red-black tree is a binary search tree with one extra attribute for each
node i.e. the color, which is either red or black. The properties of red- black tree are
1. Every node is either red or black.
2. Every leaf node is black.
3. If a node is red, then both its children are black.
4. Every simple path from a node to a descendant leaf contains the same number of black
nodes.
B- Tree 13
B- Tree: A B-tree is a tree that keeps data sorted and allows searches, insertions, and
deletions in logarithmic time. Unlike self-balancing binary search trees, it is optimized for
systems that read and write large blocks of data. It is most commonly used in database
and file systems. The properties of B-Tree are
1. B-tree nodes have many more than two children.
2. A B-tree node may contain more than just a single element.
Introduction to Binary Tree 14
Definition: A binary tree is a tree in which no node can have more than two children. The
children of a binary tree are described as left child and right child of the parent node.
A binary tree T is defined as a finite set of elements called nodes such that,
i. T is empty (i.e. if T has no nodes called the null tree or empty tree)
ii. T contains a special node R, called the root node of T, and the remaining nodes of T
form disjoint binary trees T1 and T2 and they are called as left child and right child of the
parent node T.

Fig: Binary Tree


Basic Definition of Binary Trees
15
Basic Definition of Binary Trees: Consider the tree in which A is the root as given below.
 Father: Root of tree is called as the father of the tree. Thus, A is the father of the binary tree given below.
 Son: The root of the subtrees is called the son of the tree. Thus B, C are the sons of the node as given below.
 Leaf Node: The leaf node is a node which has no sons. Thus D, G, H, I are the leaf nodes of the binary tree as
given below.

 Ancestor and Descendants: A node that is connected to the lower-level nodes is called an "ancestor". The
connected lower-level nodes are "descendants" of the ancestor. Thus A is called as ancestor of B,C,D,…. and
B,C,D,…. are called as descendants in the given binary tree.
 Left Descendant and Right Descendant: A node is called as left descendant if it is the left child of its ancestor. A
node is called as right descendant if it is the right child of its ancestor. Thus, B is called as left descendant of A
and C is called as right descendant of A in the given binary tree.
 Siblings: All the children of a given node are known as siblings. Thus B & C are siblings in the given binary tree.
Types of Binary Trees 16
There are different types of binary trees. They are

1. Full Binary Tree


2. Complete Binary Tree
3. Degenerate Binary Trees
4. Perfect Binary Trees
5. Balanced Binary Trees
6. Binary Search Trees
17

 Full Binary Tree: A Full Binary Tree is a binary tree in which every node has zero or two
children.
 Complete Binary Tree: A Complete Binary Tree has all levels completely filled with
nodes except the last level and in the last level, all the nodes are as left side as possible.
 Degenerate (or Pathological) Binary Tree: A Degenerate Binary Tree is a Binary Tree
where every parent node has only one child node.
 Perfect Binary T ree: A Perfect Binary Tree is a Binary Tree in which all internal nodes
have 2 children and all the leaf nodes are at the same depth or same level.
 Balanced Binary Tree: A Balanced Binary Tree is a Binary tree in which height of the left
and the right sub-trees of every node may differ by at most 1.
Binary Search Trees
18
A binary search tree follows some order to arrange the elements. In a Binary search tree, the value of left
node must be smaller than the parent node, and the value of right node must be greater than the parent
node. This rule is applied recursively to the left and right subtrees of the root.

In the above Diagrams First tree, we can observe that the root node is 40, and all the nodes of the left
subtree are smaller than the root node, and all the nodes of the right subtree are greater than the root node.
Similarly, we can see the left child of root node is greater than its left child and smaller than its right child.
So, it also satisfies the property of binary search tree. Therefore, we can say that the tree in the above
image is a binary search tree.
In Second tree, the value of root node is 40, which is greater than its left child 30 but smaller than right
child of 30, i.e., 55. So, the above tree does not satisfy the property of Binary search tree. Therefore, the
above tree is not a binary search tree.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy