Lecture 07
Lecture 07
(COMP4120)
Lecture # 7
Binary Tree
Course Instructor
Dr. Aftab Akram
PhD CS
Assistant Professor
Department of Information Sciences, Division of Science &
Technology
University of Education, Lahore
Definitions and Properties
• A binary tree is made up of a finite set of elements
called nodes.
• This set either is empty or consists of a node called
the root together with two binary trees, called the
left and right subtrees, which are disjoint from each
other and from the root.
• Disjoint means that they have no nodes in
common.
• The roots of these subtrees are children of the root.
• There is an edge from a node to each of its
children, and a node is said to be the parent of its
children.
Definitions and Properties
• If is a sequence of nodes in the tree such
that is the parent of for , then this
sequence is called a path from to
• The length of the path is .
• If there is a path from node R to node M, then R is an
ancestor of M, and M is a descendant of R.
• Thus, all nodes in the tree are descendants of the root
of the tree, while the root is the ancestor of all nodes.
• The depth of a node M in the tree is the length of the
path from the root of the tree to M.
• The height of a tree is one more than the depth of the
deepest node in the tree.
• All nodes of depth are at level in the tree.
Definitions and Properties
• The root is the only node at level 0, and its depth is
0.
• A leaf node is any node that has two empty
children.
• An internal node is any node that has at least one
non-empty child.
• All binary tree nodes have two children (one or
both of which might be empty)
Definitions and Properties
Definitions and Properties
• Two restricted forms of binary tree are sufficiently
important to warrant special names.
• Each node in a full binary tree is either:
• an internal node with exactly two non-empty children
• a leaf.
• A complete binary tree has a restricted shape obtained
by starting at the root and filling the tree by levels from
left to right.
• In the complete binary tree of height , all levels except
possibly level are completely full.
• The bottom level has its nodes filled in from the left
side.
Definitions and Properties
Binary Tree Traversals
• Any process for visiting all of the nodes in some order is
called a traversal.
• Any traversal that lists every node in the tree exactly
once is called an enumeration of the tree’s nodes.
• Preorder traversal: if a node is visited first before
visiting its children
• Postorder traversal: if a node is visited only after we
visit its children (and their subtrees)
• An inorder traversal first visits the left child (including
its entire subtree), then visits the node, and finally
visits the right child (including its entire subtree).
Binary Tree Traversals
Binary Search Tree
• A BST is a binary tree that conforms to the
following condition, known as the Binary Search
Tree Property:
• All nodes stored in the left subtree of a node whose key
value is have key values less than .
• All nodes stored in the right subtree of a node whose
key value is have key values greater than or equal to
.
• One consequence of the Binary Search Tree
Property is that if the BST nodes are printed using
an inorder traversal, the resulting enumeration will
be in sorted order from lowest to highest.
Binary Search Tree
Binary Search Tree
• To find a record with key value in a BST, begin at the root.
• If the root stores a record with key value , then the search
is over. If not, then we must search deeper in the tree.
• What makes the BST efficient during search is that we need
search only one of the node’s two subtrees.
• If is less than the root node’s key value, we search only
the left subtree.
• If is greater than the root node’s key value, we search only
the right subtree.
• This process continues until a record with key value is
found, or we reach a leaf node. If we reach a leaf node
without encountering , then no record exists in the BST
whose key value is .
Binary Search Tree
Heap and Priority Queues
• When a collection of objects is organized by
importance or priority, we call this a priority queue.
• A normal queue data structure will not implement
a priority queue efficiently because search for the
element with highest priority will take time.
• A list, whether sorted or not, will also require
time for either insertion or removal.
• A BST that organizes records by priority could be
used, with the total of inserts and remove
operations requiring time in the average
case.
Heap and Priority Queues
• A heap is a partially ordered data structure which
means there is a relationship between the value
stored at any node and the values of its children.
• There are two types of heaps:
• A max-heap has the property that every node
stores a value that is greater than or equal to the
value of either of its children.
• A min-heap has the property that every node stores
a value that is less than or equal to that of its
children.