Sei sulla pagina 1di 15

UNIT VI Advanced Concepts of Trees

Advanced Concepts of Trees

Tree Traversals Using Stack:-

In-order tree traversal using stack:-

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.

Example: - consider the following binary tree

2 3

4 5

Stack s is initialized to NULL

=>operation Stack[s] In-order

* push1 1

* find left child of 1

Push 2 1, 2

* find left child of 2

Push 4 1, 2, 4

* find left child of 4

NULL, so pop element from Stack [s] to In-order

=>operation Stack[s] In-order

DS NEC, CSE Dept. Page 1


UNIT VI Advanced Concepts of Trees

Pop 1, 2 4

* find right child of 4

NULL, so pop element from Stack [s] to In-order

Pop 1 4, 2

* find right child of 2

Push 5 1, 5 4, 2

* find left child of 5

NULL, so pop element from Stack[s]

Pop 1 4, 2, 5

* find right child of 5

NULL, so pop element from Stack[s] to In-order

Pop --- 4, 2, 5, 1

* find right child of 1

Push 3 3 4, 2, 5, 1

* find left child of 3

NULL, so pop element from Stack [s] to In-order

Pop --- 4, 2, 5, 1, 3

* find right child of 3

NULL, so pop element from Stack [s] to In-order

Pop Stack is empty 4, 2, 5, 1, 3

Since both the right child and stack is empty this represents the end of in-order traversal using
stack.

Therefore, the in-order traversal for the given Binary Tree is 4, 2, 5, 1, 3.

Pre-order traversal using stacks: -

DS NEC, CSE Dept. Page 2


UNIT VI Advanced Concepts of Trees

Algorithm:-

1. First create a binary tree and initialize Stack [s] to NULL

2. Push the node to Stack [s]

3. Pop the element from Stack [s]

4. If the node is not NULL, print the node and push the right child and left child on to Stack [s]

5. Repeat step3 and step4 until Stack [s] is empty.

This represents that the tree is completely traversal in pre-order traversal

Example: - consider an example to traverse the binary tree in pre-order traversal

2 3

4 5

=>operation stack[s] Pre-order

* Initialize Stack [s] to NULL

* 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

=>operation stack[s] Pre-order

DS NEC, CSE Dept. Page 3


UNIT VI Advanced Concepts of Trees

* 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.

The Pre-order traversal for the above Binary Tree is: 1, 2, 4, 5, 3

Post-order traversal using stacks: -


DS NEC, CSE Dept. Page 4
UNIT VI Advanced Concepts of Trees

Algorithm: -

1. First create a binary tree and initialize the Stack1 and Stack2 to NULL

2. Push the node to Stack1

3. Pop the node from the Stack1

4. If the node is not NULL, then

(a) Push it on to Stack2

(b) Push the left child to Stack1

(c) Push the right child to Stack1

5. Repeat step3 and 4 until Stack1 is empty

6. Then pop the elements from the Stack2 to output until Stack2 is empty

Thus pre-order traversal of binary tree using stacks.

Example: - consider the following example to traverse the binary tree in post-order traversal.

2 3

4 5

=>operation Stack1 [s] Stack2 [s]

