Title: Foundation of Computing Systems
1Foundation of Computing Systems
2Tree Example
3Tree Definition
- A tree is a finite set of one or more nodes such
that - (i) there is a specially designated node called
the root - (ii) remaining nodes are partitioned into n (n gt
0) disjoint sets T1, T2, . . ., Tn, where each Ti
(i 1, 2, . . ., n) is a tree T1, T2, . . ., Tn
are called sub trees of the root.
T (A(B(E, F(K, L))), C(G), D(H, I, J))
4Binary Tree
- A binary tree is a special form of a tree
- A binary tree T is a finite set of nodes, such
that - (i) T is empty (called empty binary tree), or
- (ii) T contains a specially designated node
called the root of T, and the remaining nodes of
T form two disjoint binary trees T1 and T2 which
are called left sub-tree and the right sub-trees,
respectively.
5Full Binary Tree
6Complete Binary Tree
7Skewed Binary Tree
8Properties of Binary Trees
- Property 1
- In any binary tree, maximum number of nodes on
level l is 2l, where l 0. - Property 2
- Maximum number of nodes possible in a binary tree
of height h is 2h 1. - Property 3
- Minimum number of nodes possible in a binary tree
of height h is h. - Property 4
- For any non-empty binary tree, if n is the number
of nodes and e is the number of edges, then n e
1.
9Properties of Binary Trees
- Property 5
- For any non-empty binary tree T, if n0 is the
number of leaf nodes (degree 0) and n2 is the
number of internal node (degree 2), then n0
n2 1. - Property 6
- Height of a complete binary tree with n number of
nodes is - Property 7
- Total number of binary tree possible with n nodes
is -
10Representation of Binary Trees
- Linear representation
- Using array
- Linked representation
- Using linked list structure
11Linear Representation of Binary Trees
- The root node is at location 1.
- For any node with index i, 1 lt i n, (for some
n) - (a) PARENT(i)
- For the node when i 1, there is no
parent. - (b) LCHILD(i) 2 i
- If 2 i gt n, then i has no left child.
- (c) RCHILD(i) 2 i 1
- If 2 i 1 gt n, then i has no right child.
12Linked Representation of Binary Trees
13Linked Representation of Binary Trees
14Binary Tree Operations
- Major operations on a binary tree can be listed
as - Insertion.  To include a node into an existing
(may be empty) binary tree. - Deletion.  To delete a node from a non-empty
binary tree. - Traversal.  To visit all the nodes in a binary
tree. - Merge.  To merge two binary trees into a larger
one.
15Binary Tree Insertion
16Binary Tree Deletion
17Binary Tree Traversals
- Depth-first search
- Breadth-first search (level-by search)
- Recursive search
1. R Tl Tr 4. Tr Tl R 2. Tl R Tr 5. Tr R Tl
3. Tl Tr R 6. R Tr Tl
18Binary Tree Traversals
- A B C / E F
F E / C B A
A B C E / F
F / E C B A
A B C E F /
/ F E C B A
19Binary Tree Traversals
R Tl Tr (Preorder) Tl R Tr
(Inorder) Tl Tr R (Postorder)
- A B C / E F
F E / C B A
A B C E / F
F / E C B A
A B C E F /
/ F E C B A
20Binary Tree Traversals
- Preorder traversal
- Visit the root node R.
- Traverse the left sub-tree of R in preorder.
- Traverse the right sub-tree of R in preorder.
- Inorder traversal
- Traverse the left sub-tree of the root node R
is inorder. - Visit the root node R.
- Traverse the right sub-tree of the root node R
is inorder.
- Postorder traversal
- Traverse the left sub-tree of the root R in
postorder - Traverse the right sub-tree of the root R in
postorder - Visit the root node R.
21Binary Tree Merging
22Different Binary Trees
- There are several types of binary trees possible
each with its own properties. - Expression tree
- Binary search tree
- Heap tree
- Threaded binary tree
- Huffman tree
- Height balanced tree (also known as AVL tree)
- Red black tree
- Splay tree
- Decision tree