Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree.

Similar presentations


Presentation on theme: "Tree."— Presentation transcript:

1 Tree

2 Overview Tree is a non-linear data structure
Collection of nodes or Finite set of nodes This collection can be empty A graph which does not contain any cycle, so there is exactly one path from the root to each node in a tree

3 Terminology Nodes are vertices and Edges are connections between nodes
Root is top most node or base of tree where from tree starts Parent, Child, Siblings (children of same parent), Grand parent, Grand child Ancestor – a node A is said to be ancestor of B if A is either parent of B or parent of some ancestor of B Descendent – a node B is said to be descendent of A if B is either child of A or child of some other descendent of A Every node except the root has one parent Sub-tree – Part of tree Degree of a node is number of nodes connected to it Leaf node – node which does not have any child Internal node – out degree>0 Size: The number of nodes in a tree.

4 Example

5 Tree levels

6 Height and Depth Height of a node is the length of a longest path from this node to a leaf All leaves are at height zero Height of a tree is the height of its root (maximum level) Depth of a node is the length of path from root to this node Root is at depth zero Depth of a tree is the depth of its deepest leaf that is equal to the height of this tree

7 Height and Depth

8 Binary Tree Tree with no node of out degree greater than two
Out degree of any node can be 0,1 or 2 Any node can have at most two children

9 Binary Tree

10 Binary Tree (Cont…) Not a binary tree Binary tree

11 Problem The following sequence of operation is performed on stack : push(1),push(2),pop,push(1),push(2),pop,pop,pop,push(2),pop. The sequence of popped out values are ? [A] 2,2,1,1,2 [B] 2,2,1,2,2 [C] 2,1,2,2,1 [D] 2,1,2,2,2

12 Binary Tree (Cont…) Complete binary tree Full binary tree

13 Full Binary Tree A full binary tree is a binary tree in which every node other than the leaves has two children. This is also called strictly binary tree or proper binary tree or 2-tree. Is this Full Binary tree Is this Full Binary tree Is this Full Binary tree NO YES YES

14 Some Important Facts Let T be a nonempty, full binary tree Then:
If T has I internal nodes, the number of leaves is L = I + 1. (b) If T has I internal nodes, the total number of nodes is N = 2I + 1. (c) If T has a total of N nodes, the number of internal nodes is I = (N – 1)/2. (d) If T has a total of N nodes, the number of leaves is L = (N + 1)/2. (e) If T has L leaves, the total number of nodes is N = 2L – 1. (f) If T has L leaves, the number of internal nodes is I = L – 1. Basically, this theorem says that the number of nodes N, the number of leaves L, and the number of internal nodes I are related in such a way that if you know any one of them, you can determine the other two.

15 Some Important Facts 1.Maximum number of leaves in the full binary tree of height h = 2h and 2h – 1 internal nodes 2. Total no. of nodes =Total number of leaves + Total number of internal nodes =2h+1 – 1 3. Height of tree if no. of nodes are given = log2n

16 Complete Binary Tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Is this complete Binary tree Is this complete Binary tree Is this complete Binary tree YES YES NO

17 Complete Binary Tree Is this complete Binary tree
YES But this binary tree is complete. It has only one node, the root. YES It is called the empty tree, and it has no nodes, not even a root. YES

18 More Examples Is this Full or complete Is this Full or complete
Complete but not full Neither Full nor complete Full but Not Complete Perfect binary tree :- Every node except the leaf nodes have two children and every level (last level too) is completely filled.

19 Skewed tree Left Skewed Tree Right Skewed Tree A A B B C C D D E E
Height = N – 1

20 Representation of binary tree
Tree can be represented in two ways – Array Linked list Array Representation – Three arrays are to be maintained. 1 2 3 4 5 6 Data A B C D E F G A C B F E D G LC 1 3 4 -1 RC 2 -1 5 6 Note: Parenthetical representation A(B(D( G) )C(EF))

21 Representation of binary tree (Cont…)
Linked List Representation – struct node{ struct node *lc; int data; struct node *rc; }; A C B F E D G A B C D E F G

22 Traversal Traversal is the process of visiting every node once exactly once Three recursive techniques for binary tree traversal Pre order In order Post order Visit the root Traverse left subtree Traverse right subtree Traverse left subtree Visit the root Traverse right subtree Traverse left subtree Traverse right subtree Visit the root

23 PreOrder (NLR) Algorithm Preorder (root): Traverse a binary tree in node-left-right sequence. If (root is not null) process (root) Preorder (leftSubtree) Preorder (rightSubtree) 1. N (node) 2. L (left) 3. R (right) Preorder – A B C D E F

