Professional Documents
Culture Documents
Unit 3 Trees
Unit 3 Trees
•Root − The node at the top of the tree is called root. There is only one root per tree and one path from the
root node to any node.
•Parent − Any node except the root node has one edge upward to a node called parent.
•Child − The node below a given node connected by its edge downward is called its child node.
•Leaf − The node which does not have any child node is called the leaf node.
•Subtree − Subtree represents the descendants of a node.
•Visiting − Visiting refers to checking the value of a node when control is on the node.
•Levels − Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
•keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
Binary Tree
Tree having children 0,1 or 2.
Tree traversal is the process of visiting each node in the tree exactly once. Visiting each node in a
graph should be done in a systematic manner. If search result in a visit to all the vertices, it is called a
traversal. There are basically three traversal techniques for a binary tree that are,
1.Preorder traversal
2.Inorder traversal
3.Postorder traversal
1) Preorder traversal
To traverse a binary tree in preorder, following operations
are carried out:
1.Visit the root.
2.Traverse the left sub tree of root.
3.Traverse the right sub tree of root.
Note: Preorder traversal is also known as NLR traversal.
Practice questions
2) In order traversal
To traverse a binary tree in inorder traversal, following
operations are carried out:
1.Traverse the left most sub tree.
2.Visit the root.
3.Traverse the right most sub tree.
Note: Inorder traversal is also known as LNR traversal.
Practice questions
Therefore the in order traversal of above tree will
be: 0,1,2,3,4,5,6,7,8,9,10
3) Postorder traversal
To traverse a binary tree in postorder traversal, following operations
are carried out:
1.Traverse the left sub tree of root.
2.Traverse the right sub tree of root.
3.Visit the root.
Note: Postorder traversal is also known as LRN traversal.
Practice questions
Therefore the postorder traversal of the above tree will be: 0,2,4,6,5,3,1,8,10,9,7
Link for reference
Preorder, Inorder and Postorder in 5 minute | Tree Traversal | Easiest and Shortest Trick - YouTube
Simplest Binary Tree Traversal trick for preorder inorder postorder - YouTube
Binary Search Trees
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −
•The value of the key of the left sub-tree is less than the value of its parent (root)
node's key.
•The value of the key of the right sub-tree is greater than or equal to the value of its
parent (root) node's key.
SEARCHING, INSERTION AND
DELETION
Searching
Searching means finding or locating some specific element or node
within a data structure.
However, searching for some specific node in binary search tree is
pretty easy due to the fact that, element in BST are stored in a
particular order.
2.If the item is matched then return the location of the node.
Insert function is to be designed in such a way that, it must node violate the property of
binary search tree at each value.
2.Set the data part to the value and set the left and right pointer of tree, point to NULL.
3.If the item to be inserted, will be the first element of the tree, then the left and right of
this node will point to NULL.
4.Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.
5.If this is false, then perform this operation recursively with the right sub-tree of the root.
Insert 12
Deletion
Delete function is used to delete the specified node from a binary search tree.
However, we must delete a node from a binary search tree in such a way, that the
property of binary search tree doesn't violate.
There are three situations of deleting a node from binary search tree.
FIRST:
The node to be deleted is a leaf node
Second: The node to be deleted has only one child.
The node to be deleted has two children.
In the following image, the node 50 is to be deleted which is the root node of the
tree.
Now, 50 will be moved to the leaf of the tree, which will simply be deleted.
1. Which of the following is false about a binary search tree?
a) The left child is always lesser than its parent
b) The right child is always greater than its parent
c) The left and right sub-trees should also be binary search trees
d) In order sequence gives decreasing order of elements
2. What is the speciality about the inorder traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node