Chapter 6

21
Chapter 6 Chapter 6 More Trees More Trees

description

Chapter 6. More Trees. General Trees. Definitions. A Tree T is a finite set of one or more node such that there is one designated node called the root of T. A node’s out degree is the number of children for that node A forest is collection of trees. - PowerPoint PPT Presentation

Transcript of Chapter 6

Page 1: Chapter 6

Chapter 6Chapter 6More TreesMore Trees

Page 2: Chapter 6

General TreesGeneral Trees

Page 3: Chapter 6

DefinitionsDefinitions►A Tree T is a finite set of one or more A Tree T is a finite set of one or more

node such that there is one designated node such that there is one designated node called the root of T.node called the root of T.

►A node’s A node’s out degree out degree is the number of is the number of children for that nodechildren for that node

►A A forestforest is collection of trees. is collection of trees.►Each node has precisely one parent Each node has precisely one parent

expect for the root of the tree.expect for the root of the tree.

Page 4: Chapter 6

Tree ActivitiesTree Activities►Any tree needs to be able to initializeAny tree needs to be able to initialize►You should be able to access the root You should be able to access the root

of the tree and probably the children.of the tree and probably the children. How do you access the children when you How do you access the children when you

don’t know how many children a node hasdon’t know how many children a node has You can give an index for the child you You can give an index for the child you

want.want. You can give access to the leftmost child You can give access to the leftmost child

and then the next childand then the next child

Page 5: Chapter 6

Tree TraversalsTree Traversals►We can easily define pre-order and We can easily define pre-order and

post-order traversals for treespost-order traversals for trees►Generally we don’t talk about in-order Generally we don’t talk about in-order

traversals because we don’t know how traversals because we don’t know how many nodes we have.many nodes we have.

Page 6: Chapter 6

ExampleExample

Page 7: Chapter 6

Parent-Pointer Parent-Pointer ImplementationImplementation

►A very simple way to implement a tree is A very simple way to implement a tree is to have each node store a reference to its to have each node store a reference to its parentparent

►This is not a general purpose general This is not a general purpose general tree.tree.

►This is used to equivalence classes.This is used to equivalence classes.►Could also be used to store information Could also be used to store information

about cities on roads, circuits on a board, about cities on roads, circuits on a board, etc.etc.

Page 8: Chapter 6

Parent PointerParent Pointer►Two basic operationsTwo basic operations

To determine if two objects are in the To determine if two objects are in the same setsame set

To merge two sets togetherTo merge two sets together►Called Union/FindCalled Union/Find►This can easily be implemented in an This can easily be implemented in an

array with the parent reference being array with the parent reference being the index of the parentthe index of the parent

Page 9: Chapter 6

Parent PointerParent Pointer

Page 10: Chapter 6

Union/FindUnion/Find► Initially all nodes are roots of their own Initially all nodes are roots of their own

treetree►Pairs of equivalent nodes are passed Pairs of equivalent nodes are passed

in.in.►Either pair is assumed to be the Either pair is assumed to be the

parent, in this example we will use the parent, in this example we will use the first node alphabetically as the parentfirst node alphabetically as the parent

Page 11: Chapter 6

Union/Find ProcessingUnion/Find Processing

Page 12: Chapter 6

Union/Find ProcessingUnion/Find Processing

Page 13: Chapter 6

IssuesIssues►There is no limit on the number of nodes There is no limit on the number of nodes

that can share the same parentthat can share the same parent►We want to limit the depth of the tree as We want to limit the depth of the tree as

much as we canmuch as we can►We can use the We can use the weighted union ruleweighted union rule

when joining two treeswhen joining two trees►We will make the smaller tree point to We will make the smaller tree point to

the bigger tree with Unioning two treesthe bigger tree with Unioning two trees

Page 14: Chapter 6

Path CompressionPath Compression►The weighted union rule will help when The weighted union rule will help when

we are joining two trees, but we will not we are joining two trees, but we will not always join two treesalways join two trees

►Path compression will allow us to create Path compression will allow us to create very shallow treesvery shallow trees

►Once we have found the parent for node Once we have found the parent for node X, we set all the nodes on the path from X, we set all the nodes on the path from X to the parent to point directly to X’s X to the parent to point directly to X’s parent.parent.

Page 15: Chapter 6

General Tree General Tree ImplementationsImplementations

►List of ChildrenList of Children Each node has a linked list of its childrenEach node has a linked list of its children Each node is implemented in an arrayEach node is implemented in an array Not so easy to find a right sibling of a Not so easy to find a right sibling of a

nodenode Combining trees can be difficult if the Combining trees can be difficult if the

trees are stored in separate arraystrees are stored in separate arrays

Page 16: Chapter 6

Left-Child/Right SiblingLeft-Child/Right Sibling►Each node storesEach node stores

Its valueIts value A pointer to its parentA pointer to its parent A pointer to its leftmost childA pointer to its leftmost child A pointer to it right sibilingA pointer to it right sibiling

Page 17: Chapter 6

Dynamic Node Dynamic Node ImplementationsImplementations

►Problem: How many children do you Problem: How many children do you allow a node to store?allow a node to store? You can limit the space and allow a known You can limit the space and allow a known

number of children.number of children.►You can create an array for the children.You can create an array for the children.

You can allow for variable numbers of You can allow for variable numbers of children.children.►You can have a linked list for the children.You can have a linked list for the children.

Page 18: Chapter 6

Left-Child/Right SiblingLeft-Child/Right Sibling► Another approach is to convert a general Another approach is to convert a general

tree into a binary tree.tree into a binary tree.► You can do this by having each node store a You can do this by having each node store a

pointer to its right sibling and its left childpointer to its right sibling and its left child

Page 19: Chapter 6

K-ary TreesK-ary Trees►K-ary trees have K childrenK-ary trees have K children►As K becomes large the potential for As K becomes large the potential for

the number of NULL pointers becomes the number of NULL pointers becomes greater.greater.

►As K becomes large, you may need to As K becomes large, you may need to choose a different implementation for choose a different implementation for the internal and leaf nodesthe internal and leaf nodes

Page 20: Chapter 6

Sequential ImplementationsSequential Implementations►Store nodes with minimal amount of Store nodes with minimal amount of

information needed to reconstruct the treeinformation needed to reconstruct the tree►Has great space saving advantagesHas great space saving advantages►A disadvantage is that you must access A disadvantage is that you must access

the tree sequentially.the tree sequentially.► Ideal for saving trees to diskIdeal for saving trees to disk► It stores the node’s information as they It stores the node’s information as they

would be enumerated in a pre-order would be enumerated in a pre-order traversaltraversal

Page 21: Chapter 6

ExamplesExamples►For a binary tree, we can store node For a binary tree, we can store node

values and null pointersvalues and null pointers AB/D//CEG//FH//I//AB/D//CEG//FH//I//

►Alternative approach – mark internal Alternative approach – mark internal nodes and store only empty subtreesnodes and store only empty subtrees A’B’/D’C’E’G/F’HIA’B’/D’C’E’G/F’HI

►Third Alternative for general trees – Third Alternative for general trees – mark end of a child listmark end of a child list RAC)D)E))BF)))RAC)D)E))BF)))