21
3/13/13 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms and Data Structures Colorado State University Outline Introduction Terminology Binary Tree Basic operations Traversals of a Binary Tree Representations of a Binary Tree – Implementations Binary Search Tree – Algorithms – Implementations – Efficiency – TreeSort General Tree 2 CS200 Algorithms and Data Structures Colorado State University 3/13/13 Samgmi Lee Pallickara 3 A B C D G A B C D G Tree grows top to bo/om! Only one parent! (except for the root node) CS200 Algorithms and Data Structures Colorado State University 3/13/13 Samgmi Lee Pallickara 4 CS200 Algorithms and Data Structures Colorado State University 3/13/13 Samgmi Lee Pallickara 5 CS200 Algorithms and Data Structures Colorado State University 3/13/13 Samgmi Lee Pallickara 6

CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

Embed Size (px)

Citation preview

Page 1: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

1  

CS200 Algorithms and Data Structures Colorado State University

Part 6. Trees (1)

CS 200 Algorithms and Data Structures

1

CS200 Algorithms and Data Structures Colorado State University

Outline

•  Introduction •  Terminology •  Binary Tree

–  Basic operations –  Traversals of a Binary Tree –  Representations of a Binary Tree –  Implementations

•  Binary Search Tree –  Algorithms –  Implementations –  Efficiency –  TreeSort

•  General Tree

2

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 3

A  

B   C   D  

G  

A  

B  C  D  

G  

Tree  grows  top  to  bo/om!  

Only  one  parent!    (except  for  the  root  node)  

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 4

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 5

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 6

Page 2: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

2  

CS200 Algorithms and Data Structures Colorado State University

Data Representation <?xml version="1.0" encoding="ISO-8859-1"?>! <note>  !!<to>Sangmi Pallickara</to>  !

<from>Amazon.com</from>  ! <heading>Message</heading>  ! <body>Your order has been shipped. </body>!</note> !!

7

 Root  Element  

Note  

to   from   heading   body  

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 8

Ques%on  :  Can  we  model  this  with    a  Tree  Data  Structure?  A.  Yes              B.  No  

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 9

Ques%on  :  Can  we  model  this  with    a  Tree  Data  Structure?  

CS200 Algorithms and Data Structures Colorado State University

3/13/13 Samgmi Lee Pallickara 10

CS200 Algorithms and Data Structures Colorado State University

ESnet4 NetworkDrawn by Mark Redman

Revised:Revised by Mark Redman

!"#$%&'(%#$%)*+%,-.%/0$#12$$%3$2%-1456%,*%7*8%92:#$8+#;0826%<*+%=*(#2$%=*18'=8%#1)*>2$6128

US R&EInternationalR & E Peer ing

CommercialCommercial Peer ingInternational Peer ing

O C12-L ink(622Mb)N L R

10 G E C ircuit

Internet 2

10G E-SDN-C ircuit

Internet2

10G E-IP-C ircuit

1G E-L ink

SDN 10G E-L ink

IP

MAN SITE

M A N 10 G E RIN G

SITE O C48-L ink(2.48Gb) O C3-L ink(155Mb) F E-L ink(100Mb) DS3-L ink(45Mb) T1-L ink(1.45Mb)Ether-L ink(10Mb)R & E and International

10G Peer ing ConnectionsR & E and International

1G Peer ing ConnectionsPerfSonar enabled

perfSONAR2/28/2011

CANADA(CANARIE)

CENIC/DREN/INTERNET2/

LOS NETTOS/NASA/NLR/UW

ASIA-PACIFIC(KAREN/KREONET2/NUS-GP/ODN/REANNZ/SINET/TANET2/

TRANSPAC2)

AUSTRALIA(AARnet)

CENIC/INTERNET2/LOS NETTOS/NASA

ASIA-PACIFIC(KAREN/KREONET2/NUS-GP/

ODN/REANNZ/SINET/TRANSPAC2)

AUSTRALIA(AARNET)

LATIN AMERICA(AMPATH/CLARA)

EUROPE(GEANT)

MAN LANINTERNET2 / NLR /

NYSERNET

ASIA-PACIFIC(BINP/HEPNET)

CERNUSLHCNet

FRANCE(OPEN TRANSIT)

CANADA(CANARIE)

RUSSIA & CHINA(GLORIAD)

ASIA-PACIFIC(ASGC/KREONET2/TANET2)

CICNET-OMNIPOP/DREN/INTERNET2/MREN/NISN/NLR/NWU/STARLIGHT/UCHICAGO/

UIOWA/UWMADISON/WISCNET

SLA

C

SNLLLLNL

LBNL

JGI

NERSC

ANL

FNAL

SOX

EM

FRGP

ABQ-GIGAPOPOSOGRANDE

TECHNET

MERIT/ULTRALIGHT

CERNUSLHCNet

CHINA(GLORIAD)

MAXGIGAPOP

NASA / NISNUS-DOI

3ROX/PSC

CANADA(CANARIE)

NOX

ASIA-PACIFIC(SINET)

INDIANA-GIGAPOPUI-ICCN

DREN/INTERNET2/

NASA

ORO

BJC

SRELSRS

LANL

KIR

TLA

ND

-AFB

DO

E-A

BLQ

INL

Y12

AMES

PU-HEP

GFDL

PPPL

NREL

IARC

GA

PAIX-PA

LIGO

SANFORD

ONENET

LATIN AMERICA(CLARA / CUDI)

LATIN AMERICA(CLARA / CUDI)

INTERNET2

SDSC

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

perfSONAR

SNV-MR2

SDN1KANS

ATLA-CR1

CLEV-CR1

SDN1CLEV

BOST-CR1

SDN2SDSC

AOFA-CR2

ELPA-CR1

