23
Data Structures and Algorithms(6) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh Five-Year" national planning textbook) https://courses.edx.org/courses/PekingX/04830050x/2T2014/ Ming Zhang "Data Structures and Algorithms"

Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

Data Structures and Algorithms(6)

Instructor: Ming ZhangTextbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao

Higher Education Press, 2008.6 (the "Eleventh Five-Year" national planning textbook)

https://courses.edx.org/courses/PekingX/04830050x/2T2014/

Ming Zhang "Data Structures and Algorithms"

Page 2: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

2

目录页

Ming Zhang “Data Structures and Algorithms”

Chapter 6 Trees• General Definitions and Terminology

of Tree

• Linked Storage Structure of Tree

• Sequential Storage Structure of Tree

• K-ary Trees

Trees

Chapter 6

JI

FE

G

A

BC

D

H

Page 3: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

3

目录页

Ming Zhang “Data Structures and Algorithms”

• Preorder sequence with right link representation

• Double-tagging preorder sequence representation

• Double-tagging level-order sequence representation

• Postorder sequence with degree representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Sequential Storage Structure of Tree

Page 4: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

4

目录页

Ming Zhang “Data Structures and Algorithms”

Preorder sequence with right link representation• Nodes are stored continuously according to preorder sequence

– info:the data of the node

– rlink:right link

• Point to the next sibling of the node, which iscorresponding to the right child node of the parent nodein the binary tree

– ltag:tag

• If the node has no child node, which means the nodedoesn’t have a left child node in the binary tree, and ltagwill be 1.

• Otherwise, ltag will be 0.

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

rlink info ltag

Page 5: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

5

目录页

Ming Zhang “Data Structures and Algorithms”

Index0 1 2 3 4 5 6 7 8 9

rlink 7 5 3 4 -1 6 -1 -1 9 -1

info C E J K L F G D X I

ltag 0 0 1 1 1 1 1 0 1 1

Preorder sequence with right link representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

F

C

G

D

X I

E

K H J

J

K

D

G

C

E

F

I

X

L

Page 6: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

6

目录页

Ming Zhang “Data Structures and Algorithms”

From a preorder rlink-ltag to a tree

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Index0 1 2 3 4 5 6 7 8 9

rlink 7 5 3 4 -1 6 -1 -1 9 -1

info C E J K L F G D X I

ltag 0 0 1 1 1 1 1 0 1 1

J

K

D

G

C

E

F

I

X

L

F

C

G

D

X I

E

K H J

Page 7: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

7

目录页

Ming Zhang “Data Structures and Algorithms”

Double-tagging preorder sequence representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

In preorder sequence with right link representation, rlink is

still redundant, so we can replace the pointer rlink with a

tag rtag, then it is called “double-tagging preorder sequence

representation”. Each node includes data and 2 tags(ltag

and rtag), the form of the node is like: rtag info ltag

According to the preorder sequence and 2 tags(ltag, rtag), we

can calculate the value of llink and rlink of each node in the

“Left-child/Right-sibling” list. And llink will be the same as

that in preorder sequence with right link representation.

Page 8: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

8

目录页

Ming Zhang “Data Structures and Algorithms”

Double-tagging preorder sequence representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

00 000 111 1 1

info

ltag

H FD EC KA B J G

000 0 111 1 1 1rtag

A

B C

K

F

D

E G

H J G

FH

D

C

A

B

K

E

J

Page 9: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

9

目录页

Ming Zhang “Data Structures and Algorithms”

From a rtag-ltag preorder sequence to a tree

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Index0 1 2 3 4 5 6 7 8 9

rtag 0 0 0 0 1 0 1 1 0 1

info C E J K L F G D X I

ltag 0 0 1 1 1 1 1 0 1 1

G

C

I

L

X

D

K

E

FJ

F

C

G

D

X I

E

K H J

stack C E JKFX

Page 10: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

10

目录页

Ming Zhang “Data Structures and Algorithms”

Rebuild the tree by double-tagging preorder sequence

template<class T>

class DualTagTreeNode { // class of double-tagging preorder sequence node

public:

T info; // data information of the node

int ltag, rtag; // left/right tag

DualTagTreeNode(); // constructor

virtual ~DualTagTreeNode();

};

template <class T>

Tree<T>::Tree(DualTagTreeNode<T> *nodeArray, int count) {

// use double-tagging preorder sequence representation to build “Left-child/Right-sibling” tree

using std::stack; // Use the stack of STL

stack<TreeNode<T>* > aStack;

TreeNode<T> *pointer = new TreeNode<T>; // ready to set up root node

root = pointer;

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Page 11: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

11

目录页

Ming Zhang “Data Structures and Algorithms”

for (int i = 0; i < count-1; i++) { // deal with one node

pointer->setValue(nodeArray[i].info); // assign the value to the node

if (nodeArray[i].rtag == 0) // if rtag equals to 0, push the node into the stack

aStack.push(pointer);

else pointer->setSibling(NULL); // if rtag equals to 1, then right sibling pointer

// should be NULL

TreeNode<T> *temppointer = new TreeNode<T>; // get ready for the next node

if (nodeArray[i].ltag == 0) // if ltag equals to 0, then set the child node

pointer->setChild(temppointer);

else { // if ltag equals to 1

pointer->setChild(NULL); // set child pointer equal to NULL

pointer = aStack.top(); // get the top element of the stack

aStack.pop();

pointer->setSibling(temppointer); } // set a sibling node for the top element of the stack

pointer = temppointer; }

pointer->setValue(nodeArray[count-1].info); // deal with the last node

pointer->setChild(NULL); pointer->setSibling(NULL);

}

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Page 12: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

12

目录页