* Push 1 to Stack1 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 (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

=>operation Stack1 [s] Stack2 [s]

DS NEC, CSE Dept. Page 5


UNIT VI Advanced Concepts of Trees

* Pop from Stack1 and node is NULL, so move to step3.

2, NULL 1, 3

* Pop from Stack1 and node is NULL, so move to step3.

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

* Pop from Stack1 and node is NULL, so move to step3.

4, NULL 1, 3, 2, 5

* Pop from Stack1 and node is NULL, so move to step3.

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

* Pop from Stack1 and node is NULL, so move to step3.

NULL 1, 3, 2, 5, 4

* Pop from Stack1 and node is NULL, so move to step3.

---- 1, 3, 2, 5, 4

Since the Stack1 is empty, pop the element from Stack2 to Output. 4, 5, 2, 3, 1.

This represents the elements in post-order traversal of BT using Stacks.

Threaded Binary Trees: -

DS NEC, CSE Dept. Page 6


UNIT VI Advanced Concepts of Trees

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:

If the left thread is 1 then it is inorder predecessor of node.

If the left thread is 0 then it is left child of node.

Inorder to distinguish rightchild and inorder successor, right thread is used as follows:

If the right thread is 1 then it is inorder successor of node.

If the right thread is 0 then it is right child of node.

Linked List Representation of Threaded Binary Tree:-

struct TBT
{
struct TBT *lchild;
int lthread;
int element;
int rthread;
struct TBT*rchild;
};

Consider the following example:-

A
DS NEC, CSE Dept. Page 7
B C

F
UNIT VI Advanced Concepts of Trees

D E
Fig: Binary Tree
G

Inorder traversal for the above Binary Tree is G, F, B, A, D, C, E.

The following is Threaded BT of above BT:

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:

1. If the lthread is 0 then it is leftchild of element

DS NEC, CSE Dept. Page 8


UNIT VI Advanced Concepts of Trees

2. If the lthread is 1 then it is inorder predecessor of element

3. If the rthread is 0 then it is rightchild of element

4. If the rthread is 1 then it is inorder successor of element

Binary Search Trees: -

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.

If the input list is as follows:

20 17 16 8 10 7 18 13 12 5

Then the binary tree shown in following figure will result:


2
0

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: -

1. Searching of a node in 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

of the root node.

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,

o Searching process proceeds in the left subtree of the root node.

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.

2. Insertion of a node in a BST:-

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.

3. Deletion from a binary tree: -

The following are four possible cases that we need to consider

1. The node containing the data has no children

2. The node containing the data has exactly one child

3. The node containing the data has two children

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

DS NEC, CSE Dept. Page 10


UNIT VI Advanced Concepts of Trees

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

Initial BST After Insertion of 7 [to 8 left]


Figure (i) Figure (ii)

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

After deletion of 7 [parent-.>right=NULL] After deletion of 10 [parent-.>right=cur->left]


Figure (iii) Figure (iv)

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

After deletion of 25 [since two child,


find inorder successor [29] and replace After deletion of 17 [since two child,
25 with 29. Delete inorder successor find inorder successor [18] and replace
[29]. Parent->right=NULL] Figure (v) 17 with
2 18. Delete inorder successor
3
[18]. Parent->right=NULL Figure (vi)
1 2
8 9

6 2
4

5 8

After deletion of 20 [since two child, find inorder successor[23] and

replace 20 with 23. Delete inorder successor [23]. Parent->right=cur-


>right.
Figure (vii)
Balanced Binary Tree: -

A balanced binary tree is a rooted tree where each subtree of the root
has equal number of nodes.

DS NEC, CSE Dept. Page 12


UNIT VI Advanced Concepts of Trees

Note: Complete binary tree is a height balanced binary tree.

Need of Balanced Binary Tree: -

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

Advantages of height balanced binary trees: -

DS NEC, CSE Dept. Page 13


UNIT VI Advanced Concepts of Trees

1. Height balanced binary tree are balanced.

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.

Applications of balanced binary trees: -

1. *B-trees are a category of tree designed to work well on magnetic


tapes and other direct access secondary storage devise.

Types of the balanced binary trees: -

Some of the more common types are

1) AVL-trees

2) Red-black trees

3) 2-3 tree

AVL-Tree: - These were invented by Adleson-Velskii and Landis in 1962


(hence the name AVL)

Red-black tree: - These are standard binary trees that satisfy the following
conditions:

1. Every node is either red or black.

2. Every external node (NULL) is black.

3. No red node can have a red parent.

4. Every simple path from a node to an external contains the same


number of black nodes.

B-tree: - A B-tree of order m is a tree exhibiting the following properties

1. The root is either a leaf or has between 2 and m children.

2. All the non-leaf nodes (except the root) have between m/2 and m
children.

3. All leafs are at the same depth.

DS NEC, CSE Dept. Page 14


UNIT VI Advanced Concepts of Trees

B-Tree of order 3 is known as 2-3 trees.

Bo tree of order 4 is known as 2-3-4 trees.

DS NEC, CSE Dept. Page 15

Potrebbero piacerti anche