10Trees - CS 240 Chapter 10 – Trees CS 240 Chapter 10 –...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 1Page 1 Chapter 10 Trees The tree abstract data type provides a hierarchical structure to the representation of certain types of relationships. ü Array Implementation ü Applications ü Linked List Implementation ü General Trees ü Binary Search Trees CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 2Page 2 The Binary Tree ADT The binary tree abstract data type is a structure in which entries are configured to have at most two offspring. Root Leaf Node Leaf Leaf Binary Tree In addition to constructors and a destructor, the binary tree ADT needs the following functions: • isEmpty - to determine whether the binary tree contains any entries • insert - to insert a new entry at the appropriate location within the tree • traverse - some mechanism to explore the tree The binary aspect of this structure makes it particularly useful in applications where individual choices are needed, making it possible to recursively proceed to the right offspring or to the left offspring of the current node. For example, video games frequently present a player with two options, using a game tree to keep track of the alternatives selected by the player. CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 3Page 3 Basic Binary Tree Terminology value value value value value value value value value value value value value value value value leaf nodes sibling nodes parent node child node CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 4Page 4 Application: The Heap 1000 875 650 325 700 575 400 25 250 350 425 150 275 75 100 Keeps the data in each node greater than or equal to the data in both of its subtrees. CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 5Page 5 Application: The Binary Search Tree Keeps the data in each node's left subtree less than or equal to the node's data, and the data in the node's right subtree greater than or equal to the node's data. 72 41 90 23 50 74 82 14 97 79 98 67 31 99 38 CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 6Page 6 Application: The Binary Expression Tree Strategically places the binary operators in the upper nodes, and the primitive operands in the leaf nodes. Example: (A+B)/C+D*((E+F)%(G-H)) + / * + C D + A % E G F H- B CS 240 Chapter 10 – Trees CS 240 Chapter 10 – Trees 7Page 7 Array Implementation Of Binary Tree • Place root in slot #0 • Place left child of slot k's node in slot #(2*k+1) • Place right child of slot k's node in slot #(2*k+2) • Locate parent of slot k's node in slot #((k-1)/2)       ...
View Full Document

{[ snackBarMessage ]}

Page1 / 36

10Trees - CS 240 Chapter 10 – Trees CS 240 Chapter 10 –...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online