1 / 21

Chapter 6

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.

stacey
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 More Trees

  2. General Trees

  3. 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.

  4. 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

  5. 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.

  6. Example

  7. 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.

  8. 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

  9. Parent Pointer

  10. 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

  11. Union/Find Processing

  12. Union/Find Processing

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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.

  18. 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

  19. 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

  20. 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

  21. 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)))

More Related