unit4 binary tree
TRANSCRIPT
-
8/7/2019 Unit4 Binary Tree
1/34
Trees
Non-Linear Data Structure
-
8/7/2019 Unit4 Binary Tree
2/34
Non-Linear Data Structure
These data structures donot have their
elements in a sequence.
Trees is an example.
Trees are mainly used to represent datacontaining a hierarchical relationshipbetween elements, ex : records, family
trees and table of contents.
-
8/7/2019 Unit4 Binary Tree
3/34
Terminology of Trees The boxes on the tree are called nodes
The nodes immediately below (to the left and right of) a given nodeare called its children
The node immediately above a given node is called its parent
The (unique) node without a parent is called the root
A node with no children is called a leaf Two children of the same parent are said to be siblings
A node can be an ancestor (e.g. grandparent) or a descendant(e.g. great-grandchild)
-
8/7/2019 Unit4 Binary Tree
4/34
Some tree terminology root: node with no parent
A non-empty tree has exactly one root
leaf: node with no children
siblings: two nodes with the same parent.
path: a sequence of nodes n1, n2, , nk such that ni is the parent of
ni+1 for 1 i < k
in other words, a sequence of hops to get from one node to
another
the length of a path is the number of edges in the path, or 1 less
than the number of nodes in it
-
8/7/2019 Unit4 Binary Tree
5/34
Depth and height depth or level: length of the path from root to the current node
(depth of root = 0)
height: length of the longest path from root to any leaf
empty (null) tree's height: -1
single-element tree's height: 0 tree with a root with children: 1
-
8/7/2019 Unit4 Binary Tree
6/34
Tree example
-
8/7/2019 Unit4 Binary Tree
7/34
Trees
Tree nodes contain two or more links
All other data structures we have discussedonly contain one
Binary trees All nodes contain two links None, one, or both of which may be NULL
The root node is the first node in a tree. Each link in the root node refers to a child
A node with no children is called a leaf node
-
8/7/2019 Unit4 Binary Tree
8/34
Binary Trees Special type of tree in which every node or
vertex has either no children, one child ortwo children.
Characteristics :
Every binary tree has a root pointer whichpoints to the start of the tree.
A binary tree can be empty.
It consists of a node called root, a left subtreeand right subtree both of which are binarytrees themselves.
-
8/7/2019 Unit4 Binary Tree
9/34
Examples : Binary Trees
X X X X
Y YZ Z
A
B C
(1) (2) (3) (4)
Root of tree is node having info as X.(1) Only node is root.
(2) Root has left child Y.
(3) Root X has right child Z.
(4) Root X has left child Y and right child Z whichis again a binary tree with its parent as Z andleft child of Z is A, which in turn is parent forleft child B and right child C.
-
8/7/2019 Unit4 Binary Tree
10/34
Properties of Binary Tree A tree with n nodes has exactly (n-1) edges or
branches. In a tree every node except the root has exactly
one parent (and the root node does not have a
parent). There is exactly one path connecting any two
nodes in a tree.
The maximum number of nodes in a binary treeof height K is 2K+1 -1 where K>=0.
-
8/7/2019 Unit4 Binary Tree
11/34
-
8/7/2019 Unit4 Binary Tree
12/34
Representation of Binary Tree Linked List
Every node will consists of information, and twopointers left and right pointing to the left and rightchild nodes.
struct node{
int data;struct node *left;
struct node *right;
};
The topmost node or first node is pointed by a rootpointer which will inform the start of the tree. Rest ofthe nodes are attached either to left if less than parentor right if more or equal to parent.
-
8/7/2019 Unit4 Binary Tree
13/34
Diagram of a binary tree
B
A D
C
-
8/7/2019 Unit4 Binary Tree
14/34
Operations on Binary Tree
Searching an existing node.
Inserting a new node.
Deleting an existing node.
Traversing the tree. Preorder
Inorder
Postorder
-
8/7/2019 Unit4 Binary Tree
15/34
Search Process Initialize a search pointer as the root pointer.
All the data is compared with the data stored in eachnode of the tree starting from root node.
If the data to be searched is equal to the data of thenode then print successful search.
Else if the data is less than nodes data move the pointerto left subtree. Else data is more than nodes data movethe pointer to right subtree.
Keep moving in the tree until the data is found or searchpointer comes to NULL in which case it is anunsuccessful search.
-
8/7/2019 Unit4 Binary Tree
16/34
Example used for Search21
18
197
6 9
8 11
14
13
Root
-
8/7/2019 Unit4 Binary Tree
17/34
Example : Search Initialize temp as root pointer which is node having 21.
Loop until temp!=NULL or ITEM found Compare item=9 with the root 21 of the tree, since
9
-
8/7/2019 Unit4 Binary Tree
18/34
Insert Process For node insertion in a binary search tree, initially the
data that is to be inserted is compared with the data ofthe root data.
If the data is found to be greater than or equal to thedata of the root node then the new node is inserted in
the right subtree of the root node, else left subtree. Now the parent of the right or left subtree is considered
and its data is compared with the data of the new node
and the same procedure is repeated again until a NULLis found which will indicate a space when the new nodehas to be attached. Thus finally the new node is madethe appropriate child of this current node.
-
8/7/2019 Unit4 Binary Tree
19/34
Example used for Insert38
14
238
Root
56
8245
18 70
20
-
8/7/2019 Unit4 Binary Tree
20/34
Example : Insert Suppose the ITEM=20 is the data part of the new node
to be inserted in the tree and the root is pointing to thestart of the tree.
Compare ITEM=20 with the root 38 of the tree. Since 20< 38 proceed to the left child of 38, which is 14.
Compare ITEM=20 with 14, since 20 > 14 proceed to theright child of 14, which is 23.
Compare ITEM=20 with 23 since 20 < 23 proceed to the
left child of 23 which is 18. Compare ITEM=20 with 18 since 20 > 18 and 18 does
not have right child, 10 is inserted as the right child of 18.
-
8/7/2019 Unit4 Binary Tree
21/34
Tree traversals: Inorder traversal prints the node values in ascending order
1. Traverse the left subtree with an inorder traversal
2. Process the value in the node (i.e., print the node value)
3. Traverse the right subtree with an inorder traversal
Preorder traversal
1. Process the value in the node
2. Traverse the left subtree with a preorder traversal
3. Traverse the right subtree with a preorder traversal
Postorder traversal
1. Traverse the left subtree with a postorder traversal
2. Traverse the right subtree with a postorder traversal3. Process the value in the node
-
8/7/2019 Unit4 Binary Tree
22/34
Binary tree traversals three common binary tree traversal orderings
(each one begins at the root):
preorder traversal: the current node is processed, then thenode's left subtree is traversed, then the node's right subtree istraversed (CURRENT-LEFT-RIGHT)
in-order traversal: the node's left subtree is traversed, then thecurrent node itself is processed, then the node's right subtree istraversed (LEFT-CURRENT-RIGHT)
postorder traversal: the node's left subtree is traversed, thenthe node's right subtree is traversed, and lastly the current nodeis processed (LEFT-RIGHT-CURRENT)
-
8/7/2019 Unit4 Binary Tree
23/34
Binary tree preorder traversal order: C F T B R K G
The trick: Walk around the outside of the tree and emit a node'svalue when you touch the left side of the node.
"C"
"G""F"
root
"T"
"R""B"
"K"
-
8/7/2019 Unit4 Binary Tree
24/34
Binary tree in-order traversal order: B T R F K C G
The trick: Walk around the outside of the tree and emit a node'svalue when you touch the bottom side of the node.
"C"
"G""F"
root
"T"
"R""B"
"K"
-
8/7/2019 Unit4 Binary Tree
25/34
Binary tree postorder traversal order: B R T K F G C
The trick: Walk around the outside of the tree and emit a node'svalue when you touch the right side of the node
"C"
"G""F"
root
"T"
"R""B"
"K"
-
8/7/2019 Unit4 Binary Tree
26/34
Delete Process There are four possible conditions are to be taken into account :
i. No node in the tree holds the specified data.
ii. The node containing the data has no children.iii. The node containing the data has exactly one child.
iv. The node containing data has two children.
Condition (i) In this case we simply print the message that the data
item is not present in the tree.
Condition (ii) In this case since the node to be deleted has nochildren the memory occupied by this should be freed and eitherthe left link or the right link of the parent of this node should be set
to NULL. Which of these is to be set to NULL depends uponwhether the node being deleted is a left child or right child of itsparent.
-
8/7/2019 Unit4 Binary Tree
27/34
Condition (iii) In this case since the node to be deleted has one child the solution is to
adjust the pointer of the parent of the node to be deleted such that afterdeletion it points to the child of the node being deleted as in the diagram.
21
18
197
6 9
11
10
Root
24
26
25 30
21
18
197
6 11
10
24
26
25 30
Root
-
8/7/2019 Unit4 Binary Tree
28/34
Condition (iv)In this case the node to be deleted has two children. Consider node 9 before deletion. The inorder successor of thenode 9 is node 10. The data of this inorder successor should now be copied into the node to be deleted and a pointershould be set up pointing to the inorder successor (node 10). This inorder successor would always have one or zerochild. It should then be deleted using the same procedure as for deleting one child or a zero child node. Thus, the wholelogic of deleting a node with two children is to locate the inorder successor, copy its data and reduce the problem to a
simple deletion of a node with one or zero child. This is shown in the example.
21
18
197
6 9
11
10
Root
24
26
25 30
8
21
18
197
6 10
11
Root
24
26
25 30
8
Inorder successor of node 9
-
8/7/2019 Unit4 Binary Tree
29/34
Deletion Condition (i) Print message data not found.
Condition (ii) Since no children so free the node& make either parent->left=NULL or parent->right=NULL.
A A
B C
Parent Parent
left right
X X
In the example1,
parent->left==x so free(x) andmake parent->left=NULL.
In the example2,
parent->right==x so free(x) andmake parent->right=NULL.
-
8/7/2019 Unit4 Binary Tree
30/34
Deletion Condition (iii) Adjust parent to point to the child of the deleted node.
I. Node deleted(X) has only left child :
1) If(parent->left==x) parent->left=x->left.
2) If(parent->right==x) parent->right=x->left.
A A
B
Parent Parent
left right
X XB
C C
left
right
-
8/7/2019 Unit4 Binary Tree
31/34
Deletion Condition (iii) Adjust parent to point to the child of the deleted node.
II. Node deleted(X) has only right child :
1) If(parent->left==x) parent->left=x->right.
2) If(parent->right==x) parent->right=x->right.
A A
B
Parent Parent
left right
X XB
C Cright
right
-
8/7/2019 Unit4 Binary Tree
32/34
Deletion Condition (iv) solution more complex as X has two children, Find inorder successor of the node X. Inorder successor of any node will be first go to right of the
node and then from that node keep going left until NULL encountered, that will be the inordersuccessor.
Copy data of inorder successor to node Xs data. Place pointer at the inorder successor node. Now the inorder succesor will have zero or one child
only (so the complex problem is reduced to condition (iii)). Delete the inorder successor node by using logic of condition (ii) if no children or condition (iiii) if one
right child.
A
B
Parent
left
X
D
rightleft
C
E
left
A
E
Parent
left
X
D
rightleft
C
E
leftHere inorder successor E hasno children condition (ii)
-
8/7/2019 Unit4 Binary Tree
33/34
DeletionA
B
Parent
left
X
D
rightleft
C
E
left
F
right
G H
Inorder
Successor
left right
A
E
Parent
left
X
D
rightleft
C
E
left
F
right
G H
Inorder
Successor
left right
A
E
Parent
left
D
rightleft
C
F
G H
left right
Here inorder successor E hasone right child condition (iii)
If(parent->left==x)
parent->left=x->right.
left
-
8/7/2019 Unit4 Binary Tree
34/34
Application of Tree Expression Tree
Game Tree