Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
TREES The function of education is to teach one to think intensively and to think critically. Intelligence plus character - that is the goal of true education. Martin Luther King, Jr. Smitha N Pai • Basic terminologies • Binary tree representation • Recursive/ non recursive inorder, preorder and post order tree traversal • Binary search tree • AVL trees. • Applications: • Expression trees • Inserting, deleting, searching, height of BST 8 hrs • Ellis Horowitz, Sartaj Sahni , Anderson, “ Fundamentals of Data Structures in C”, Silicon Press, 2 nd Edition, 2007. Tree terminology • A tree is a finite set of one or more nodes such that • There is a specially designed 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 • A node stands for the item of information and the branches to other nodes • Degree of a node is the number of subtrees of the node • Degree of a tree is the maximum degree of the nodes in the tree • A node that has subtrees is the parent of the roots of the subtrees and the roots of the subtrees are the children of the node • Indegree • Outdegree • Isolated node • Directed tree –one node with indegree 0 and other nodes with indegree 1 • Internal nodes • External nodes • Directed graph • Complete binary tree • Almost complete binary tree Tree terminology(contd.) • Ancestor of a node are all the nodes along the path from the root to the node • Descendants of a node are all the nodes that are in its subtrees • Root is at level one • Subsequent nodes have the level one level of the node’s parent plus one • Height or depth of the tree is the maximum level of any node in the tree Representation of Trees • List Representation • Draw the tree having the list representation of (A(B(E(K,L),F),C(G),D(H(M),I,J))) Data Link1 Link2 … Linkn • Left Child-Right Sibling Representation Data Left Child Right Sibling • Representation as a degree two tree (Left child-Right Child tree) Terminology of binary Trees • A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary tree called the left subtree and the right subtree. • It is a finite set of elements that is either empty or is partitioned into three disjoint subsets- root, left and right subtrees –both of which can be empty • Each element of a binary tree is called a node of the tree • Nonleaf nodes are internal nodes and leaves external • Father, left/ right son • Skewed tree is one in which all nodes in the tree are on to the left or to the right • Difference between tree and binary tree? • Degree two • Left and right subtree • Tree have zero nodes • A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k Properties of binary trees • Lemma: Maximum number of nodes • The maximum number of nodes on level i of a binary tree is 2 i-1 , i>=1 • Proof: • Induction base: The root is the only node on level i=1. Hence, maximum number of nodes on level i= 1 is 2 i-1 = 2 0 =1 • Induction hypothesis: • For all j, 1<=j<I, the maximum number of nodes on level j is 2 j-1 • Induction step: The maximum number of nodes on level i-1 is 2 i-2 by induction hypothesis. Since each node in a binary tree has a maximum degree of 2, the maximum number of nodes on level i is two times the maximum number of nodes on level i-1 on 2 i-1 The maximum number of nodes in a binary tree of depth k is 𝒌 𝒌 𝒊 − 𝟏 =2k - 1 , k>=1 𝒎𝒂𝒙 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒏𝒐𝒅𝒆𝒔 𝒐𝒏 𝒍𝒆𝒗𝒆𝒍 𝒊 = 𝟐 𝒊=𝟏 𝒊=𝟏 Properties of binary tree • Lemma: Relation between number of leaf nodes and nodes of degree 2 • For any nonempty binary tree, T, if n 0 is the number of leaf nodes and n 2 the number of nodes of degree 2, then n 0=n2+1 • Proof: • Let n1 be the number of nodes of degree one and n the total number of nodes. Since all in T are of degree at most two, we have • n= n0+n1+n2 (1) • If we count the number of branches in a binary tree, we see that every node except the root has a branch into it • If B is the number of branches then n=B+1. • All branches stem from a node of degree one or two. Thus B=n 1+2n2. • We obtain n=1+n 1+2n2 (2) • Subtracting (2) from (1) and rearranging terms, we get • n0=n2+1 Binary tree representations • A full binary tree of depth k is a binary tree of depth k having 2 k -1 nodes, K>=0. • A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. • If a complete binary tree with n nodes • (depth = log 2𝑛 + 1 is represented sequentially then for any node with index i, 1<=i<=n, we have • Parent(i) is at 𝑖/2 𝑖𝑓 𝑖 ≠ 1. 𝐼𝑓 𝑖 = 1, 𝑎𝑡 𝑡ℎ𝑒 𝑟𝑜𝑜𝑡 𝑎𝑛𝑑 ℎ𝑎𝑠 𝑛𝑜 𝑝𝑎𝑟𝑒𝑛𝑡 • Leftchild (i) is at 2i if 2i<=n. If 2i<n , then i has no left child • Rightchild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n, then i has no right child Storage representation of binary trees: • Trees can be represented using sequential allocation techniques(arrays) or by dynamically allocating memory. • In 2nd technique, node has 3 fields 1. Info : which contains actual information. 2. Llink :contains address of left subtree. 3. Rlink :contains address of right subtree. struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node *NODEPTR; Expression tree • A node representing an operator is a non leaf whereas a node representing an operand is a leaf • Postorder traversal places an operator after its two operands – produces postfix form of expression • Preorder traversal –Prefix expression • Inorder traversal–Infix expression Binary Search Tree • A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: • Every element has a key, and no two elements have the same key, that is, the keys are unique • The keys in a non empty left sub tree must be smaller than the key in the root of the sub tree • The keys in a non empty right sub tree must be larger than the key in the root of the sub tree • The left and right sub trees are also binary search tree. Balanced trees(AVL trees) –(Adleson-Velskii and Landis) • The balanced trees where height is used as a criterion for balancing is height balanced trees • An empty binary tree is height balanced • If T is a nonempty binary tree with T L and TR as its left and right subtrees, then T is height balanced if • TL and TR are height balanced • |hL –hR| <=1 where hL and hR are height of TL and TR resp. • Weight balanced are the ones where the number of external nodes form the basis • Balance factor of a node, T in a binary tree is defined as hL-hR where hL and hR are respectively, the heights of the left and right sub trees of T. For any node T in an AVK tree BF(T)= -1, 0 or 1.