Module No. 3 - Trees - Swe2001
Module No. 3 - Trees - Swe2001
Module No. 3 - Trees - Swe2001
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.
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
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.