computer notes - data structures - 15
Post on 06-Apr-2018
221 Views
Preview:
TRANSCRIPT
-
8/3/2019 Computer Notes - Data Structures - 15
1/55
Class No.15
Data Structures
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
2/55
Level-order Traversal
There is yet another way of traversing abinary tree that is not related to recursivetraversal procedures discussed previously.
In level-order traversal, we visit the nodesat each level before proceeding to the nextlevel.
At each level, we visit the nodes in a left-to-right order.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
3/55
Level-order Traversal
Level-order: 14 4 15 3 9 18 7 16 20 5 17
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
4/55
Level-order Traversal
How do we do level-order traversal?
Surprisingly, if we use a queue instead ofa stack, we can visit the nodes in level-order.
Here is the code for level-order traversal:
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
5/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
6/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
7/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
8/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
9/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
10/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
11/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
12/55
Level-order Traversalvoid levelorder(TreeNode* treeNode)
{
Queue q;
if( treeNode == NULL ) return;
q.enqueue( treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout getInfo()) getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )q.enqueue( treeNode->getRight());
}
cout
-
8/3/2019 Computer Notes - Data Structures - 15
13/55
Level-order Traversal
Queue: 14
Output:
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
14/55
Level-order Traversal
Queue: 4 15
Output: 14
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
15/55
Level-order Traversal
Queue: 15 3 9
Output: 14 4
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
16/55
Level-order Traversal
Queue: 3 9 18
Output: 14 4 15
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
17/55
Level-order Traversal
Queue: 9 18
Output: 14 4 15 3
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
18/55
Level-order Traversal
Queue: 18 7
Output: 14 4 15 3 9
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
19/55
Level-order Traversal
Queue: 7 16 20
Output: 14 4 15 3 9 18
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
20/55
Level-order Traversal
Queue: 16 20 5
Output: 14 4 15 3 9 18 7
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
21/55
Level-order Traversal
Queue: 20 5 17
Output: 14 4 15 3 9 18 7 16
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
22/55
Level-order Traversal
Queue: 5 17
Output: 14 4 15 3 9 18 7 16 20
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
23/55
Level-order Traversal
Queue: 17
Output: 14 4 15 3 9 18 7 16 20 5
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
24/55
Level-order Traversal
Queue:
Output: 14 4 15 3 9 18 7 16 20 5 17
14
4
9
7
3
5
15
18
16 20
17
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
25/55
Storing other Type of Data
The examples of binary trees so far havebeen storing integer data in the tree node.
This is surely not a requirement. Any typeof data can be stored in a tree node.
Here, for example, is the C++ code tobuild a tree with character strings.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
26/55
Binary Search Tree with Strings
void wordTree()
{
TreeNode* root = new TreeNode();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout
-
8/3/2019 Computer Notes - Data Structures - 15
27/55
Binary Search Tree with Strings
void wordTree()
{
TreeNode* root = new TreeNode();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout
-
8/3/2019 Computer Notes - Data Structures - 15
28/55
Binary Search Tree with Strings
void wordTree()
{
TreeNode* root = new TreeNode();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout
-
8/3/2019 Computer Notes - Data Structures - 15
29/55
Binary Search Tree with Strings
void wordTree()
{
TreeNode* root = new TreeNode();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout
-
8/3/2019 Computer Notes - Data Structures - 15
30/55
Binary Search Tree with Strings
void wordTree()
{
TreeNode* root = new TreeNode();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout
-
8/3/2019 Computer Notes - Data Structures - 15
31/55
Binary Search Tree with Strings
void insert(TreeNode* root, char* info)
{
TreeNode* node = new TreeNode(info);
TreeNode *p, *q;
p = q = root;
while( strcmp(info, p->getInfo()) != 0 && q != NULL )
{
p = q;
if( strcmp(info, p->getInfo()) < 0 )
q = p->getLeft();
else
q = p->getRight();
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
32/55
Binary Search Tree with Strings
void insert(TreeNode* root, char* info)
{
TreeNode* node = new TreeNode(info);
TreeNode *p, *q;
p = q = root;
while( strcmp(info, p->getInfo()) != 0 && q != NULL )
{
p = q;
if( strcmp(info, p->getInfo()) < 0 )
q = p->getLeft();
else
q = p->getRight();
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
33/55
Binary Search Tree with Strings
void insert(TreeNode* root, char* info)
{
TreeNode* node = new TreeNode(info);
TreeNode *p, *q;
p = q = root;
while( strcmp(info, p->getInfo()) != 0 && q != NULL )
{
p = q;
if( strcmp(info, p->getInfo()) < 0 )
q = p->getLeft();
else
q = p->getRight();
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
34/55
Binary Search Tree with Strings
void insert(TreeNode* root, char* info)
{
TreeNode* node = new TreeNode(info);
TreeNode *p, *q;
p = q = root;
while( strcmp(info, p->getInfo()) != 0 && q != NULL )
{
p = q;
if( strcmp(info, p->getInfo()) < 0 )
q = p->getLeft();
else
q = p->getRight();
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
35/55
Binary Search Tree with Strings
void insert(TreeNode* root, char* info)
{
TreeNode* node = new TreeNode(info);
TreeNode *p, *q;
p = q = root;
while( strcmp(info, p->getInfo()) != 0 && q != NULL )
{
p = q;
if( strcmp(info, p->getInfo()) < 0 )
q = p->getLeft();
else
q = p->getRight();
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
36/55
Binary Search Tree with Strings
if( strcmp(info, p->getInfo()) == 0 ){
cout setRight( node );
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
37/55
Binary Search Tree with Strings
if( strcmp(info, p->getInfo()) == 0 ){
cout setRight( node );
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
38/55
Binary Search Tree with Strings
if( strcmp(info, p->getInfo()) == 0 ){
cout setRight( node );
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
39/55
Binary Search Tree with StringsOutput:
abandonabash
accuse
adhere
advise
babble
backup
banditcease
chain
daily
debunk
eagle
economy
fablefeeder
fetch
gain
genius
jacket
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
40/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
41/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
This should not come as a surprise ifyou consider how we built the BST.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
42/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
This should not come as a surprise ifyou consider how we built the BST.
For a given node, values less than theinfo in the node were all in the leftsubtree and values greater or equalwere in the right.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
43/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
This should not come as a surprise ifyou consider how we built the BST.
For a given node, values less than theinfo in the node were all in the leftsubtree and values greater or equalwere in the right.
Inorder prints the left subtree, then thenode finally the right subtree.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
44/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
This should not come as a surprise ifyou consider how we built the BST.
For a given node, values less than theinfo in the node were all in the leftsubtree and values greater or equalwere in the right.
Inorder prints the left subtree, then thenode finally the right subtree.
Building a BST and doing an inordertraversal leads to a sorting algorithm.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
45/55
Binary Search Tree with Stringsabandon
abashaccuse
adhere
advise
babblebackup
banditcease
chain
dailydebunk
eagle
economy
fablefeederfetch
gain
genius
jacket
Notice that the words are sorted inincreasing order when we traversedthe tree in inorder manner.
This should not come as a surprise ifyou consider how we built the BST.
For a given node, values less than theinfo in the node were all in the leftsubtree and values greater or equalwere in the right.
Inorder prints the left subtree, then thenode finally the right subtree.
Building a BST and doing an inordertraversal leads to a sorting algorithm.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
46/55
Deleting a node in BST
As is common with many data structures,the hardest operation is deletion.
Once we have found the node to bedeleted, we need to consider severalpossibilities.
If the node is a leaf, it can be deleted
immediately.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
47/55
Deleting a node in BST
If the node has one child, the node can bedeleted after its parent adjusts a pointer tobypass the node and connect to inordersuccessor.
6
2
4
3
1
8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
48/55
Deleting a node in BST
The inorder traversal order has to bemaintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
49/55
Deleting a node in BST
The inorder traversal order has to bemaintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
6
2
31
8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
50/55
Deleting a node in BST
The complicated case is when the node tobe deleted has both left and right subtrees.
The strategy is to replace the data of this
node with the smallest data of the rightsubtree and recursively delete that node.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
51/55
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4Inorder
successor
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
52/55
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4Inorder
successor
Inorder successor will be the left-mostnode in the right subtree of 2.
The inorder successor will not have a leftchild because if it did, that child would bethe left-most node.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
53/55
Deleting a node in BST
Delete(2): copy data from inorder successor
6
2
5
3
1
8
4
63
5
3
1
8
4
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
54/55
Deleting a node in BST
Delete(2): remove the inorder successor
6
2
5
3
1
8
4
63
5
3
1
8
4
63
5
3
1
8
4
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 15
55/55
Deleting a node in BST
Delete(2)
63
5
4
1
8
63
5
3
1
8
4
http://ecomputernotes.com/
top related