Upcoming SlideShare
×

Tree

622 views

Published on

Discrete Structure.

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total views
622
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
19
0
Likes
2
Embeds 0
No embeds

No notes for slide

Tree

1. 1. A tree is a finite set of one or more nodes such that:There is a specially designated 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.
2. 2. CrownChurch of England Cabinet House of Commons House of Lords Supreme Court Ministers County Council Metropolitan police Rural District Council County Borough Council
3. 3. Levelnode (13)degree of a node A 1leaf (terminal) 3 1Non terminalparent B C D2 2 2 2 1 2 3childrensibling E F G H I J 2 30 30 31 3 0 30 3 3degree of a tree (3)ancestor K L M 4 0 40 4 0 4level of a nodeheight of a tree (4)
4. 4. ♣ The degree of a node is the number of subtrees of the node ◙ The degree of A is 3; the degree of C is 1.♣ The node with degree 0 is a leaf or terminal node.♣ A node that has subtrees is the parent of the roots of the subtrees.♣ The roots of these subtrees are the children of the node.♣ Children of the same parent are siblings.♣ The ancestors of a node are all the nodes along the path from the root to the node.
5. 5. Property Value A Number of nodes 9 Height 5 B C Root Node A Leaves C,D,F,H,ID E F Interior nodes A,B,E,G Number of levels 5 Ancestors of H G,E,B,A G Descendants of B E,G,H,I Siblings of E GH I
6. 6. ◛Every tree node:  object – useful information  children – pointers to its children nodes OO O O O
7. 7. Two main methods: ► Preorder ► Postorder Recursive definition▓ PREorder: ► visit the root ► traverse in preorder the children (subtrees)▓ POSTorder ► traverse in postorder the children (subtrees) ► visit the root
8. 8. Algorithm preorder(v) “visit” node v for each child w of v do recursively perform preorder(w)In simple language first Root node is traversed then Left child then Right child is traversed. Root Node 1 Left Child 2 3 Right Child 4 5 6 7 8 9 10 11 12 13 14 15
9. 9. Algorithm postorder(v) for each child w of v do recursively perform postorder(w) “visit” node vIn simple language first Left child is traversed then Right child then Root node is traversed. Root Node 1 Left Child 2 3 Right Child 4 5 6 7 8 9 10 11 12 13 14 15
10. 10. ◘ A special class of trees: max degree for each node is 2◘ Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.◘ Any tree can be transformed into binary tree. ◚ By left child-right sibling representation
11. 11. A B E C G DK F H L M I J
12. 12. The maximum number of nodes on level i of a binary tree is 2i- 1, i>=1.The maximum number of nodes in a binary tree of depth k is 2k- 1, k>=1. Prove by induction k 2 i 1 2 k 1 i 1
13. 13. If a complete binary tree with n nodes (depth = log n + 1) isrepresented 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 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.