PNWG

PNNL

SDN1PNWG

SDN1ALBU

SDN1DENV

SDN1ATLA

SDN2CHIC

MIT

GA-RT2

PNWG-CR1

SDN1HOUS

HOUS-CR1

STAR-CR1SDN1STAR

SDN2WASHJLAB

BOIS-CR1PNNL

PANTEX

SDN2SUNN

SDN1ELPA

SDN1AOFA

SDN1NEWY

SNLA ALBU-CR1

DENV-CR2

NASH-CR1 SDN1NASHORNL

ORNL-RT2

FORR

DOE-GTN

NNSA

NSOLASV-RT1WISCNET

KANS-CR1

EQX-SJ

SUNN-CR1

EQX-ASH

BNL

EQX-CHI CHIC-CR1

ORAUNOAA OSTI

PNNL

SNL-DC

LLNL & LANL-DC

WASH-CR1

CERN

EUROPE(GEANT/

NORDUNET)

KCP

Univ.  of  Washington  

PNNL  

LBNL  

NERSC  

JPL  UCLA  

SDSC  

Univ.  Of  Utah  

CSU  

NCAR  

ANL  

NCSA   RCAC  

TACC  

BNL  Stony  Brook  Univ.  

GFDL  COLA  GSFC  

LRC  NCCS   LRD  

R&E  InternaTonal  10Gb  NLR  10GE  Circuit  Internet  2  10  GE  (SDN)  Circuit  Internet  2  10  GE  IP  circuit  10  GE  Link  2.48  Gb  Link  1GE  Link  (else:  1.45~600Mb)  

CMMAP  Collaborators  Keywords  COLA-­‐Center  for  Ocean-­‐Land-­‐Atmosphere  Studies  CSU-­‐Colorado  State  University  GFDL-­‐Geophysical  Fluid  Dynamics  Laboratory  GSFC-­‐Goddard  Space  Flight  Center  JPL-­‐Jet  Propulsion  Laboratory  LRC-­‐Langley  Research  Center  LBNL-­‐Lawrence  Berkeley  NaTonal  Laboratory  

NCAR-­‐NaTonal  Center  for  Atmospheric  Research  PNNL-­‐Pacific  Northwest  NaTonal  Laboratory  SCRIPPS-­‐Scripps  InsTtuTon  of  Oceanography  UCLA-­‐University  of  California-­‐Los  Angeles  

CMMAP  Collaborators  Super  CompuTng/Data  Centers  

CS200 Algorithms and Data Structures Colorado State University

Tree data structures •  Represent hierarchical relationships

12

Page 3: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

3  

CS200 Algorithms and Data Structures Colorado State University

Outline

•  Introduction •  Terminology •  Binary Tree

–  Basic operations –  Traversals of a Binary Tree –  Representations of a Binary Tree –  Implementations

•  Binary Search Tree –  Algorithms –  Implementations –  Efficiency –  TreeSort

•  General Tree

13

CS200 Algorithms and Data Structures Colorado State University

Terminology

14

A  

B   C   D  

G  

Parent  node  

Child  node  

The  parent  child  relaTonship  is  generalized  to  the  relaTonship  of  ancestor  and  descendant  

CS200 Algorithms and Data Structures Colorado State University

Terminology

15

A  

B   C   D  

G  

Parent  node  

Child  node  

Siblings  

CS200 Algorithms and Data Structures Colorado State University

Terminology

16

A  

B   C   D  

E   F   G  

Root  

Parent  node  

Child  node  

Subtree  of  a  node  A  

Leaf  of  the  tree?  

CS200 Algorithms and Data Structures Colorado State University

Terminology

17

A  

B   C   D  

E   F   G  

Path  

CS200 Algorithms and Data Structures Colorado State University

Binary Tree •  Set T of nodes such that either,

– T is empty, or – T is partitioned into three disjoint subsets

•  A single node r, root •  Two possibly empty sets that are binary trees, called left

and right subtrees of r

•  Each node in a binary tree has no more than two children.

18

root  

TL   TR  

Page 4: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

4  

CS200 Algorithms and Data Structures Colorado State University

Outline

•  Introduction •  Terminology •  Binary Tree

–  Basic operations –  Traversals of a Binary Tree –  Representations of a Binary Tree –  Implementations

•  Binary Search Tree –  Algorithms –  Implementations –  Efficiency –  TreeSort

•  General Tree

19

CS200 Algorithms and Data Structures Colorado State University

Binary Tree

20

Root  r  

Le:  Subtree   Right  Subtree  

Le:  child  of  r   Right  child  of  r  

CS200 Algorithms and Data Structures Colorado State University

General Tree

•  General Tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: – A single node r, the root – Sets that are general trees, called subtrees of r

21

CS200 Algorithms and Data Structures Colorado State University

Degree, depth, and Height •  Degree

– Degree of a node: number of children – Degree of a tree: maximum degree of nodes.

•  Depth: length of path from node to root.

•  Height of a tree – The number of nodes on the longest path from the

root to a leaf.

22

CS200 Algorithms and Data Structures Colorado State University

Height of a Tree •  If T is empty, its height is 0.

•  If T is not empty, its height is equal to the maximum level of its nodes.

23

CS200 Algorithms and Data Structures Colorado State University

Height of a Binary Tree •  If T is empty, its height is 0.

•  If T is a non empty binary tree, height(T) = 1 + max{height(TL), height(TR)}

24

root  

TL   TR  

Height  of  TL  

Height  of  TR  

Page 5: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

5  

CS200 Algorithms and Data Structures Colorado State University

Binary trees with same nodes but different heights

25

C  

A  

B  

D   E  

F  

G  

A  

C  

B  

D  

E  

F  

G  

A  

B   C  

D   E   F   G  

