Chapter 6
description
Transcript of Chapter 6
Chapter 6Chapter 6More TreesMore Trees
General TreesGeneral Trees
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.
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
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.
ExampleExample
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.
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
Parent PointerParent Pointer
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
Union/Find ProcessingUnion/Find Processing
Union/Find ProcessingUnion/Find Processing
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
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.
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
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
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.
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
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
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
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)))