Lec 11
Lec 11
AND ALGORITHMS
• A tree T is a set of nodes storing elements such that the nodes have a parent-child
relationship.
• Root: a node with no parent.
• A leaf node has no children.
• Siblings: the children of the same parent.
• Path: A sequence of nodes n1, n2, . . ., nk, such that ni is the parent of ni + 1 for i = 1, 2,. . .,
k–1
• Level: The level of the node refers to its distance from the root.
• The depth of a node is the length of the path (or the number of edges) from the root to
that node.
• The height of a node is the longest path from that node to its leaves.
• The height of a tree is the height of the root.
BASIC TERMS OF A TREE
• TreeNode Implementation
• Tree Implementation
EXAMPLE
LEC 8
Tree
Binary Tree
BASIC TERMS
BINARY TREES
(a) (b)
(c) (d) (e)
TYPES OF BINARY TREE
1. Strict Binary Tree: all the Internal nodes must having two
children.
2. Complete Binary Tree: All the levels are completely filled
except possibly the last level.
3. Full Binary Tree: Simply Every node has 0 or 2 children. A full
binary tree of height h has all its leaves at level h.
BINARY TREE TYPES
BINARY TREE IMPLEMENTATION
TRAVERSING A BINARY TREE
BST Characteristics
1. It is a binary tree.
2. Values of all nodes in its left subtree must be smaller than its root value.
3. Values of all nodes in its right subtree must be greater than its root value.
Example: You have the following sequence of numbers.
45, 36, 76, 23, 89, 115, 98, 39, 41, 56, 69, 48
• 45, 36, 23, 39, 41, 76, 56, 48, 69, 89, 115, 98
• 23, 36, 39, 41, 45, 48, 56, 69, 76, 89, 98, 115
• 23, 41, 39, 36, 48, 69, 56, 98, 115, 89, 76, 45
MOST BST OPERATIONS
• Look up (contains)
• Insert
• remove
LOOKUP STEPS
Case 2