Tree  A  

Tree  B  

Tree  D  

CS200 Algorithms and Data Structures Colorado State University

Frequently Asked Questions •  Curving score •  Projected score (weighted score)

3/13/13 Samgmi Lee Pallickara 26

CS200 Algorithms and Data Structures Colorado State University

Full Binary Tree •  A full binary tree is a tree in which every node

other than the leaves has two children.

27

CS200 Algorithms and Data Structures Colorado State University

Definition of Full Binary Tree •  If T is empty, T is a full binary tree of height 0.

•  If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h -1.

28

CS200 Algorithms and Data Structures Colorado State University

Complete Binary Tree •  Is a binary tree in which every level, except

possibly the last, is completely filled, and all nodes are as far left as possible.

29

CS200 Algorithms and Data Structures Colorado State University

Formal Definition of a Complete Binary Tree

•  A binary tree T of height h is complete if, – All nodes at level h-2 and above have two children

each, and – When a node at level h-1 has children, all nodes to

its left at the same level have two children each, and

– When a node at level h-1 has one child, it is a left child.

•  Full binary tree is complete.

30

Page 6: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

6  

CS200 Algorithms and Data Structures Colorado State University

Balanced Binary Tree •  A binary tree is height balanced, or balanced,

if the height of any node’s right subtree differs from the height of the node’s left subtree by no more than 1 – The height of the two subtrees of every node never

differ by more than 1

31

CS200 Algorithms and Data Structures Colorado State University

Full? Complete? Balanced? Binary tree?

32

A  

B  C   D  

E  

CS200 Algorithms and Data Structures Colorado State University

Outline

•  Introduction •  Terminology •  Binary Tree

–  Basic operations –  Traversals of a Binary Tree –  Representations of a Binary Tree –  Implementations

•  Binary Search Tree –  Algorithms –  Implementations –  Efficiency –  TreeSort

•  General Tree

33

CS200 Algorithms and Data Structures Colorado State University

Operations of the Binary Tree •  Add and remove node and subtrees •  Retrieve and set the data in the root •  Determine whether the tree is empty

34

CS200 Algorithms and Data Structures Colorado State University

General operations

35

Root!Left subtree!Right subtree  

creteBinaryTree()!makeEmpty()!isEmpty()!getRootItem()!setRootItem()!attachLeft()!attachRight()!attachLeftSubtree()!attachRightSubtree()!detachLeftSubtree()!detachRightSubtree()!getLeftSubtree()!getRightSubtree()!

CS200 Algorithms and Data Structures Colorado State University

Example tree1.setRootItem(“F”)!tree1.attachLeft(“G”)!

tree2.setRootItem(“D”)!tree2.attachLeftSubtree(tree1)!

tree3.setRootItem(“B”)!tree3.attachLeftSubtree(tree2)!tree3.attachRight(“E”)!

tree4.setRootItem(“C”)!

binTree.createBinaryTree(“A”, tree3, tree4)!

36

Page 7: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

7  

CS200 Algorithms and Data Structures Colorado State University

Traversal Algorithms

•  The traversal of a tree is the process of “visiting” every node of the tree –  Display a portion of the data in the node. –  Process the data in the node

•  Because a tree is not linear, there are many ways that this can be done.

37

CS200 Algorithms and Data Structures Colorado State University

Breadth-first traversal

•  Breadth-first processes the tree level by level starting at the root and handling all the nodes at a particular level from left to right.

38

CS200 Algorithms and Data Structures Colorado State University

Breadth-first traversal

39

60  

20   70  

10   40  

30   50  

60  –  20  –  70  –  10  –  40  –  30  –  50    

CS200 Algorithms and Data Structures Colorado State University

Depth-first traversals •  Three choices of when to visit the root r.

– These methods are recursive methods for tree traversal.

1.  Before it traverses both of r’s subtrees 2.  After it has traversed r’s left subtree (before it

traverses r’s right subtree) 3.  After it has traversed both of r’s subtrees

•  Preorder, inorder, and postorder

40

CS200 Algorithms and Data Structures Colorado State University

Depth First: Preorder traversal •  Preorder traversal processes the information

at the root, followed by the entire left subtree and concluding with the entire right subtree.

41

CS200 Algorithms and Data Structures Colorado State University

R  L   R  

L  

Right  subtree  

Le:  subtree  

Depth First: Preorder traversal

42

60  

20   70  

10   40  

30   50  

60  –  20  –  10  –  40  –  30  –  50  –  70    

Page 8: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

8  

CS200 Algorithms and Data Structures Colorado State University

Depth First: Inorder traversal

•  Inorder traversal processes all the information in the left subtree before processing the root.

•  It finishes by processing all the information in the right subtree.

43

CS200 Algorithms and Data Structures Colorado State University

Depth First: Inorder traversal

44

60  

20   70  

10   40  

30   50  

Le:  subtree  

Right  subtree  

L  

R  L   R  

10  –  20  –  30  –  40  –  50  –  60  –  70    

CS200 Algorithms and Data Structures Colorado State University

Depth First: Postorder traversal •  Postorder traversal processes the left subtree,

then the right subtree and finishes by processing the root.

45

CS200 Algorithms and Data Structures Colorado State University

Depth First: Postorder traversal

46

60  

20   70  

10   40  

30   50  

Le:  subtree  

Right  subtree  

L  

R  L   R  

10  –  30  –  50  –  40  –  20  –  70  –  60    

CS200 Algorithms and Data Structures Colorado State University

47

60  

20   70  

10   40  

30   50  

Ques%on  :  What  is  the  preorder  traversals  of  this  tree?  A.   60-­‐20-­‐10-­‐70-­‐40-­‐30-­‐50  B.   10-­‐20-­‐60-­‐70-­‐30-­‐40-­‐50  C.   10-­‐20-­‐30-­‐50-­‐40-­‐70-­‐60  

