210 likes | 334 Views
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.
E N D
Chapter 6 More 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. • Each node has precisely one parent expect for the root of the tree.
Tree Activities • Any tree needs to be able to initialize • You should be able to access the root of the tree and probably the children. • How do you access the children when you don’t know how many children a node has • You can give an index for the child you want. • You can give access to the leftmost child and then the next child
Tree Traversals • We can easily define pre-order and post-order traversals for trees • Generally we don’t talk about in-order traversals because we don’t know how many nodes we have.
Parent-Pointer Implementation • A very simple way to implement a tree is to have each node store a reference to its parent • This is not a general purpose general tree. • This is used to equivalence classes. • Could also be used to store information about cities on roads, circuits on a board, etc.
Parent Pointer • Two basic operations • To determine if two objects are in the same set • To merge two sets together • Called Union/Find • This can easily be implemented in an array with the parent reference being the index of the parent
Union/Find • Initially all nodes are roots of their own tree • Pairs of equivalent nodes are passed in. • Either pair is assumed to be the parent, in this example we will use the first node alphabetically as the parent
Issues • There is no limit on the number of nodes that can share the same parent • We want to limit the depth of the tree as much as we can • We can use the weighted union rule when joining two trees • We will make the smaller tree point to the bigger tree with Unioning two trees
Path Compression • The weighted union rule will help when we are joining two trees, but we will not always join two trees • Path compression will allow us to create very shallow trees • Once we have found the parent for node X, we set all the nodes on the path from X to the parent to point directly to X’s parent.
General Tree Implementations • List of Children • Each node has a linked list of its children • Each node is implemented in an array • Not so easy to find a right sibling of a node • Combining trees can be difficult if the trees are stored in separate arrays
Left-Child/Right Sibling • Each node stores • Its value • A pointer to its parent • A pointer to its leftmost child • A pointer to it right sibiling
Dynamic Node Implementations • Problem: How many children do you allow a node to store? • You can limit the space and allow a known number of children. • You can create an array for the children. • You can allow for variable numbers of children. • You can have a linked list for the children.
Left-Child/Right Sibling • Another approach is to convert a general tree into a binary tree. • You can do this by having each node store a pointer to its right sibling and its left child
K-ary Trees • K-ary trees have K children • As K becomes large the potential for the number of NULL pointers becomes greater. • As K becomes large, you may need to choose a different implementation for the internal and leaf nodes
Sequential Implementations • Store nodes with minimal amount of information needed to reconstruct the tree • Has great space saving advantages • A disadvantage is that you must access the tree sequentially. • Ideal for saving trees to disk • It stores the node’s information as they would be enumerated in a pre-order traversal
Examples • For a binary tree, we can store node values and null pointers • AB/D//CEG//FH//I// • Alternative approach – mark internal nodes and store only empty subtrees • A’B’/D’C’E’G/F’HI • Third Alternative for general trees – mark end of a child list • RAC)D)E))BF)))