1 / 45

Discrete Mathematics Computer Science Level 3

Explore the fundamentals of trees in discrete mathematics and computer science, including binary trees, complete binary trees, and binary tree representations. Learn about nodes, degrees, parent-child relationships, and depth calculations.

willief
Download Presentation

Discrete Mathematics Computer Science Level 3

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. Discrete Mathematics Computer Science Level 3 Trees

  2. Nature View of a Tree leaves branches root

  3. root nodes Computer Scientist’s View leaves branches

  4. Definition of Tree • A tree is a finite set of one or more nodes such that: • There is a specially designated node called the root. • The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. • We call T1, ..., Tn the subtrees of the root.

  5. Terminology • The degree of a node is the number of subtreesof the node • The degree of A is 3; the degree of C is 1. CHAPTER 5

  6. Given the following tree show: • The node with degree 0 is a leaf or terminal node. • A node that has subtrees is the parent of the roots of the subtrees. • The roots of these subtrees are the children of the node. • Children of the same parent are sbliings. • The ancestors of a node are all the nodes along the path from the root to the node.

  7. Binary Trees • A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtreeand the right subtree. • Any tree can be transformed into binary tree. • by left child-right sibling representation • The left subtree and the right subtree are distinguished. CHAPTER 5

  8. Samples of Trees A A A B B B C C F G D E D E H I Complete Binary Tree 1 2 Skewed Binary Tree 3 4 5 CHAPTER 5

  9. The maximum number of nodes on level i of a binary tree is 2i-1, i>=1. • The maximum nubmer of nodes in a binary tree of depth k is 2k-1, k>=1.

  10. Sequential Representation [1] [2] [3] [4] [5] [6] [7] [8] [9] A B C F G D E H I A B C D E F G H I CHAPTER 5

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  33. Is This Complete?

  34. Is This Complete?

  35. Is This Complete?

  36. Is This Complete?

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

  38. Implementing a Complete Binary Tree • We will store the data from the nodes in a partially-filled array. 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.

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

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

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

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

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

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

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

More Related