Ds unit 5
Ds unit 5
A tree is a nonlinear hierarchical data structure . When any tree has at most two child , those tree is said
It consists of nodes connected by edges. to be binary tree.
In which , easy to navigate and search.
Terminologies of Tree
Node
A node is an entity that contains a key or value and
pointers to its child nodes.
Edge
It is the link between any two nodes.
Root Complete binary tree
It is the topmost node of a tree. [10] All levels are filled, except possibly the last.
Child node: Nodes are filled from left to right on all levels.
Any subnode of a given node is called a child node.
Ex: 20 & 30 & 50 are children of 10 etc.
Parent:
If node contains any sub-node, then node is called
parent of that sub-node.
Ex: 30 is parent of 60 and 40 is parent of 70 etc.
Sibling:
The nodes that have the same parent are known as
siblings.
Leaf Node:-
The node of the tree, which doesn't have any child
node Extended binary tree
Leaf nodes can also be called external nodes. A binary tree T is said to be 2-tree or extended binary
Ex : 70 ,80, 90 , 25,20, 50 tree if each node
Internal nodes: has either 0 or 2 children.
A node has atleast one child node . b. Nodes with 2 children are called internal nodes and
Ex: 10 , 30 , 40 , 60 nodes with 0 children
are called external nodes.
Height of a Node
The height of a node is the number of edges from
the node to the deepest leaf
Ex:height(30) =2 &height(20) = 0 &height(10) =3
Depth of a Node
The depth of a node is the number of edges from
the root to the node.
Ex : depth(30) =1 & depth(80) = 3 depth(10) =0
Height of a Tree Binary tree
The height of a Tree is the height of the root node . extended binary tree
Ex: Height(root=10) = 3
Representation of Binary Tree using Tree traversal is the process of visiting and processing
each node in a tree data structure.
linked list The three main types of tree traversal are:
Node structure : In-order: 2 7 5 6 11 1 5 9 9
Each node in tree is represented by an object . Pre-order: 1 7 2 6 5 11 9 9 5
Post-order: 2 5 11 6 7 5 9 9 1
In-order traversal: ( L N R)
Algorithm :
1. Traverse the left sub-tree.
2. Visit the root node.
Data : the value stored in node . 3. Traverse the right sub-tree.
Left child: pointer of left subtree of that node
Right child : pointer of right subtree of that node Pseudo-code :
1.void in-order(struct node *root)
2. {
3. if(root!= NULL)
4. {
5. in-order(root→ left);
6. printf("%d",root→ data);
7. in-order(tree→ right);
8. }
9. }
Pre-order traversal: (N L R)
Algorithm :
Representation of Binary Tree using 1.Visit the root node.
2.Traverse the left subtree.
array 3.Traverse the right subtree.
Root Node : index 0 of the array
Parent-child relationship : for any element at index I, its Pseudo-code :
left child is at index 2* I +1 and its right child is at index 1.void in-order(struct node *root)
2*1 +2 2. {
3. if(root!= NULL)
4. {
printf("%d",root→ data);
5. in-order(root→ left);
6.
7. in-order(tree→ right);
8. }
9. }
Post-order traversal: ( L R N )
Algorithm :
8. }
9. }
Draw a binary tree with following traversals :
In-order : B C A E G D H F I J
Pre-order : A B C D E F G H I J
Find the post-order of the tree.
Insertion in BST:
1.if root== null, create BST node with key and return the
node pointer.
2.If root.key > key , recursively insert the new node to the
left subtree.
3.If root.key < key , recursively insert the new node to the
right subtree.
Postorder of tree : C B G E H J I F D A
algorithm
AVL TREE