Ming Zhang “Data Structures and Algorithms”

Double-tagging level-order sequence representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

• Nodes are stored continuously according to level-

order sequence

– Info represents the data of the node.

– ltag is a 1-bit tag, if the node doesn’t have a child node, which means

the node of the corresponding binary tree doesn’t have a left child

node, then ltag equals to 1, otherwise, ltag equals to 0.

– rtag is a 1-bit tag, if the node doesn’t have a right sibling node , which

means the node of the corresponding binary tree doesn’t have a right

child node, then rtag equals to 1, otherwise, rtag equals to 0.

rtag info ltag

Page 13: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

13

目录页

Ming Zhang “Data Structures and Algorithms”

From a double-tagging level-order sequence to a tree

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

JHD

1 000 1 00 0 1

J

IG

E

C

D

F X

H

K

1

info

rtag

I KG XE FC

ltag

F

C

G

D

X I

E

K H J

1 100 1 1101 1

queue C D E

Page 14: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

14

目录页

Ming Zhang “Data Structures and Algorithms”

From a double-tagging level-order sequence to a treetemplate <class T>

Tree<T>::Tree(DualTagWidthTreeNode<T>* nodeArray, int count) {

using std::queue; // use the queue of STL

queue<TreeNode<T>*> aQueue;

TreeNode<T>* pointer=new TreeNode<T>; // build the root node

root=pointer;

for(int i=0;i<count-1;i++) { // deal with each node

pointer->setValue(nodeArray[i].info);

if(nodeArray[i].ltag==0)

aQueue.push(pointer); // push the pointer into the queue

else pointer->setChild(NULL); // set the left child node as NULL

TreeNode<T>* temppointer=new TreeNode<T>;

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

Page 15: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

15

目录页

Ming Zhang “Data Structures and Algorithms”

if(nodeArray[i].rtag == 0)

pointer->setSibling(temppointer);

else {

pointer->setSibling(NULL); // set the right sibling node as NULL

pointer=aQueue.front(); // get the pointer of the first node in the queue

aQueue.pop(); // and pop it out of the queue

pointer->setChild(temppointer);

}

pointer=temppointer;

}

pointer->setValue(nodeArray[count-1].info); // the last node

pointer->setChild(NULL); pointer->setSibling(NULL);

}

Trees

Chapter 6

Page 16: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

16

目录页

Ming Zhang “Data Structures and Algorithms”

Postorder sequence with degree representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

• In postorder sequence with degree

representation, nodes are stored contiguously

according to postorder sequence, whose form

are like:

• info represents the data of the node, and

degree represents the degree of the node

degree info

Page 17: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

17

目录页

Ming Zhang “Data Structures and Algorithms”

Postorder sequence with degree representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

info C XF GJ EK H I D

3 00 00 30 0 0 2degree

F

C

G

D

X I

E

K H J

Page 18: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

18

目录页

Ming Zhang “Data Structures and Algorithms”

Postorder sequence with degree representation

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

info C XF GJ EK H I D

3 00 00 30 0 0 2degree

F

C

G

D

X I

E

K H J

K

H

J

E

F

G

C

X

I

D

Page 19: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

19

目录页

Ming Zhang “Data Structures and Algorithms”

• Preorder sequence of the full tagging binary tree

A’ B’ I C D’ E’ H F’ J G K

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

G

FH

D

C

A

B

E

J

I K

• Preorder sequence of the virtual full tagging binary tree

A’ B’ / C D’ E’ H F’ J / K

FH

D

C

A

B

E

J

K

Page 20: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

20

目录页

Ming Zhang “Data Structures and Algorithms”

Thinking: Sequential Storage of the Forest

Trees

Chapter 6

6.3 Sequential Storage Structure of Tree

• Information redundancy

• Other sequential storage of tree

– Preorder sequence with degree?

– Level-order sequence with degree?

• Sequential storage of the binary tree?

– The binary tree is corresponding to the tree, but their

semantemes are different

• Preorder sequence of the binary tree with right link

• Level-order sequence of the binary tree with left link

Page 21: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

21

目录页

Ming Zhang “Data Structures and Algorithms”

Definition of K-ary tree

Trees

Chapter 6

6.4 K-ary Trees

• K-ary tree T is a finite node set, which can be

defined recursively:

– (a) T is an empty set.

– (b) T consists of a root node and K disjoint K-

ary subtrees.

• Nodes except the root R are devided into K

subsets (T0, T

1, …, T

K–1), and each subset is a K-

ary tree, such that T = {R, T0, T

1, …, T

K-1}.

• Each branch node of K-ary tree has K child nodes.

Page 22: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

22

目录页

Ming Zhang “Data Structures and Algorithms”

Full K-ary trees and complete K-ary trees

Trees

Chapter 6

6.4 K-ary Trees

• The nodes of K-ary tree have K child nodes

• Many properties of binary tree can be generalized to K-ary tree

– Full K-ary trees and complete K-ary trees are similar to full

binary trees and complete binary trees

– Complete K-ary trees can also be storeed in an array

Full 3-ary tree Complete 3-ary tree

Page 23: Data Structures and Algorithms 6...7 目录页 Ming Zhang “Data Structures and Algorithms” Double-tagging preorder sequence representation Trees Chapter 6 6.3 Sequential Storage

Data Structures and Algorithms

Thanks

the National Elaborate Course (Only available for IPs in China)http://www.jpk.pku.edu.cn/pkujpk/course/sjjg/

Ming Zhang, Tengjiao Wang and Haiyan ZhaoHigher Education Press, 2008.6 (awarded as the "Eleventh Five-Year" national planning textbook)

Ming Zhang “Data Structures and Algorithms”