COMPLETE BINARY TREE The tree is said to be complete binary tree if all its levels, except possibly the last, have the maximum number of possible nodes and if all the nodes at last level appear as far left as possible.Maximum number of nodes at any level r =2rEq:- at level 0 maximum number of nodes=2º=1At level 2 maximum number of nodes=2²=4 and so on.. 12
COMPLETE BINARY TREESuppose given a complete binary tree having 26 nodes. We are labeling the nodes from 1 to 26.Left child of node k=2*KRight child of node k=2*K+1Parent of node K=[K/2]Depth of complete binary tree Tn with n nodeDn= [log2n +1] 13
FULL BINARY TREE VS COMPLETE BINARY TREE 14
FULL VS. COMPLETE BINARY TREE 15
Full BT vs. Complete BT A full binary tree of depth k is a binary tree of depth k having 2 k -1 nodes, k >=1. A binary tree with n nodes and depth k is complete if its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k . A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree
Complete Binary Tree If a complete binary tree with n nodes (depth=└log n + 1┘) is represented sequentially, then for any node with index i , 1<= i <= n , we have: parent ( i ) is at └i /2┘ if i !=1. If i =1, i is at the root and has no parent. leftChild ( i ) is at 2 i if 2 i <= n . If 2 i >n, then i has no left child. rightChild ( i ) is at 2 i +1 if 2 i +1<= n . If 2 i +1>n,
EXTENDED BINARY TREE •An extended binary tree is a transformation of any binary tree into a complete binary tree. This transformation consists of replacing every null subtree of the original tree with ‘special nodes.' •The nodes from the original tree are then internal nodes, while the ``special nodes'' are external nodes.•For instance, consider the following binary tree in fig(a). Fig (b) is the extended binary tree. •Empty circles represent internal nodes, and filled circles represent external nodes. Every internal node in the extended tree has exactly two children, and every external node is a leaf. The result is a complete binary tree. 18
Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2 i , i>=0. The maximum number of nodes in a binary tree of depth k is 2 k -1 , k>=1.
EXAMPLE a b 20
REPRESENTATION OF BINARY TREES IN MEMORY . Array Representation Linked Representation 21
ARRAY REPRESENTATION . • Tree Nodes are stored in the array. • Root node is stored at index ‘0’ • If a node is at a location ‘i’, then its left child is located at 2 * i + 1 right child is located at 2 * i + 2 • Root node is stored at index ‘1’ • If a node is at a location ‘i’, then its left child is located at 2 * i right child is located at 2 * i + 1 • This storage is efficient when it is a complete binary tree, because a lot of memory is wasted. 22
EXAMPLE 1 23
EXAMPLE 2 Disadvantages (1) waste space (2) insertion/deletion problem 24
LINKED REPRESENTATION • If a binary tree is not complete , a better choice for storing it is to use a linked representation. • It uses three array INFO, RIGHT, LEFT, and a pointer variable ROOT as follows.
You've reached the end of your free preview.
Want to read all 60 pages?