24 InOrder (LNR) Algorithm Inorder (root): Traverse a binary tree in left-node-right sequence. If (root is not null) Inorder (leftSubtree) process (root) Inorder (rightSubtree) 1. N (node) 2. L (left) 3. R (right) Inorder – C B D A E F

25 PostOrder (LRN) Algorithm Postorder (root): Traverse a binary tree in left-right-node sequence. If (root is not null) Postorder (leftSubtree) Postorder (rightSubtree) process (root) 1. N (node) 2. L (left) 3. R (right) Postorder – C D B F E A

26 Expression Tree An expression tree is a binary tree with the following properties: Each leaf is an operand The root and internal nodes are operators Sub trees are sub expressions, with the root being an operator Infix traversal Prefix traversal Postfix traversal Example – Infix expression A + (B x C) - D Infix expression tree - x + A D B C

27 Infix traversal Algorithm Infix(tree): Print infix expression for a given expression tree If (tree is not empty) If (token is operand) print token else print left parenthesis Infix (leftSubtree) Infix (rightSubtree) print right parenthesis Infix expression ((A + (B x C)) – D) - x + A D B C

28 Prefix traversal Algorithm Prefix(tree): Print prefix expression for a given expression tree If (tree is not empty) print token Prefix (leftSubtree) Prefix (rightSubtree) Prefix expression - + A x B C D - x + A D B C

29 Postfix traversal Algorithm Postfix(tree): Print postfix expression for a given expression tree If (tree is not empty) Postfix (leftSubtree) Postfix (rightSubtree) print token Postfix expression A B C x + D - - x + A D B C

30 Problem Level of a node is distance from root to that node. For example, level of root is 1 and levels of left and right children of root is 2. The maximum number of nodes on level i of a binary tree is (A) 2^(i-1) (B) 2^i (C) 2^(i+1) (D) 2^[(i+1)/2] Answer: (A) The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is: (A) 2^h -1 (B) 2^(h-1) – 1 (C) 2^(h+1) -1 (D) 2*(h+1) Answer: (C) 

31 Problem In a binary tree with n nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree that have exactly one child? (A) 0 (B) 1 (C) (n − 1) /2 (D) n-1 Answer: (A) The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum number of nodes in a binary tree of height 5 are (A) 63 and 6, respectively (B) 64 and 5, respectively (C) 32 and 6, respectively (D) Can not determine Answer: (A) 

32 Problem Which of the following is a true about Binary Trees (A) Every binary tree is either complete or full. (B) Every complete binary tree is also a full binary tree. (C) Every full binary tree is also a complete binary tree. (D) No binary tree is both complete and full. (E) None of the above Answer: (E) A binary tree T has 20 leaves. The number of nodes in T having two children is (A) 18 (B) 19 (C) 17 (D) Any number between 10 and 20 Answer: (B) 

33 Binary Search Tree (BST)
Binary tree with following properties – Every element has a unique key. The left sub tree of a node contains only nodes with keys less than the node's key. The right sub tree of a node contains only nodes with keys greater than the node's key. The left and right sub trees are also binary search trees. Binary Search Trees (BST) are a type of binary trees with a special organization of data This data organization leads to O(log n) complexity for searches, insertions and deletions in certain types of the BST (balanced trees). But for worst case it would be O(n). For getting all advantages of BST, it should be AVL or height balance tree.

34 BST Examples 45 54 33 56 47 22 25 G J E K H B D A

35 BST Search Algorithm Search_BST(T , Key, LOC, PAR): This algorithm searches Key in tree T. This procedure finds the location LOC of Key in tree T. ROOT is variable pointing to root of tree T. This algorithm also finds the parent location PAR of Key , Initially LOC =NULL and PAR=NULL. If ROOT == NULL LOC = NULL PAR = NULL Exit If ROOT->Data == Key LOC = ROOT Temp = ROOT

36 While Temp !=NULL PAR = Temp If Temp->Data > Key Temp = Temp ->lc Else Temp = Temp ->rc If Temp->Data == Key LOC = Temp BREAK If LOC == NULL Print Search Unsuccessful.

37 BST Insertion Algorithm Insert_BST(T, Key): This algorithm inserts Key in tree T at appropriate location. ROOT is variable pointing to root of tree T. This algorithm uses sub algorithm Search_BST to find the Key location LOC and parent location PAR of Key. Call Search_BST(T, Key, LOC, PAR) Node Temp If LOC != NULL Exit Temp = Allocate memory Temp ->Data = Key Temp-> lc = NULL Temp -> rc = NULL If PAR == NULL ROOT = Temp Else If PAR->Data > Key PAR->lc = Temp Else PAR->rc = Temp

