trees · web viewtrees a tree is another data structure (similar to a graph with nodes and edges)....

6
Trees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a Tree is connected and has NO cycles or loops. Typical uses include: hierarchical data like moves in a game An OS may use trees for directories making information easy to search manipulating sorted lists of data Complete the “Letter” column using the both the description and the diagram above Name Description Lette r Node The node contains the tree data Edge An edge connects two nodes. Every node except the root is connected by exactly one edge from another node in the level above it Root This is the only node that no incoming edges Child The set of nodes that have incoming edges from the same node Parent A node is a parent of all nodes it connects to with outgoing edges Subtre e The set of nodes and edges comprised of a parent and all descendents of the parent. A subtree may also be a leaf Leaf node A node that has no children Question Please circle any of the following that is NOT a tree

Upload: others

Post on 28-Feb-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees · Web viewTrees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a

TreesA tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a Tree is connected and undirected and has NO cycles or loops. Typical uses include:

hierarchical data like moves in a game An OS may use trees for directories making information easy to search manipulating sorted lists of data

Complete the “Letter” column using the both the description and the diagram above

Name Description LetterNode The node contains the tree dataEdge An edge connects two nodes. Every node except the root is connected by

exactly one edge from another node in the level above itRoot This is the only node that no incoming edgesChild The set of nodes that have incoming edges from the same nodeParent A node is a parent of all nodes it connects to with outgoing edgesSubtree The set of nodes and edges comprised of a parent and all descendents of the

parent. A subtree may also be a leafLeaf node A node that has no children

QuestionPlease circle any of the following that is NOT a tree

A binary search treeA binary tree is a rooted tree in which each node has a max of 2 children. A binary search tree holds items in such a way that the tree can be searched quickly and easily for a particular item, new items can be easily added and the whole tree can be printed out in sequence. A binary search tree is a typical use of a rooted tree

Page 2: Trees · Web viewTrees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a

Constructing a binary search treeSuppose the following list of numbers is to be inserted into a binary tree, in the order given, in such a way that the tree can be quickly searched

17, 8, 4, 12, 22, 19, 14, 5, 30, 25

The algorithm is:

Place the first item at the root Then for each item in the list: If the value of the new data item is less than the value in the

current node then branch left, otherwise branch right. Keep repeating this process until you come to an ‘empty’ branch, then put the new value in

the node at the end of the branch

QuestionUsing the algorithm and list of numbers above write the list of numbers in their correct node

Traversing a tree (3 ways)In-order, Pre-order and Post order.

In-order traversalDraw an outline around the tree structure, starting to the left of the root. As you pass underneath a node (red dot), output the data in that node.

Use: puts list in sequential order

QuestionWhat sequence will the modes be visited using in-order traversal?

Page 3: Trees · Web viewTrees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a

Pre-order traversalDraw an outline around the tree, starting to the left of the root. As you pass to the left of a node (red) dot, output the data in that node

Use: to produce prefix notation in coding (x=sum a,b rather than x=a+b).

QuestionWhat sequence will the modes be visited using Pre-order traversal?

Post orderDraw an outline around the tree, starting to the left of the root. As you pass to the right of a node (red dot), output the data in that node

Use: Reverse Polish Notation

QuestionWhat sequence will the modes be visited using post-order traversal?

Page 4: Trees · Web viewTrees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a

Implementation of a binary search tree

A binary search tree can be implemented using an array of recrods, with each node consisting of: a left pointer, data item and right pointer. The numbers 17, 8, 4, 12, 22, 19, 14, 5, 30, 25 used to construct the tree aboce could be held as follows:

For example, the left pointer in tree[0] points to tree[1] and the right pointer points to tree[4]. The value -1 is a ‘rogue value’ which indicates that there is no child on the relevant side (left or right)

left data Righttree[0] 1 17 4tree[1] 2 8 3tree[2] -1 4 7tree[3] -1 12 6tree[4] 5 22 6tree[5] -1 19 -1tree[6] -1 14 -1tree[7] -1 5 -1tree[8] 9 30 3tree[9] -1 25 -1

QuestionShow how the search tree below could be implemented in an array with left and right pointers.

left data Right

tree[0]tree[1]tree[2]tree[3]tree[4]tree[5]tree[6]tree[7]tree[8]tree[9]tree[10

]

Page 5: Trees · Web viewTrees A tree is another data structure (similar to a graph with nodes and edges). This structure is visualised as a hierarchical structure i.e. unlike a graph, a

Names were inserted in the tree in the following order: Monkey, Topi, Ostrich, Giraffe, Hippo, Zebra, Buffalo, Cheetah, Rhino, Baboon, Jackal