1 / 80

Trees

Trees. Main and Savitch Chapter 10. Binary Trees. A binary tree has nodes , similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. But it is the connections between the nodes which characterize a binary tree. A Binary Tree of States.

balthasar
Download Presentation

Trees

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. Trees Main and Savitch Chapter 10

  2. Binary Trees • A binary tree has nodes, similar to nodes in a linked list structure. • Data of one sort or another may be stored at each node. • But it is the connections between the nodes which characterize a binary tree.

  3. A Binary Tree of States In this example, the data contained at each node is one of the 50 states.

  4. A Binary Tree of States Each tree has a special node called its root, usually drawn at the top.

  5. A Binary Tree of States Each tree has a special node called its root, usually drawn at the top. The example tree has Washington as its root.

  6. A Binary Tree of States Each node is permitted to have two links to other nodes, called the left childand the right child.

  7. A Binary Tree of States Children are usually drawn below a node. The right child of Washington is Colorado. The left child of Washington is Arkansas.

  8. A Binary Tree of States Some nodes have only one child. Arkansas has a left child, but no right child.

  9. A Quiz Some nodes have only one child. Which node has only a right child?

  10. A Quiz Some nodes have only one child. Florida has only a right child.

  11. A Binary Tree of States A node with no children is called a leaf.

  12. A Binary Tree of States Each node is called the parent of its children. Washington is the parent of Arkansas and Colorado.

  13. A Binary Tree of States Two rules about parents: • The root has no parent. • Every other node has exactly one parent.

  14. A Binary Tree of States Two nodes with the same parent are called siblings. Arkansas and Colorado are siblings.

  15. Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us.

  16. Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us. When a complete binary tree is built, its first node must be the root.

  17. Complete Binary Trees The second node of a complete binary tree is always the left child of the root...

  18. Complete Binary Trees The second node of a complete binary tree is always the left child of the root... ... and the third node is always the right child of the root.

  19. Complete Binary Trees The next nodes must always fill the next level from left to right.

  20. Complete Binary Trees The next nodes must always fill the next level from left to right.

  21. Complete Binary Trees The next nodes must always fill the next level from left to right.

  22. Complete Binary Trees The next nodes must always fill the next level from left to right.

  23. Complete Binary Trees The next nodes must always fill the next level from left to right.

  24. Complete Binary Trees The next nodes must always fill the next level from left to right.

  25. Is This Complete?

  26. Is This Complete?

  27. Is This Complete?

  28. Is This Complete?

  29. Is This Complete? Yes! It is called the empty tree, and it has no nodes, not even a root.

  30. We will store the data from the nodes in a partially-filled array. Implementing a Complete Binary Tree An integer to keep track of how many nodes are in the tree 3 An array of data We don't care what's in this part of the array.

  31. We will store the date from the nodes in a partially-filled array. Implementing a Complete Binary Tree An integer to keep track of how many nodes are in the tree 3 Read Section 10.2 to see details of how the entries are stored. An array of data We don't care what's in this part of the array.

  32. Depth of Binary Tree This example, depth = 3

  33. Depth of Binary Tree This example, depth = 2

  34. Depth of Binary Tree This example, depth = 0

  35. Depth of Complete Binary Tree Given a complete binary tree of N nodes, what is the depth? D = 0 N = 1 D = 1 N = 3 D = 1 N = 2 D = 2 N = 4 D = 2 N = 7

  36. Depth of Complete Binary Tree Given a complete binary tree of N nodes, what is the depth? D = 0 N = 1 D = 1 N = 3 D = 1 N = 2 D = 2 N = 4 D = 2 N = 7 D = floor(log N) = O(log N)

  37. Depth of Binary Tree Given a binary tree of N nodes, what is the maximum possible depth? D = 0 N = 1 D = 2 N = 3 D = 4 N = 5 D = O(N)

  38. Summary • Binary trees contain nodes. • Each node may have a left child and a right child. • If you start from any node and move upward, you will eventually reach the root. • Every node except the root has one parent. The root has no parent. • Complete binary trees require the nodes to fill in each level from left-to-right before starting the next level.

  39. Binary Search Trees • One of the tree applications in Chapter 10 is binary search trees. • In Chapter 10, binary search trees are used to implement bags and sets. • This presentation illustrates how another data type called a dictionary is implemented with binary search trees.

  40. The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key.

  41. The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key. Example: The items I am storing are records containing data about a state.

  42. The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key. Example: The key for each record is the name of the state. Washington

  43. The Dictionary Data Type void Dictionary::insert(The key for the new item, The new item); • The insertion procedure for a dictionary has two parameters. Washington

  44. The Dictionary Data Type • When you want to retrieve an item, you specify the key... Item Dictionary::retrieve("Washington");

  45. The Dictionary Data Type • When you want to retrieve an item, you specify the key... ... and the retrieval procedure returns the item. Item Dictionary::retrieve("Washington");

  46. The Dictionary Data Type • We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

  47. Arizona Arkansas A Binary Search Tree of States Florida The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Oklahoma Colorado Mass. Washington New Hampshire West Virginia

  48. Colorado Arizona Arkansas A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Mass. Washington New Hampshire West Virginia

  49. Arizona A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Mass. Washington Example: ' Massachusetts' and ' New Hampshire' are alphabetically before 'Oklahoma' New Hampshire West Virginia Arkansas

  50. Arizona A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. • Every key to the right of a node is alphabetically after the key of the node. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas

More Related