38 Problem The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)? (GATE CS 2004) (A) 2 (B) 3 (C) 4 (D) 6 Answer: (B) The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree? (A) 10, 20, 15, 23, 25, 35, 42, 39, 30 (B) 15, 10, 25, 23, 20, 42, 35, 39, 30 (C) 15, 20, 10, 23, 25, 42, 35, 39, 30 (D) 15, 10, 23, 25, 20, 35, 42, 39, 30 Answer: (D) 

39 BST Deletion We have basically 3 case in deletion –
Let we want to delete a node X from tree, Case 1: X has no children, then delete X from tree and make its location in parent node null Case 2: X has exactly one children, then X will be deleted by replacing the location of X in its parent node by the location of its only child i.e. X only child Case 3: X has two children, then find in order successor of X and delete X from tree by replacing it from its in order successor. Maintain the BST properties accordingly.

40 Case A Algorithm CaseA_delete(T, Key): This algorithm deletes a node X with Key from tree, if this node is not having two children. ROOT is variable pointing to root of tree T. This algorithm uses sub algorithm Search_BST to find the Key location LOC and parent location PAR of Key. CHILD is a pointer variable pointing to child of node X. Call Search_BST(T, Key, LOC, PAR) If LOC == NULL Exit If LOC ->lc ==NULL CHILD = LOC ->rc Else if LOC ->rc ==NULL CHILD = LOC ->lc Else CHILD = NULL If PAR != NULL If LOC == PAR->lc PAR ->lc = CHILD PAR ->rc = CHILD ROOT = CHILD

41 Case B Algorithm CaseB_delete(T, Key): This algorithm deletes a node X with Key from tree, if this node is having two children. ROOT is variable pointing to root of tree T. This algorithm uses sub algorithm Search_BST to find the Key location LOC and parent location PAR of Key. SUC is a pointer variable pointing in order successor of node X. PARSUC is a pointer variable pointing to the parent of in order successor of node X. Call Search_BST(T, Key, LOC, PAR) Node Temp Temp = LOC ->rc While Temp->lc != NULL Temp = Temp->lc SUC = Temp CaseA_delete(T, SUC->Data)

42 If PAR !=NULL If LOC == PAR->lc PAR->lc = SUC Else PAR->rc = SUC ROOT = SUC SUC->lc = LOC->lc SUC ->rc = LOC->rc

43 BST deletion (Cont…) Algorithm Delete_BST(T, Key): This algorithm deletes a node X with Key from tree, if this node is having two children. ROOT is variable pointing to root of tree T. This algorithm uses sub algorithm Search_BST to find the Key location LOC and parent location PAR of Key. Call Search_BST(T, Key, LOC, PAR) If LOC == NULL Exit If LOC ->lc !=NULL and LOC ->rc !=NULL Call CaseB_delete(T,Key) Else Call CaseA_delete(T,Key) Free LOC

44 Extended Binary Tree (B) (C) (A) 4 6 7 9 6 7 9 4 7 9 4 6
(A) Path length of External nodes = 4*2 + 7*3 + 9*3 + 6*1 = 62 (B) Path length of External nodes = 6*2 + 4*2 + 9*2 + 7*2 = 52 (C) Path length of External nodes = 7*2 + 4*3 + 6*3 + 9*1 = 53

45 Problem Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree? (A)  (B)  (C)  (D)  Answer: (C) In-order traversal of a BST gives elements in increasing order. So answer c is correct. What are the worst-case complexities of insertion and deletion of a key in a binary search tree? (A) Θ(logn) for both insertion and deletion (B) Θ(n) for both insertion and deletion (C) Θ(n) for insertion and Θ(logn) for deletion (D) Θ(logn) for insertion and Θ(n) for deletion Answer: (B)  The time taken by search, insert and delete on a BST is always proportional to height of BST. Height may become O(n) in worst case.

46 Huffman Tree To find out unique extended binary tree with minimum weighted path length huffman algorithm is proposed. Algorithm – Consider that there are n weights W1, W2, ….., Wn are given Take two minimum weights and create a sub tree, assume minimum weights are W1 and W2 Now remaining weights will be W1 + W2, W3 …….. Wn Create all sub trees till last weight Important Facts: It is an example of a greedy algorithm: It's called greedy because the two smallest nodes are chosen at each step, and this local decision results in a globally optimal encoding tree. Huffman tree is an optimal tree: there are no other trees with the same characters that use fewer bits to encode the same string.

47 Huffman Tree Let following information is given – A B C D E F G 16 11
7 20 25 5 100 57 43 23 12 32 20 11 5 7 16 25

48 Application Huffman tree is used to code the text(message) if we want to send a message on network. So by encoding message compression is achieved. Example – Data size = 100 symbol For each symbol(Fixed) = 10 bits, Total bits = 100*10 = 1000 Huffman coding is used to assign variable size code for symbol. In huffman coding, less size code is assign to frequently occurring symbols and greater size code is assign to rarely occurring symbols. Huffman codes are prefix free codes (prefix codes) i.e. the codes (bit sequences) are assigned in such a way that the code assigned to one character is not prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the generated bit stream.

