Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Algorithm:-
1. First initialize the stack array to null and create a binary tree
2. Push the node to stack and find the left child and push it on to stack until left child is NULL
3. If the left child is NULL, pop the top element from stack.
4. Print the node and find the right child and go to step2
5. If the right child is NULL, pop the element from stack and go to step4
6. If both the right child and stack is NULL, then tree is traversal completely in in-order.
2 3
4 5
* push1 1
Push 2 1, 2
Push 4 1, 2, 4
Pop 1, 2 4
Pop 1 4, 2
Push 5 1, 5 4, 2
Pop 1 4, 2, 5
Pop --- 4, 2, 5, 1
Push 3 3 4, 2, 5, 1
Pop --- 4, 2, 5, 1, 3
Since both the right child and stack is empty this represents the end of in-order traversal using
stack.
Algorithm:-
4. If the node is not NULL, print the node and push the right child and left child on to Stack [s]
2 3
4 5
* Push 1 1
* Pop the node and is not NULL, so move the node to preorder and push the right child and left
child to Stack [s] [node (1) is not NULL]
3, 2 1
* Pop the node and is not NULL, so move the node to preorder and push the right child and left
child to Stack [s] [node (2) is not NULL]
3, 5, 4 1, 2
* Pop the node and is not NULL, so move the node to preorder and push the right child and left
child to Stack [s] [node (4) is not NULL]
3, 5, NULL, NULL 1, 2, 4
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
3, 5, NULL 1, 2, 4
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
3, 5 1, 2, 4
* Pop the node and is not NULL, so move the node to preorder and push the right child and left
child to Stack [s] [node (5) is not NULL]
3, NULL, NULL 1, 2, 4, 5
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
3, NULL 1, 2, 4,
5
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
3 1, 2, 4, 5
* Pop the node and is not NULL, so move the node to preorder and push the right child and left
child to Stack [s] [node (3) is not NULL]
NULL, NULL 1, 2, 4, 5, 3
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
NULL 1, 2, 4, 5, 3
* Pop the node and is equal to NULL, so move to step3 [node is NULL]
--- 1, 2, 4, 5, 3
* Pop the node since Stack is Empty. Thus the preorder traversal using stacks.
Algorithm: -
1. First create a binary tree and initialize the Stack1 and Stack2 to NULL
6. Then pop the elements from the Stack2 to output until Stack2 is empty
Example: - consider the following example to traverse the binary tree in post-order traversal.
2 3
4 5
* Pop from Stack1 and node is not NULL, so push the node to Stack2 and push the left child and
right child to Stack1 [node (1) is not NULL]
2, 3 1
* Pop from Stack1 and node is not NULL, so push the node to Stack2 and push the left child and
right child to Stack1 [node (3) is not NULL]
2, NULL, NULL 1, 3
2, NULL 1, 3
2 1, 3
* Pop from Stack1 and node is not NULL, so push the node to Stack2 and push the left child and
right child to Stack1 [node (2) is not NULL]
4, 5 1, 3, 2
* Pop from Stack1 and node is not NULL, so push the node to Stack2 and push the left child and
right child to Stack1 [node (5) is not NULL]
4, NULL, NULL 1, 3, 2, 5
4, NULL 1, 3, 2, 5
4 1, 3, 2, 5
* Pop from Stack1 and node is not NULL, so push the node to Stack2 and push the left child and
right child to Stack1 [node (4) is not NULL]
NULL, NULL 1, 3, 2, 5, 4
NULL 1, 3, 2, 5, 4
---- 1, 3, 2, 5, 4
Since the Stack1 is empty, pop the element from Stack2 to Output. 4, 5, 2, 3, 1.
The binary trees with n nodes, 2n pointers are required of which (n+1) are NULL pointers. To
utilize these empty pointers, a new concept called threads is introduced.
Threads are also links or pointers but replace NULL pointer by pointing to some useful
information in a binary tree.
To utilize, i.e. to avoid, NULL pointers in a binary tree two types of threads are used lthread i.e.
left thread and rthread i.e. right thread.
1. If the node left child is NULL, then it is replaced with inorder predecessor of node during
inorder traversal of binary tree.
2. If the node right child is NULL, then it is replaced with inorder successor of node during
inorder traversal of binary tree.
Inorder to distinguish left child and inorder predecessor, left thread is used as follows:
Inorder to distinguish rightchild and inorder successor, right thread is used as follows:
struct TBT
{
struct TBT *lchild;
int lthread;
int element;
int rthread;
struct TBT*rchild;
};
A
DS NEC, CSE Dept. Page 7
B C
F
UNIT VI Advanced Concepts of Trees
D E
Fig: Binary Tree
G
B C
NULL
F D E
NULL
G
Fig: Threaded BT
The left child of node G and the right child of node E are NULL due to absence of an inorder
predecessor and inorder successor respectively.
B 0 A 0 C
Lchild Lthread Element Rthread Rchild
F 0 B 1 A
G 0 F 1 B
\0 0 G 1 F
D 0 C 0 E
A 1 D 1 C
C 1 E 0 \0
The above example shows the thread binary trees during inorder traversal of binary tree:
BST is created inorder to access the elements faster. BST is created using the following
procedure: When a number is compared with the contents of a root node in a tree,
1. A left branch is taken if the number is smaller than the contents of root node.
2. A right branch is taken if greater (or) equal to the contents of root node.
20 17 16 8 10 7 18 13 12 5
1
7
6 1
8
5 8
1
7
0
1
3
Fig: BST
1
2
The binary tree of above type has the property that all the elements in the left-subtree of a root
node N are less than the content of N and all the elements in the right sub-tree of root node N are
greater than (or) equal to the contents of N.
A binary tree that has these properties is called a binary search tree. If a binary search tree is
traversed in in-order then the contents of each node are printed in ascending order.
Operations on a BST: -
To search any node in a binary tree, initially the data i.e. to be searched is compared with the data
DS NEC, CSE Dept. Page 9
UNIT VI Advanced Concepts of Trees
o If the data is equal to the data of the root node then the searching is successful.
o If the data is found to be greater than the data of the root node then the searching process
proceeds in the right sub-tree of the root node.
Otherwise,
The above procedure is repeated for the left (or) right sub-tree until the data is found. While
searching the data, if the leaf node of tree is reached and the data is not found then it is
concluded that the data is not present in the tree.
To insert any node into a BST, initially the data i.e. to be inserted is compared with the data of
the root node. If the data is found to greater than (or) equal to the data of the root node then the
new node is inserted in the right sub-tree of the root node; otherwise the new node is inserted in
the left sub-tree of the root node.
Now the root node of the right (or) left sub-tree is taken and its data is compared with the data
that is to be inserted and the same procedure is repeated. This is done till the left (or) the right
sub-tree where the new node is to be inserted is found to be empty. Finally, the new node is made
as appropriate child of some node.
Before deleting the element in binary tree, we need to search for the element whether it is part of
binary tree (or) not
Case (1): In this case since the node to be deleted has no children, the memory used by this node
should be freed and either the left link (or) the right link of the parent node should be set to
NULL.
Case (2): In this case since the node to be deleted has one child we have to adjust the pointer of
the node to be deleted, such that after deletion the parent node must point to the child of the node
being deleted.
Case (3): In this case since the node to be deleted has two children the solution is tricky we need
to find the inorder successor of node to be deleted and the data of this in-order successor should
now be copied into the node to be deleted and a pointer is setup pointing to the in-order
successor. The inorder successor should then be deleted using the same procedure as for deleting
a one child (or) a zero child node. Thus, the whole logic of deleting a node with two children is
to locate the in-order successor, copy its data and reduce the problem to a simple deletion of a
node with one (or) zero child.
Example: -
2 2
0 0
1 2 1 2
7 3 7 3
6 1 2 6 1 2
8 5 8 5
5 8 5 8 2 2
2 2
4 9 4 9
1 7 1
0 0
9 9
2 2
0 0
1 2 1 2
7 3 7 3
6 1 2 6 1 2
8 5 8 5
5 8 2 2 5 8 2 2
DS 4 NEC,
9 CSE Dept. 4 Page 911
1 9
0
9
UNIT VI Advanced Concepts of Trees
2 2
0 0
2 1 2
1
3 8 3
7
6 1 2 6 2
8 9 9
5 8 2 5 8 2
4 4
9
9
6 2
4
5 8
A balanced binary tree is a rooted tree where each subtree of the root
has equal number of nodes.
Simple binary trees are not perfect. If we insert a data into already ordered
data into a simple binary tree, you get a degenerate tree. All the insertions
go to the right; all the left pointers are NULL. So, the complexity increases
from O (logN) to O (N).
One solution to this problem is to use a balanced binary tree. This involves
a little more work, and a more complicated insertion algorithm. That is
balanced tree.
Height balanced binary tree Def.: A binary tree is balanced if for every
node in the tree has a balance factor of -1, 0 and 1.
Each node in a binary tree has a balance factor, which is equal to the height
of the left subtree minus the height of the right subtree. A binary tree is
balanced if every balance factor is of 0, 1, (or) -1.
Height of a node is number of steps to leaf node from the node. The height
of the tree is the height of its root node.
2 2
3 3
2 9 2
9
4 4
1 6 1 2
6 2
8 8 9
9
5 8 5 8
Figure (i) Height balanced Binary Tree Figure (ii) Not a Height
balanced Binary Tree
2. Operations that run in time proportional to the height of the tree are
O (logN), N the number of nodes with limited performance
variance.
1) AVL-trees
2) Red-black trees
3) 2-3 tree
Red-black tree: - These are standard binary trees that satisfy the following
conditions:
2. All the non-leaf nodes (except the root) have between m/2 and m
children.