CS200 Algorithms and Data Structures Colorado State University

48

60  

20   70  

10   40  

30   50  

Ques%on  :  What  is  the  postorder  traversals  of  this  tree?  A.   60-­‐20-­‐10-­‐70-­‐40-­‐30-­‐50  B.   10-­‐20-­‐60-­‐70-­‐30-­‐40-­‐50  C.   10-­‐20-­‐30-­‐50-­‐40-­‐70-­‐60  

Page 9: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

9  

CS200 Algorithms and Data Structures Colorado State University

49

60  

20   70  

10   40  

30   50  

Ques%on  :  What  is  the  inorder  traversals  of  this  tree?  A.   60-­‐20-­‐10-­‐70-­‐40-­‐30-­‐50  B.   10-­‐20-­‐60-­‐70-­‐30-­‐40-­‐50  C.   10-­‐20-­‐30-­‐50-­‐40-­‐70-­‐60  

CS200 Algorithms and Data Structures Colorado State University

Use case: postorder traversal •  When destroying a tree, we can’t delete the

root node until we have destroyed the left and right subtree – So a postorder traversal makes sense.

50

CS200 Algorithms and Data Structures Colorado State University

Use case: Preorder/Inorder traversal (Creating and Browsing of Binary

Sorting)

51

led  subtree  exists?  

YES   NO  

a/ach  value  as  a  led  subtree  

try  the  led  subtree  

value  <  value  at  root?  

NO  YES  

right  subtree  exists?  

YES   NO  

a/ach  value  as  a  right  subtree  

try  the  right  subtree  

Value  must  be  in  the  led  subtree  

Value  must  be  in  the  right  subtree  

CS200 Algorithms and Data Structures Colorado State University

Example of Binary sorting

52

60  

20   70  

10   40  

30   50  

60   20   10   40   70   50   30  

10   20   30   40   50   60   70  

Create  Tree    

Traversal  Tree  

CS200 Algorithms and Data Structures Colorado State University

Preorder algorithm

preorder (in binTree:BinaryTree)!!if (binTree is not empty){!! !display the data in the root of binTree !!! !preorder(Left subtree of binTree’s root)!

!! !preorder(Right subtree of binTree’s root)!

!}!

53

CS200 Algorithms and Data Structures Colorado State University

Inorder algorithm

inorder (in binTree:BinaryTree)!!if (binTree is not empty){!! !inorder(Left subtree of binTree’s root)!!! !display the data in the root of binTree !!! !inorder(Right subtree of binTree’s root)!!}!

54

Page 10: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

10  

CS200 Algorithms and Data Structures Colorado State University

Postorder algorithm

postorder (in binTree:BinaryTree)!!if (binTree is not empty){!! !postorder(Left subtree of binTree’s root)!!! !postorder(Right subtree of binTree’s root)!!! !display the data in the root of binTree !!}!

55

CS200 Algorithms and Data Structures Colorado State University

Today, •  Binary Tree

– Array based implementation – Reference based implementation

•  Questions?

3/13/13 Samgmi Lee Pallickara 56

CS200 Algorithms and Data Structures Colorado State University

Analysis of Tree Traversal •  n visits occur for a tree of n nodes.

– O(n)

57

CS200 Algorithms and Data Structures Colorado State University

An array based representation (1/6)

58

A binary tree of names

Jane  

Bob   Tom  

Alan   Ellen   Nancy  

index   item   le:Child   rightChild  

0   Jane   1   2  

1   Bob   3   4  

2   Tom   5   -­‐1  

3   Alan   -­‐1   -­‐1  

4   Ellen   -­‐1   -­‐1  

5   Nancy   -­‐1   -­‐1  

6   ?   -­‐1   -­‐1  

7   ?   -­‐1   -­‐1  

8   ?   -­‐1   -­‐1  

.   .   .  

.   .   .  

.   .   .  

.   .   .  

0  

3  

root

free Free  list:  Array  –based  Linked  List    

CS200 Algorithms and Data Structures Colorado State University

An array based representation (2/6)

59

Jane  

Bob   Tom  

Alan   Ellen   Nancy  

Ques%on  :  What  is  the  maximum  number  of  nodes  at  the  level  m  in  a  binary  tree?  A.   2m  

B.   2m-­‐1  

C.   2m+1  

CS200 Algorithms and Data Structures Colorado State University

An array based representation (3/6) General operations: revisit

60

Root!Left subtree!Right subtree  

creteBinaryTree()!makeEmpty()!isEmpty()!getRootItem()!setRootItem()!attachLeft()!attachRight()!attachLeftSubtree()!attachRightSubtree()!detachLeftSubtree()!detachRightSubtree()!getLeftSubtree()!getRightSubtree()!

Page 11: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

11  

CS200 Algorithms and Data Structures Colorado State University

An array based representation (4/6)

61

public class TreeNode<T>{!!private T item;!!private int leftChild;!!private int rightChild;!!…!!public TreeNode(){!!}!!public int getItem(){!! return item;!!}!!public int getLeftChild(){!! return leftChild;!!}!!public int getRightChild(){!! !return rightChild;!!} … setters!

}!

0   1   2   3   4   5  

CS200 Algorithms and Data Structures Colorado State University

An array based representation (5/6)

62

