+ All Categories
Home > Documents > Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order =...

Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order =...

Date post: 16-Mar-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
33
INE2011 Data Structures Data Structures Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 7 (INE2011) 1
Transcript
Page 1: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Data Structures

Electronics and Communication EngineeringHanyang University

Haewoon Nam

Lecture 7

(INE2011)

1

Page 2: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Traversal

• Many binary tree operations are done by performing a traversal of the binary tree.

• In a traversal, each element of the binary tree is visited exactly once.

• Binary tree traversal methods– Pre-order– In-order– Post-order– Level order

2

Page 3: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Traversal

• Traversing each node and subtree– L : moving left– V : visit– R : moving right

• Six possible combinations of traversal– LVR, LRV, VLR, VRL, RVL, RLV

• Traverse left before right– VLR : pre-order– LVR : in-order– LRV : post-order

3

Page 4: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

a b c

Binary Tree Traversal

• Pre-order example

a

b c

a

b

d e

c

f

jg h i

a b d g h e i c f j

4

Page 5: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

b a c

Binary Tree Traversal

• In-order example

a

b c

a

b

d e

c

f

jg h i

g d h b e i a f j c

5

Page 6: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

b c a

Binary Tree Traversal

• Post-order example

a

b c

a

b

d e

c

f

jg h i

g h d i e b j f c a

6

Page 7: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Traversal

• Pre-order, in-order, post-order

void PreOrder(TreeNode *t){

if (t != NULL){

Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild);

}}

void InOrder(TreeNode *t){

if (t != NULL){

InOrder(t->leftChild); Visit(t);InOrder(t->rightChild);

}}

void PostOrder(TreeNode *t){

if (t != NULL){

PostOrder(t->leftChild); PostOrder(t->rightChild);Visit(t);

}}

7

Page 8: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

a b c

Binary Tree Traversal

• Level order example

a

b c

a

b

d e

c

f

jg h i

a b c d e f g h i j

8

Page 9: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Threaded Binary Trees

• Threads– Many null pointers in a binary tree– Replace null pointers with the in-order predecessor or the in-

order successor

a

b

d e

c

f

jg h i

g d h b e i a f j c

g h i j

d e f

b c

a

9

Page 10: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Threaded Binary Trees

• Class for threaded node

g d h b e i a f j c

g h i j

d e f

b c

aclass treenode{private:

int data;treenode *left;bool leftThread;treenode *right;bool rightThread;

};

left child

data

right childleftThread

rightThread

- If leftThread is true, *left containsa thread, otherwise a pointer.

- Same for rightThread.

10

Page 11: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

• Can you construct the binary tree from a given traversal sequence?– When a traversal sequence has more than one element, the

binary tree is not uniquely defined– Therefore, the tree from which the sequence was obtained

cannot be reconstructed uniquely

• Can you construct the binary tree, given two traversal sequences?

a

b

a bPre-order = a

b

11

Page 12: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1

• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.

a

g d h b e i f j c

12

Page 13: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

– Step 2• b is the next root of the tree. gdh are in the left subtree. ei are in the right

subtree.

a

g d h

f j cb

e i

13

Page 14: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

– Step 3• d is the next root of the tree. g is in the left subtree. h is in the right subtree.

a

f j cb

d

g h

e i

14

Page 15: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

– Step 4• e is the next root of the tree. i is in the right subtree.

a

g

f j cb

h

d

i

e

15

Page 16: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

– Step 5• c is the next root of the tree. fj is in the left subtree.

a

g

b

h

d

i

e

c

f j

16

Page 17: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

Pre-order = g d h b e i a f j cIn-order =

a b d g h e i c f j

– Step 6• f is the next root of the tree. j is in the right subtree.

a

g

b

h

d

i

e

c

j

f

17

Page 18: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1

• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.

– Step 2 – 6• Similar to the previous example with pre-order and in-order

a

g d h b e i f j c

Post-order = g d h b e i a f j cIn-order =

g h d i e b j f c a

18

Page 19: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Tree Construction

• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1

• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.

– Step 2 – 6• Similar to the previous example with pre-order and in-order

a

g d h b e i f j c

Level order = g d h b e i a f j cIn-order =

a b c d e f g h i j

19

Page 20: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Joining and Splitting Binary Tree

• Joining(Grafting) and Splitting(Pruning)

a

b

e

c

f j

a

b

e

c

f js

p q

s

p q

Grafting

Pruning

Page 21: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Dictionary operations– IsEmpty( )– Get(key)– Insert(key, value)– Delete(key)

• Treenode– Each node has a (key, value) pair

21

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9class treenode{private:

int key;char value;treenode *left;treenode *right;

};

Page 22: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Ascend( )– Do an in-order traversal

g d h b e i a f j c

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

22

Page 23: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Get(key)– Search the node with the key (input)– Return the value of the node

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

Get(12) returns e

23

Page 24: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Insert(key, value)– Insert a node at a proper position based on the key (input)

Insert(23, k)

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

k23

24

Page 25: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– No element with the delete key– Element is in a leaf– Element is in a degree 1 node– Element is in a degree 2 node

25

Page 26: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a leaf

Delete(9)

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

26

Page 27: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 1 node

Delete(20)

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

27

Page 28: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 1 node

Delete(12)

a

b

d e

c

f

jg h i

15

10

8 12

20

17

19135 9

28

Page 29: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 2 node– Example 1

Delete(10)

a

b

d e

c

f

jg h i

15

10

7 12

20

17

19135 9Find the largest key in the left subtree

m8

Or the smallest key in the right subtree

29

Page 30: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 2 node– Example 1

Delete(10)

a

b

d e

c

f

jg h i

15

10

7 12

20

17

19135 9Find the largest key in the left subtree

m8

Or the smallest key in the right subtree

30

Page 31: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 2 node– Example 1

Delete(10)

a

h

d e

c

f

jg i

15

9

7 12

20

17

19135m

8

31

Page 32: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 2 node– Example 2

Delete(15)

a

b

d e

c

f

jg h i

15

10

7 12

20

17

19135 9

m8

32

Page 33: Data Structurescontents.kocw.net/KOCW/document/2015/hanyang_erica/... · 2016-09-09 · In-order = g d h b e i a f j c a b d g h e i c f j • Scan the pre-order left to right using

INE2011 Data Structures

Binary Search Tree

• Delete(key)– Element is in a degree 2 node– Example 2

Delete(15)

a

b

d e

c

f

jg h i

15

10

7 12

20

17

19135 9

m8

Find the largest key in the left subtree

33


Recommended