Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Standard text messaging rates apply

# Lec6

689
views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
689
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
91
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Trees trees binary trees data structures for trees 1
• 2. Trees: Definitions A is the root node. B is parent of D & E. A is ancestor of D & E. D and E are descendants of A. C is the sibling of B D and E are the children of B. D, E, F, G, I are leaves. 2
• 3. Trees: Definitions (2) A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is 3 The degree of node B is 2 level 0 level 1 level 2 level 3 3
• 4. TreesA tree represents ahierarchy, for e.g. theorganization structure of acorporationOr table of contentsof a book 4
• 5. Another ExampleUnix or DOS/Windows file system 5
• 6. Binary Tree An ordered tree is one in which the children of each node are ordered. Binary tree: ordered tree with all nodes having at most 2 children. 6
• 7. Recursive definition of binary treeA binary tree is either a leaf or An internal node (the root) and one/two binary trees (left subtree and/or right subtree). 7
• 8. Examples of Binary Treesarithmetic expressions 8
• 9. Examples of Binary Treesdecision trees 9
• 10. Complete Binary treelevel i has 2i nodesIn a tree of height h leaves are at level h No. of leaves is 2h h No. of internal nodes = 1+2+22+…+2h-1 = 2 -1 No of internal nodes = no of leaves -1 Total no. of nodes is 2h+1-1 = nIn a tree of n nodes No of leaves is (n+1)/2 Height = log2 (no of leaves) 10
• 11. Binary Tree A Binary tree can be obtained from an appropriate complete binary tree by pruning 11
• 12. Minimum height of a binary tree A binary tree of height h has  At most 2i nodes at level i  At most 1+2+22+…+2h = 2h+1-1 nodes If the tree has n nodes then n <= 2h+1-1  Hence h >= log2 (n+1)/2 12
• 13. Maximum height of a binary tree A binary tree on n nodes has height at most n-1 This is obtained when every node (except the leaf) has exactly one child 13
• 14. No of leaves in a binary tree no of leaves <= 1+ no of internal nodes. Proof: by induction on no of internal nodes  Tree with 1 node has a leaf but no internal node.  Assume stmt is true for tree with k-1 internal nodes.  A tree with k internal nodes has k1 internal nodes in left subtree and (k-k1-1) internal nodes in right subtree.  No of leaves <= (k1+1)+(k-k1) = k+1 14
• 15. leaves in a binary tree (2)For a binary tree on n nodes No of leaves + no of internal nodes = n No of leaves <= no of internal nodes + 1 Hence, no of leaves <= (n+1)/2 Minimum no of leaves is 1 15
• 16. ADTs for Trees generic container methods: size(), isEmpty(), elements() positional container methods: positions(), swapElements(p,q), replaceElement(p,e) query methods: isRoot(p), isInternal(p), isExternal(p) accessor methods: root(), parent(p), children(p) update methods: application specific 16
• 17. ADTs for Binary Trees accessor methods: leftChild(p), rightChild(p), sibling(p) update methods:  expandExternal(p), removeAboveExternal(p)  other application specific methods 17
• 18. The Node StructureEach node in the treecontains  key[x] – key  left[x] – pointer to left child  right[x] – pt. to right child  p[x] – pt. to parent node 18
• 19. Representing Rooted TreesBinaryTree: Root  Parent: BinaryTree  LeftChild: BinaryTree   RightChild: BinaryTree           19
• 20. Unbounded BranchingUnboundedTree: Root  Parent: UnboundedTree  LeftChild: UnboundedTree    RightSibling: UnboundedTree          20