Trees   trees   binary trees   data structures for trees                                1
Trees: Definitions   A is the root node.   B is parent of D & E.   A is ancestor of D & E.   D and E are    descendant...
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 degre...
TreesA tree represents ahierarchy, for e.g. theorganization structure of acorporationOr table of contentsof a book        ...
Another ExampleUnix or DOS/Windows file system                                  5
Binary Tree   An ordered tree is one in which the children of    each node are ordered.   Binary tree: ordered tree with...
Recursive definition of binary treeA binary tree is either a   leaf or   An internal node (the root) and one/two binary ...
Examples of Binary Treesarithmetic expressions                           8
Examples of Binary Treesdecision trees                           9
Complete Binary treelevel i has 2i nodesIn a tree of height h   leaves are at level h   No. of leaves is 2h           ...
Binary Tree   A Binary tree can be obtained from an    appropriate complete binary tree by    pruning                    ...
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...
Maximum height of a binary tree A binary tree on n nodes has height at  most n-1 This is obtained when every node (excep...
No of leaves in a binary tree no of leaves <= 1+ no of internal nodes. Proof: by induction on no of internal nodes     ...
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 in...
ADTs for Trees   generic container methods: size(), isEmpty(),    elements()   positional container methods: positions()...
ADTs for Binary Trees accessor methods: leftChild(p),  rightChild(p), sibling(p) update methods:     expandExternal(p),...
The Node StructureEach node in the treecontains   key[x]  – key   left[x] – pointer to left    child   right[x] – pt. t...
Representing Rooted TreesBinaryTree:                        Root   Parent: BinaryTree   LeftChild: BinaryTree           ...
Unbounded BranchingUnboundedTree:                                  Root   Parent: UnboundedTree   LeftChild: UnboundedTr...
Upcoming SlideShare
Loading in...5
×

Lec6

726

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
726
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
98
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lec6

  1. 1. Trees trees binary trees data structures for trees 1
  2. 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. 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. 4. TreesA tree represents ahierarchy, for e.g. theorganization structure of acorporationOr table of contentsof a book 4
  5. 5. Another ExampleUnix or DOS/Windows file system 5
  6. 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. 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. 8. Examples of Binary Treesarithmetic expressions 8
  9. 9. Examples of Binary Treesdecision trees 9
  10. 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. 11. Binary Tree A Binary tree can be obtained from an appropriate complete binary tree by pruning 11
  12. 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. 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. 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. 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. 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. 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. 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. 19. Representing Rooted TreesBinaryTree: Root  Parent: BinaryTree  LeftChild: BinaryTree   RightChild: BinaryTree           19
  20. 20. Unbounded BranchingUnboundedTree: Root  Parent: UnboundedTree  LeftChild: UnboundedTree    RightSibling: UnboundedTree          20
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×