public class BinaryTreeArrayBased<T> {!!protected final int MAX_NODES = 100;!!!protected ArrayList<TreeNode<T>> tree;!!protected int root;!!protected int free; //index of next unused array location!!…!

public BinaryTreeArrayBased<T> () {!!tree = new ArrayList<TreeNode<T>>()’ !

}!

public creatTree(TreeNode<T> _root){!!root = 0;!!tree.set(0, _root);!!free++;!

}!

CS200 Algorithms and Data Structures Colorado State University

An array based representation (6/6)

– C

63

public TreeNode<T> getRootItem(){!!return tree.get(root);!

} !

public TreeNode<T> getRight(){!!return tree.get(root.getRightChild());!

}!

public TreeNode<T> getLeft(){!!return tree.get(root.getLeftChild());!

}!

public void makeEmpty(){!!how?!

}!

More methods..!

CS200 Algorithms and Data Structures Colorado State University

Complete Binary Tree

64

Level-by-level numbering of a complete binary tree

0:Jane  

1:Bob   2:Tom  

3:Alan   4:Ellen   5:Nancy  

index   item  

0   Jane  

1   Bob  

2   Tom  

3   Alan  

4   Ellen  

5   Karen  

6  

7  

If  the  binary  tree  is  complete  and    remains  complete  

A  memory-­‐efficient  array-­‐based  implementaTon    can  be  used  

CS200 Algorithms and Data Structures Colorado State University

Complete Binary Tree

65

0:Jane  

1:Bob   2:Tom  

3:Alan   4:Ellen   5:Nancy  

Ques%on  :  What  is  the  maximum  number  of  nodes  in  a  complete  binary  tree  with  hight  h?  A.   2h  B.   2h-­‐1  C.   2h-­‐1  

CS200 Algorithms and Data Structures Colorado State University

A reference-based representation

66

ledChild   rightChild  

item  

root  

ledChild   rightChild  

item  

ledChild   rightChild  

item  

HOW?  

Page 12: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

12  

CS200 Algorithms and Data Structures Colorado State University

ledChild   rightChild  

item  

A reference-based representation

67

root  

ledChild   rightChild  

item  

ledChild   rightChild  

item  

TreeNode  

Tree  

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (1/6)

68

public TreeNode<T> {!!T item;!!TreeNode<T> leftChild;!!TreeNode<T> rightChild;!

!public TreeNode(T newItem){!! item = newItem;!

leftChild = null;! rightChild = null;!

!}!

!public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right){!

! !item = newItem;!! !leftChild = left;!! !rightChild = right;!!}!

}!

Step  1.  TreeNode  

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (2/6)

69

