1 / 17

Introduction to Trees

This introduction provides an overview of trees, their definitions, and their importance in computer science. Learn about the different types of trees and their uses in data structures and algorithms.

lintonm
Download Presentation

Introduction to 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. Introduction to Trees

  2. when here we can only go here if here then here or here Tree Graph Trees Motivation When at a node in a singly linked list, there is no choice as to which node can be visited next: In a doubly linked list we have a choice of 2 nodes: If decisions or multiple possibilities are inherent in an application, then more than one or two nodes should be able to be reached from any given node. Data structures allowing this include trees and graphs: Trees can be regarded as falling between linear lists and graphs in the hierarchy of structural complexity.

  3. Trees Tree Definitions There are many ways of defining a tree. We will look at 4 definitions, and as we do, we will define many other aspects of trees, which MUST be understood and remembered for successful mastery: 1) Definition in terms of Graphs: A tree is a connected, undirected graph that contains no circuit. In other words, there is a unique path between any 2 nodes. Not a tree, because there is a circuit: We use the adjective connected since one graph can be considered as consisting of two disconnected parts: The adjective undirected allows the definition to be more general: undirected graph: directed graph or digraph: (restricted, since you can’t go against an arrow) (general, since you can go both ways between nodes) considered as one graph

  4. For Example: Trees Because computer applications of trees require that we start at one designated node in the tree in order to reach the other nodes, we drop the undirected requirement from the definition: root Using directed edges allows us to designate the node where we should start as the root node. Definition: The root node has indegree of 0. Indegree ≡ The number of directed edges pointing at a node. Outdegree ≡ The number of directed edges pointing away from a node. Leaf nodes have outdegree of 0. This root has: indegree = 0 and outdegree = 4 This internal node has: indegree = 1 and outdegree = 3

  5. Trees To impose some standardization so that we can visually pick out the root more easily, computer scientists require that trees be rooted: side rooted bottom rooted top rooted (European) (American) (compromise) When we root a directed tree, it is no longer necessary to draw the arrows on the directed edges. They are understood to be there: (From now on, we will usually draw trees as top-rooted, with all edges understood to be directed downwards.)

  6. Trees 2) Definition in terms of Number of Edges and Nodes: A tree is a finite connected graph such that: no. of Nodes = no. of Edges + 1 For Example: 5 nodes 4 edges 8 nodes 7 edges 3 nodes 2 edges 3) Definition in terms of Connectivity: A graph is a tree iff the removal of any edge would disconnect the graph: Removing this, or any other edge, disconnects the graph. For Example: Definition: A collection of disconnected trees is called a forest: A forest of 5 trees. For Example:

  7. The Recursion Trees 4) Recursive Definition (Defining trees in terms of trees.) (This is the definition that computer scientists prefer.) A tree is a finite set of nodes which is either empty (root=NULL) or there is one node called the root node and the remaining nodes can be partitioned into disjoint sets each of which is also a tree. These 3 disjoint partitions are also trees – and with respect to the larger structure, they are called subtrees. 3 1 2 This recursive definition for trees gives us some easily defined search algorithms, which we will study later.

  8. Trees Defining Parts of a Tree: Many scientific and medical terms seem obscure to us because these disciplines originated millennia ago within ancient languages. Many medical terms derive from ancient Greek. stetho·scope ophthalmo·scope osteo·myel·itis lungs eye Greek: to watch bone marrow inflamation Even though the terminology came from common, everyday words, it is now foreign to us because of the language difference. More recent scientific disciplines kept Latin as their common language of discourse. Fortunately for us, computer science did not make the mistake of obscuring its definitions in ancient Latin or Greek. It uses current, common everyday English words like mouse, bugs, chips, etc. (More obscure terms mainly come from abbreviations and acronyms.) However, there is a drawback. For some concepts within our discipline, there may be more than one common word, and the one which will predominate from long historical use remains to be seen. Botanical Generic ← root ------------------ start node ← branch ------------- edge or arc ← branch node ---- internal node Generic Familial Relations predecessor ancestor – parent – father → ← leaf node ---- external node ↑ vertex is another name for node successor descendent – child – son → brothers or siblings

  9. Trees Kinds of Trees – in terms of branching factor (outdegree). Unary Trees – outdegree for every node ≤ 1. (a singly linked list) Binary Trees – outdegree for every node ≤ 2. Ternary Trees – outdegree for every node ≤ 3. . . . m - ary Trees – outdegree for every node ≤ m. m Note: The indegree for every node in a directed tree, except the root, is 1.

  10. . . . . . . . . . . . . . . . . . . . . . . . . . . . h In general: Trees Mathematical Properties of Trees Max no. of Nodes per Level: Level Binary Tree Ternary Tree m-ary Tree 0 1 1 1 3 m 1 2 4 9 2 m2 m3 8 27 3 . . . . . . . . . mh 2h 3h . . . Total max no. of nodes: . . . These formulas may be proved by induction:

  11. Trees Definition The path length of a node in a tree is the no. of edges from the root to that node. path length = 0 path length = 1 height (or depth) = 4 path length = 2 path length = 3 path length = 4 The height or depth of a tree is the max over all the path lengths of its nodes.

  12. Binary Ternary Trees Definition An m-ary tree is regular if every one of its internal nodes has exactly m sons. For each of the following kinds of trees, state which are regular and which are not regular: regular regular regular not regular  not regular regular 

  13. Trees Definition Question: For a regular binary tree of height h, what is the maximum no. of leaves it can have? Answer: 2h Question: What is the max no. of leaves for a regular m-ary tree of height h ? Answer: mh Such trees are called full trees. For example: Full Binary Trees non-Full Binary Trees

  14. m = 2 h = 4 m = 3 h = 5 Trees Definition The min no. of leaves that a regular m-ary tree of height h can have is: 1 + h(m-1) An example, for binary trees: So for a binary tree of height 4, the min no. of leaves for such a tree is: 1 + 4(2-1) = 1 + 4 = 5 An example, for ternary trees: So for a ternary tree of height 5, the min no. of leaves for such a tree is: 1 + 5(3-1) = 1 + 10 = 11

  15. Trees Definition Complete Binary Tree – Such a tree of height h is a full tree up to level h-1 and is filled in from left-to-right at level h. Complete Binary Trees For example: . . . Not Complete

  16. mh+1−1 m −1 = n Similarly, since for full m-ary trees, the solution is easily determined to be: Trees More Mathematical Properties Question: How is the height h related to the total number of nodes, n, in a: 1) full binary tree? 2) full m-ary tree? Recall that the no. of nodes for a full binary tree of height h is: 2h+1−1 So, to get h in terms of n just solve the following equation for h: 2h+1−1 = n 2h+1 = n + 1 h + 1 = log2 (n + 1) h = log2 (n + 1) − 1 h = logm [n(m−1) + 1] − 1 Questions: Will these log functions always return integer values? Why, or why not?

  17. 0 1 1 node h= 0 2 nodes h= 1 3 nodes h= 1 4 nodes h= 2 1 2 1 3 7 nodes h= 2 8 nodes h= 3 5 nodes h=2 6 nodes h= 2 2 4 2 5 2 6 2 7 3 8 ⋮ ⋮ Trees Mathematical Properties Question: How do we determine h in terms of n for complete binary trees? Hint: Derive the formula by looking at the trees below, and filling in and observing the number pattern in the table: Note that the height h increases by 1 at every power of 2, and is simply related as the logarithm of that power of 2: h = log2(n) But the log2 of the intervening integers results in decimal numbers, not integers. How can we denote the truncation of their decimal parts? For complete binary trees: h = ⎿log2(n)⏌ For complete m-ary trees: h = ⎿logm(n)⏌ The pair of delimiters ⎿ ⏌ is called the floor function, and is defined as: ⎿x⏌≡the largest integer ≤ x – e.g.⎿2.135⏌= 2 ⎿2.975⏌= 2 The ceiling function⎾ ⏋ is the complement of the floor function, and is defined as: ⎾ x⏋≡the smallest integer ≥ x – e.g.⎾2.135⏋= 3 ⎾2.975⏋= 3

More Related