49 Example 1 1 A – 5 B – 10 C – 15 D – 20 E – 25 1 1 Fixed Code Size
Variable Code Size A B C D E 000 001 010 011 100 A B C D E 000 001 01 10 11 1 1 A – 5 B – 10 C – 15 D – 20 E – 25 1 E C D 1 A B

50 Exercise “the quick brown fox jumps over the lazy dog”
Draw the huffman tree and find the codes for the following statements – “the quick brown fox jumps over the lazy dog” Eerie eye seen near the lake A cat may look at a king Fortune favours the brave

51 Threaded Binary Tree There are some issues associated with traversals of binary tree – Extra amount of space is needed to store the stack during recursive traversal(How much space?) b. If number of nodes are n in the tree then n+1 would be null pointers c. Difficult to find the successor of any node Therefore, to overcome these problems, we can store some useful information in NULL pointers. This useful information could be successor or predecessor of that node depend on the type of traversal. These special pointers with this information are known as threads and such binary tree is called as threaded binary tree. it uses stack space proportional to the height of a tree. If the tree is fairly balanced, this amounts to O(log n) space for a tree containing n elements. In the worst case, when the tree takes the form of a chain, the height of the tree is n so the algorithm takes O(n) space.

52 Threaded Binary Tree (Cont…)
Classification – Left threaded : If we store predecessor information in NULL left pointers only Right threaded: If we store successor information in NULL right pointers only Fully threader or simply threader : Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor respectively. Types – Preorder Threaded Binary Trees : we store preorder predecessor information and preorder successor information to left and right respectively. Inorder Threaded Binary Trees : we store inorder predecessor information and inorder successor information to left and right respectively. Postorder Threaded Binary Trees : we store postorder predecessor information and postorder successor information to left and right respectively.

53 Threaded Binary Tree (Cont…)
Fig:A threaded tree, with the special threading links shown by dashed arrows

54 Problem 1. A networking company uses a compression technique to encode the message before transmitting over the network. Suppose the message contains the following characters with their frequency: character   Frequency     a           5     b           9     c           12     d           13     e           16     f           45 If the compression technique used is Huffman Coding, how many bits will be saved in the message? (A) 224 (B) 800 (C) 576 (D) 324 Answer: (C) Total number of characters in the message = 100. Each character takes 1 byte. So total number of bits needed = 800. After Huffman Coding, the characters can be represented with: f: 0, c: 100, d: 101, a: 1100, b: 1101, e: 111 Total number of bits needed = 224 Hence, number of bits saved = = 576

55 Problem In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation? (A) Inorder Successor is always a leaf node (B) Inorder successor is always either a leaf node or a node with empty left child (C) Inorder successor may be an ancestor of the node (D) Inorder successor is always either a leaf node or a node with empty right child Answer: (B)

56 General Tree A general tree is a tree where each node may have zero or more children (a binary tree is a specialized case of a general tree). General trees are used to model applications such as file systems.

57 General tree to binary Tree
This conversion also known as left-child, right-sibling binary conversion. Rule-1: Binary tree left child = leftmost child Rule-2: Binary tree right child = right sibling

58 Forest A forest is a collection of one or more general trees.

59 Problem 1. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is: (A) d e b f g c a (B) e d b g f c a (C) e d b f g c a (D) d e f g b c a Answer: (C)

60 Height Balance Tree The shorter the tree, the easier it is to locate any desired node in the tree Consider a height-balancing scheme where following conditions should be checked to determine if a binary tree is balanced. An empty tree is height-balanced. A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree (Balance Factor) is not more than 1. 1 A C B F E D G Balance Factor B = HL – HR Where HL is the height of the left subtree and HR is the height of the right subtree. 2 -1

61 Height Balanced binary tree
The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.

62 AVL(Adelson – Velskii – Landis) tree
An AVL tree is a binary search tree in which for every node in the tree, the height of the left and right subtrees differ by at most 1.

63 5 Not AVL Tree 3 1 4 5 3 8 1 4 10 AVL Tree

64 Operations in AVL Tree O(log N) Searching, Complexity
FindMin, Complexity O(log N) Deletion O(log N) Insertion O(log N)

65 Insertion

66

67

68

69

70

71 LL Rotation

72 U V X Y Z 5 0.8 5 3 1 4 8 3 1 8 4 AVL Tree 3 Insert 0.8 5 1 0.8 After Rotation 4 8

73 RR Rotation


Download ppt "Tree."

Similar presentations


Ads by Google