public class BinaryTree<T> {!

!public BinaryTree(){!!}!!public BinaryTree(T rootItem, BinaryTree<T>

leftTree,BinaryTree<T> rightTree){!! root = new TreeNode<T> (rootItem, null, null);!! attachLeftSubtree(leftTree);!! attachRightSubtree(rightTree);!!}!

!public void setRootItem(T newItem){!! !if(root!=null){!! ! !root.ite = newItem;!! !}!! !else {!! ! !root = new TreeNode<T>(newItem, null, null);!! !}!!}!

Step  2.  Tree  (BinaryTree)  

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (3/6)

70

public void attachLeft(T newItem){!!if (!isEmpty()&&root.leftChild == null) {!! !root.leftChild = new TreeNode<T>(newItem, null, null);!!}!

}!

public void attachRight(T newItem){!!if (!isEmpty()&&root.leftChild == null) {!! !root.rightChild = new TreeNode<T>(newItem, null,

null);!!}!

}!

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (4/6)

71

public void attachLeftSubtree(BinaryTree<T> leftTree)!!throws TreeException{!!if (isEmpty()) {!! !throw new TreeException(“TreeException:Empty tree.”);!!}!!else if (root.leftChild != null){!! throw new TreeException(“TreeException: cannot

overwrite left subtree.”);!!}!!else{!! !root.leftChild = leftTree.root;!! !leftTree.makeEmpty();!!}!

}!

Public void attachRightSubtree(… !similar to attachLeftSubtree()!}!

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (5/6)

72

public BinaryTree<T> detachLeftSubtree(BinaryTree<T> leftTree)!

!throws TreeException{!!if (isEmpty()) {!! !throw new TreeException(“TreeException:Empty tree.”);!!}!!else{!! !BinaryTree<T> leftTree;!! !leftTree = new BinaryTree<T>(root.leftChild);!! !root.leftChild = null;!! !return leftTree;!!} !!

}!

Public void detachRightSubtree(… !similar to detachLeftSubtree()!}!

Page 13: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

13  

CS200 Algorithms and Data Structures Colorado State University

A reference based representation (6/6)

73

Public class TreeException extends RuntimeException{!!public TreeException (String s){!! !super(s)!!}!

}!

CS200 Algorithms and Data Structures Colorado State University

Tree Traversals Using an Iterator

•  Iterator interface – next(), hasNext(), and remove()!

1.  Use a queue to order the nodes according to the type of traversal.

2.  Initialize iterator by type (pre, post or in) and enqueue all nodes, in order, necessary for traversal

3.  dequeue in the next operation

74

CS200 Algorithms and Data Structures Colorado State University

What is Java Iterator? •  An iterator allows going over all the element of

the collection in sequence

•  Unlike Enumeration, iterator allows the caller to remove element from the underlying collection –  java.util.Iterator

•  boolean hasNext() •  Object next() •  void remove()

– Java.util.Enumeration •  Boolean hasMoreElement() •  Object nextElement()

75

CS200 Algorithms and Data Structures Colorado State University

How to use Java Iterator?

76

import java.util.*; !class IteratorDemo { ! public static void main(String args[]) { ! // create an array list ! ArrayList al = new ArrayList(); ! // add elements to the array list ! al.add("C"); al.add("A"); al.add("E");!  al.add("B"); al.add("D"); al.add("F"); !

C   A   E   B   D   F  

CS200 Algorithms and Data Structures Colorado State University

How to use Java Iterator?

77

// use iterator to display contents of al !Iterator itr = al.iterator(); ! while(itr.hasNext()) {    ! Object element = itr.next();     ! System.out.print(element + " ");! }!}  !

C   A   E   B   D   F  

CS200 Algorithms and Data Structures Colorado State University

Using TreeIterator for the Preorder

78

60  

20   70  

10   40  

30   50  

60  20  70   10  40  30  50  

Front  End  

Page 14: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

14  

CS200 Algorithms and Data Structures Colorado State University

Using TreeIterator for the Inorder

79

60  

20   70  

10   40  

30   50  

10  20  70   30  40  50  60  

Front  End  

CS200 Algorithms and Data Structures Colorado State University

Using TreeIterator for the Postorder

80

60  

20   70  

10   40  

30   50  

10  30  60   50  40  20  70  

Front  End  

CS200 Algorithms and Data Structures Colorado State University

TreeIterator import java.util.LinkedList;!public class TreeIterator<T> implements java.util.Iterator<T> {!

!private BinaryTreeBasis<T> binTree; !!private TreeNode<T> currentNode; !!private LinkedList <TreeNode<T>> queue; !!public TreeIterator(BinaryTreeBasis<T> bTree) {} !

!public boolean hasNext() {} !!public T next() throws java.util.NoSuchElementException{} !

!public void setPreorder() {}!!public void setInorder() {} !!public void setPostorder() {} !

!private void preorder(TreeNode<T> treeNode) {}!!private void inorder(TreeNode<T> treeNode){} !!private void postorder(TreeNode<T> treeNode){}!

}!

81

binTree  =  bTree;          currentNode  =  null;  queue  =  new  LinkedList  <TreeNode<T>>();  return  !queue.isEmpty();  currentNode  =  queue.remove();            return  currentNode.getItem();  queue.clear();          preorder(binTree.root);  queue.clear();        inorder(binTree.root);    queue.clear();      \    postorder(binTree.root);  if  (treeNode  !=  null)  {                          queue.add(treeNode);                      preorder(treeNode.getLed());                    preorder(treeNode.getRight());          }  //  end  if  

 if  (treeNode  !=  null)  {                    inorder(treeNode.getLed());                queue.add(treeNode);                inorder(treeNode.getRight());          

}  //  end  if  

 if  (treeNode  !=  null)  {                    postorder(treeNode.getLed());                  postorder(treeNode.getRight());      queue.add(treeNode);          

}  //  end  if  

CS200 Algorithms and Data Structures Colorado State University

Outline

•  Terminology •  Binary Tree

–  Basic operations –  Traversals of a Binary Tree –  Representations of a Binary Tree –  Implementations

•  Binary Search Tree –  Algorithms –  Implementations –  Efficiency –  TreeSort

•  General Tree

82

CS200 Algorithms and Data Structures Colorado State University

Binary Search Trees

Find the record for the person whose ID number is 1212121212.

83

CS200 Algorithms and Data Structures Colorado State University

Binary Search Trees •  A binary tree T is a binary search tree if for every node n in

T:   n’s value is greater than all values in its left subtree TL

  n’s value is less than all values in its right subtree TR

  TR and TL are binary search trees

84

Page 15: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

15  

CS200 Algorithms and Data Structures Colorado State University

Is this a Binary Search Tree?

85

8  

4   9  

3   5  

6  

5  

5  

6   7  

Tree  A  

Tree  B  

Tree  C  

Ques%on  :  Find  binary  search  tree(s).  a.   Tree  A  only    b.   Tree  A  and  B  c.   Tree  A,  B  and  C  d.   Tree  A  and  C  

CS200 Algorithms and Data Structures Colorado State University

Organizing Items in BST

•  BST uses inorder traversal.

•  The sequence of adding and removing nodes influences the shape of the tree.

86

6  

5   7  6  

5  

7  

CS200 Algorithms and Data Structures Colorado State University

BST operations (1/2)

•  Insert a new item –  insert(in newItem:TreeItemType)!

•  Delete the item with a given search key –  delete(in searchKey: KeyType) throws TreeException!

•  Retrieve the item with a given search key –  retrieve(in searchKey:KeyType):TreeItemType!

87

CS200 Algorithms and Data Structures Colorado State University

Example of nodes in BST public class Person {!!private String id;!!private String phoneNo;!!private String address;!!public Person( String _id, String _phoneNo, ! ! ! ! ! ! String _address){!!! !id = _id;!!! !phoneNo = _phoneNo;!!.!!.!!.!!}!!//other methods appear here!

}!

88

CS200 Algorithms and Data Structures Colorado State University

Search in BST •  BST is a recursive algorithm. •  Search key should be unique. •  Using BST’s properties.

–  If search key == search key of root’s item •  The record is found

–  If search key < search key of root’s item •  Search in the left subtree recursive method

–  If search key > search key of root’s item •  Search in the right subtree recursive method

89

CS200 Algorithms and Data Structures Colorado State University

Example: Find the person with ID 50

90

60  

20   70  

10   40  

30   50  

50  is  smaller  than  60    Search  in  the  led  subtree  

50  is  bigger  than  60    Search  in  the  right  subtree  

50  is  bigger  than  40    Search  in  the  right  subtree  

Found  the  record  

Page 16: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

16  

CS200 Algorithms and Data Structures Colorado State University

Example: Find 50 (using recursive calls)

91

60  

20   70  

10   40  

30   50  

50  is  smaller  than  60    Search  in  the  led  subtree  

50  is  bigger  than  60    Search  in  the  right  subtree  

50  is  bigger  than  40    Search  in  the  right  subtree  

Found  the  record  

Search  (myTree  with  root  60,  50)  

Search  (subtree  of  myTree  with  root  20,  50)  

Search  (subtree  of  myTree  with  root  40,  50)  

Search  (subtree  of  myTree  with  root  50,  50)  

CS200 Algorithms and Data Structures Colorado State University

Pseudocode search(in bst:BinarySearchTree, in searchKey:KeyType)!!if (bst is empty){!!! !The desired record is not found!

}!!else if (searchKey == searchKey of root’s item){!!! !The desired record is found!

}! else if (searchKey < search key of root’s item) {! search (Left subtree of bst, searchKey)! }! else {! search(Right subtree of bst, searchKey)! }!

92

CS200 Algorithms and Data Structures Colorado State University

Insertion in BST (1/2) •  You want to insert a record for a person

– Person’s ID number is 55.

93

60  

20   70  

10   40  

30   50  

55  

CS200 Algorithms and Data Structures Colorado State University

Insertion in BST (2/2)

•  Use search to determine where in the tree to insert a new ID, 55.

•  New item will be inserted as a new leaf.

•  Different orders of insertion can get a different binary search tree.

94

CS200 Algorithms and Data Structures Colorado State University

Example: Insert a person with ID 55

95

60  

20   70  

10   40  

30   50  

55  

Found  the  locaTon  

55  is  bigger  than  50  

Step  1.  Find  the  locaWon  to  add  

Step  2.  Add  a  new  node  

CS200 Algorithms and Data Structures Colorado State University

Example: Insert item with 45

96

60  

20   70  

10   40  

30   50  

55  

A   B  

C   D   E  

Page 17: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

17  

CS200 Algorithms and Data Structures Colorado State University

Pseudocode (high-level) insertItem (in treeNode:TreeNode, ! in newItem:TreeItemType) !!Let parentNode be the parent of the empty subtree at which search terminates when it seeks newItem’s search key

!if (search terminated at parentNode’s left subtree){!!! !Set leftChild of parentNode to reference newItem!!}!!else{!!! !Set rightChild of parentNode to reference newItem!

}!

97

CS200 Algorithms and Data Structures Colorado State University

Reference to the new item

98

60  

20   70  

10   40  

30   50  

L   60   R  

L   20   R   L   70   R  

L   40   R  L   10   R  

L   30   R   L   50   R   NULL  

L   55   R  

CS200 Algorithms and Data Structures Colorado State University

Pseudocode insertItem (in treeNode:TreeNode, ! in newItem:TreeItemType)! if (treeNode is null){!!! !create a new node and let treeNode reference it !!! !create a new node with newItem as the data portion !! !set the reference in the new node to null }!

!else if (newItem.getKey() < treeNode.item.getKey()){!!! !treeNode.leftChild = !

insertItem(treeNode.leftChild, newItem)! }!!else {!!! treeNode.rightChild = !

insertItem(treeNode.rightChild, newItem)! }! return treeNode!

99

CS200 Algorithms and Data Structures Colorado State University

Example: Insert 55

100

60  

20   70  

10   40  

30   50  

insertItem  (myTree  with  root  60,  55)  

insertItem  (subtree  of  myTree  with  root  20,  55)  

insertItem  (subtree  of  myTree  with  root  40,  55)  

insertItem  (subtree  of  myTree  with  root  50,  55)  

insertItem  (null,  55)  

L   50   R   NULL  

Create  a  new  item  Return  new  item  

Returned  new  item  is  referenced  by  treeNode.rightChild  And  return  this  treeNode  (with  root    50)  

Returned  item  (treeNode  with  root  50)  is  referenced  by  treeNode.rightChild  And  return  this  treeNode  (with  root  40)  

Returned  item  (treeNode  with  root  40)  is  referenced  by  treeNode.rightChild  And  return  this  treeNode  (with  root  20)  

Returned  item  (treeNode  with  root  20)  is  referenced  by  treeNode.ledChild  And  return  this  treeNode  (with  root60  )  

55  

CS200 Algorithms and Data Structures Colorado State University

Deletion in BST

•  Use search algorithm to locate the item with the specified search key.

•  Three cases to consider: 1.  N is a leaf 2.  N has only one child 3.  N has two children

101

CS200 Algorithms and Data Structures Colorado State University

Case 1. N is a leaf •  Set the reference in its parent to null.

102

L   60   R  

L   20   R   L   70   R  

NULL  

Page 18: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

18  

CS200 Algorithms and Data Structures Colorado State University

Case 2. N has only one child (left child) •  Let N’s parent adopt N’s child.

103

L   60   R  

L   20   R   L   70   R  

L   10   R   L   80   R  

CS200 Algorithms and Data Structures Colorado State University

Case 2. N has only one child (right child) •  Let N’s parent adopt N’s child.

104

L   60   R  

L   20   R   L   70   R  

L   10   R   L   80   R  

CS200 Algorithms and Data Structures Colorado State University

Case 3. N has two children

1.  Find the inorder successor of N’s search key. –  The node whose search key comes

immediately after N’s search key –  The inorder successor is in the leftmost node

in N’s right subtree.

2.  Copy the item of the inorder successor, M to the deleting node N.

3.  Remove the node M from the tree.

105

CS200 Algorithms and Data Structures Colorado State University

Remove 20

106

60  

20   70  

10   40  

30   50  

Step  1.  Locate  the  inorder  successor  Ledmost  node  of  node-­‐20’s  right  subtree  

30  

Step  2.  Copy  item  in  the  inorder  successor                  to  the  node-­‐20  

Step  3.  remove  node  30  

CS200 Algorithms and Data Structures Colorado State University

Retrieval in BST retrieveItem(in treeNode:TreeNode, ! in searchKey:KeyType): TreeItemType!!if(treeNode ==null){!!! !treeItem = null!!}!!else if (searchKey< treeNode.item.getKey()){!!! !treeItem = !!! ! !retrieveItem(treeNode.leftChild, searchKey)!!}!!else{!!! !treeItem = !!! ! !retrieveItem(treeNode.rightChild, searchKey)!!}!!return treeItem!

107

CS200 Algorithms and Data Structures Colorado State University

Does the inorder traversal of a BST visit its nodes in sorted-key order?

Theorem 6-1 The inorder traversal of a binary search tree T will

visit its nodes in sorted search-key order.

108

Proof

Prichard  Chapter  11,  Theorem  11-­‐1  pp.  615  

Page 19: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

19  

CS200 Algorithms and Data Structures Colorado State University

The Efficiency of BST Operations

•  Searching, inserting, deleting, and retrieving data

•  Compare the specified value searchKey to the search keys in the nodes along a path through the tree.

•  The maximum number of comparisons that these operations can require is equal to the height of the binary search tree.

109

CS200 Algorithms and Data Structures Colorado State University

The maximum and minimum heights of a BST

•  Maximum height: each internal node has exactly one child.

110

60  

20  

70  

10  

40  

30  

50  

CS200 Algorithms and Data Structures Colorado State University

Counting the nodes in a full binary tree of height h

111

60  

20   70  

10   40  

30   50  30   50  

10   40  

30   50  30   50  

Level  1  :  1  node  (20),    Total  number  of  nodes  unTl  this  level:  1  =  21  -­‐  1  

Level  2  :  2  nodes  (21),  Total  number  of  nodes  unTl  this  level:  3  =  22  -­‐  1  

Level  3  :  4  nodes  (22),  Total  number  of  nodes  unTl  this  level:  7  =  23  -­‐  1  

Level  4  :  8  nodes  (23),  Total  number  of  nodes  unTl  this  level:  15  =  24  -­‐  1  

Level  h  :  2h-­‐1  nodes,  Total  number  of  nodes  unTl  this  level:  2h  -­‐  1  

CS200 Algorithms and Data Structures Colorado State University

Theorem 6-2 The maximum number of nodes that a binary

tree can have on the level h > 0 is 2h-1

112

Proof  

Prichard  Chapter  11,  Exercises  25  pp.  638  

CS200 Algorithms and Data Structures Colorado State University

Theorem 6-3 A full binary tree of height h ≥ 0 has 2h – 1 nodes

113

Proof  

Prichard  Chapter  11,  Theorem  11-­‐3  pp.  621  

CS200 Algorithms and Data Structures Colorado State University

Theorem 6-4 The minimum height of a binary tree with n

nodes is

114

Proof  

Prichard  Chapter  11,  Theorem  11-­‐4  pp.  622  

Page 20: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

20  

CS200 Algorithms and Data Structures Colorado State University

•  Complete trees and full trees with n nodes have heights of

•  The height of an n-node BST ranges from to n

•  Insertion in search-key order will need maximum-height BST. – Adding the node to the leaf node.

115

CS200 Algorithms and Data Structures Colorado State University

Treesort •  Sort an array of records efficiently into a search-

key order.

•  Each insertion into a BST requires O(log n) operations in the average case, and O(n) for the worst case.

•  n insertions requires O(n log n) operations in the average case and O(n2) in the worst case.

•  Sorted items in the tree are traversed and copied to the array: O(n)

•  Average case: O(n log n) + O(n) = O(n log n) •  Worst case: O(n2) + O(n) = O(n2)

116

CS200 Algorithms and Data Structures Colorado State University

Storing a BST in a file •  Storing and restoring

•  Original shape vs. balanced shape? – There is NO difference in the ADT operations. – Efficient operations are assured if the binary search

tree is balanced.

117

CS200 Algorithms and Data Structures Colorado State University

How to guarantee a restored tree of minimum height

•  Create complete binary search tree with n nodes

readTree(in inputFile:FileType, ! in n:integer):Treenode!

!if (n > 0 ){!!! !treeNode = reference to new node !

with null child references!!! !Set treeNode’s left child to !

readTree(inputFile, n/2)! Read Item from file into treeNode’s item! Set treeNode’s right child to ! readFull(inputFile, (n-1)/2)! }! return treeNode!

118

CS200 Algorithms and Data Structures Colorado State University

Example: store

119

60  

20   70  

10   40  

30   50  

55  

10   20   40  30   50   55   60   70  

CS200 Algorithms and Data Structures Colorado State University

Example: restore

120

50  

30   60  

55   70  

70  60  50   55  40  30  20  10  LEFT  tree   Right  tree  

L     R    40  30  20  10   70  60  55  

L   R  

20  10  L    

20   40  

10  

Page 21: CS200 Algorithms and Data Structures Colorado … 1 CS200 Algorithms and Data Structures Colorado State University Part 6. Trees (1) CS 200 Algorithms and Data Structures 1 CS200 Algorithms

3/13/13  

21  

CS200 Algorithms and Data Structures Colorado State University

Comparison

121

50  

30   60  

55   70  20   40  

10  

60  

20   70  

10   40  

30   50  

55  

CS200 Algorithms and Data Structures Colorado State University

n-ary General tree •  Tree with no more than n children. •  How can we implement it?

122

CS200 Algorithms and Data Structures Colorado State University

n = 3

123

A  

B   D  

E   G  F   H   I  

C  Oldest  child  Or  first  child  

A  

.   .  

A  

.   .  .  

Case  1:  using  2  references  

Case  2:  using  3  references  

CS200 Algorithms and Data Structures Colorado State University

Case 1: Using 2 references

124

A  

A   A   A  

A   A   A  

A   A  

CS200 Algorithms and Data Structures Colorado State University

Case 2: Using 3 references

125

A  

I  H  

G  E   F  

D  B   C