Presentation is loading. Please wait.

Presentation is loading. Please wait.

B.Ramamurthy Chapter 9 CSE116A,B

Similar presentations


Presentation on theme: "B.Ramamurthy Chapter 9 CSE116A,B"— Presentation transcript:

1 B.Ramamurthy Chapter 9 CSE116A,B
Trees B.Ramamurthy Chapter 9 CSE116A,B 5/2/2019 B. Ramamurthy

2 Introduction Data organizations we studied are linear in that items are one after another. Data organization can also be hierarchical whereby an item can have more than one immediate successor. Thus an ADT can be classified as linear and non-linear. We will study non-linear ADTs tree and binary trees in this discussion. 5/2/2019 B. Ramamurthy

3 Topics for discussion Terminology
Tree and Binary Tree -- Formal definition Tree Traversal Full binary tree Complete binary tree Balanced Tree Summary 5/2/2019 B. Ramamurthy

4 Tree : Formal Definition
A tree T is a finite, non-empty set of nodes, T = {r} U T1 U T2 U T3… U Tn with the following properties: 1. A designated node of the set r, is called the root of the tree; 2. The remaining nodes are partitioned into n >= 0 subsets T1, T2, .. Tn each of which is a tree. 5/2/2019 B. Ramamurthy

5 Binary Tree : formal definition
A binary tree is a set T of nodes such that either T is empty, or T is parameterized into three disjoint sets: A single node r, the root Two possibly empty sets that are binary trees called left and right subtrees of r. Example : Binary trees to represent algebraic expressions. 5/2/2019 B. Ramamurthy

6 Terminology Trees are used to represent relationships: items in a tree are referred to as nodes and the lines connecting the nodes that express the hierarchical relationship are referred to as edges. The edges in a tree are directed. Trees are hierarchical which means that a parent-child relationship exist between the nodes in the tree. Each node has at most one parent. The node with no parents is the root node. The nodes that have no successors (no children nodes) are known as leaf nodes. 5/2/2019 B. Ramamurthy

7 More Definitions The degree of a node is the number of subtrees associated with that node. A node of degree 0 is the leaf node. In a binary tree the degree of each node except the leaf nodes is 2. Level of a node of tree is measured from its root, whereas height of a node is measured from its root to the leaf. Height of a leaf node is 0. 5/2/2019 B. Ramamurthy

8 Basic Tree Anatomy root Level 0 Level 1 Internal node Level 2 leaf
5/2/2019 B. Ramamurthy

9 N-nary Tree An n-nary tree T is a finite set of nodes with the following properties: 1. Either the set id empty or 2. The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into N>= 0 sunbsets T0, T1, .. TN-1, each of which is an N-ary tree such that T = {R, T0, T1, .. TN-1}. 5/2/2019 B. Ramamurthy

10 Observation An important concept used in the above definition is recursion. A tree is defined in terms of smaller trees. This concept of defining a term by smaller of the kind is called recursive definition. We will discuss recursion next class. 5/2/2019 B. Ramamurthy

11 External and Internal Nodes
Theorem 9.1 (in your text) : An N-ary tree with n >= 0 internal nodes contains (N-1)(n+1) external nodes. Theorem 9.2 : Consider an N-ary tree T of height h >= 0. The maximum number of internal nodes in T is given by (h+1) - 1 N N - 1 5/2/2019 B. Ramamurthy

12 Leaf nodes Theorem 9.3 : Consider an N-ary tree of height h >= 0. The maximum number of leaf nodes in T is N h 5/2/2019 B. Ramamurthy

13 Binary Tree A binary tree T is a finite set of nodes with the following properties: 1. Either the set is empty, T = O. or 2. The set consists of a root, r, and exactly two distinct binary trees T L and TR T = {r, TR, TL}. TL is called the left subtree and TR is called the right subtree. 5/2/2019 B. Ramamurthy

14 Binary tree : example A - B / C A \ B C How do you traverse this tree?
5/2/2019 B. Ramamurthy

15 Recursive Solutions Recursion is an important problem solving approach that is an alternative to iteration. These are questions to answer when using recursive solution: 1. How can you define the problem in terms of a smaller problem of the same type? 2. How does each recursive call diminish the size of the problem? 3. What instance of the problem can serve as the base case? 4. As the problem size diminishes, will you reach this base case? Ramamurthy

16 Example 1: Factorial of N
Iterative definition: Factorial(N) = N * (N-1) * (N-2) *…1 for any N > 0. Factorial(0) = 1 Recursive solution: 1. Factorial(N) = N * Factorial(N-1) 2. Problem diminishing? yes. 3. Base case: Factorial(0) = 1; base case does not have a recursive call. 4. Can reach base case as problem diminishes? yes Ramamurthy

17 Writing String Backwards
1. Write a string of length N backwards in terms of writing a string of length (N-1) backwards. 2. Base case : Choice 1: Writing empty string. Choice 2: Writing a string of length 1. Ramamurthy

18 WriteBackward(S); if string is empty
do nothing; // this is the base case else { write the last character of S; WriteBackward(S - minus its last character;} Ramamurthy

19 Defining Languages A grammar states the rules of a language.
A recursive algorithm can be written based on this grammar that determines whether a given string is a member of the language ==> recognition algorithm. For expressing a grammar we use special symbols: X | Y means X or Y X.Y or simply X Y means X followed by Y <xyz> an instance of entity xyz 5/2/2019 B. Ramamurthy

20 A grammar for Java identifiers
<identifier> = <letter> | <identifier> <letter> | <identifier><digit> <letter> = a | b …|z| A | B | C…|Z|_ <digit> = 0 | 1 | …|9 5/2/2019 B. Ramamurthy

21 Recognition algorithm for identifiers
boolean IsId (w) { // Returns TRUE if w is a legal Java identifier, // Otherwise returns FALSE if (w is of length 1) if (w is a letter) return true; else return false; else if (last char of w is a letter or a digit) return IsId(w minus its last char); else return false; } 5/2/2019 B. Ramamurthy

22 Tree Traversals Preorder, inorder and post order.
Pre, In and Post refer to the order in which root is traversed. PreOder traversal: Visit root, traverse TL, Traverse TR. InOrder traversal : Traverse TL, visit root, traverse TR. PostOrder traversal: Traverse TL, Traverse TR, and visit root. 5/2/2019 B. Ramamurthy

23 Expression Trees + / * A B -- E D C 5/2/2019 B. Ramamurthy

24 Implementing a Binary Tree
Examine Java API Use the existing classes. Extend existing classes. Implement necessary interfaces. 5/2/2019 B. Ramamurthy

25 Binary Search Tree A binary search tree is a binary tree that is in a sense sorted according to the values in its nodes. For each node n, a binary search tree satisfies the following three properties: 1. n’s value is greater than all values in its left subtree TL. 2. n’s value is less than all its values in its right subtree TR. 3. Both TL and TR are binary search tree. 5/2/2019 B. Ramamurthy

26 Example : Tree of names Jane Bob Tom Ellen Alan Wendy Nancy
Where will you insert the name Randy? How about Ian? 5/2/2019 B. Ramamurthy


Download ppt "B.Ramamurthy Chapter 9 CSE116A,B"

Similar presentations


Ads by Google