0% found this document useful (0 votes)
6 views

Lecture 1 Trees

This document discusses trees and binary trees. It defines trees, binary trees, and binary search trees. It describes properties of trees like paths, roots, parents, children, and levels. It explains tree traversals like inorder, preorder, and postorder traversal. Binary search trees are defined as binary trees where the left subtree of a node contains nodes with keys lesser than the node's key and the right subtree contains nodes with greater keys. Example algorithms for tree traversals are provided.

Uploaded by

Jk Alviento
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 1 Trees

This document discusses trees and binary trees. It defines trees, binary trees, and binary search trees. It describes properties of trees like paths, roots, parents, children, and levels. It explains tree traversals like inorder, preorder, and postorder traversal. Binary search trees are defined as binary trees where the left subtree of a node contains nodes with keys lesser than the node's key and the right subtree contains nodes with greater keys. Example algorithms for tree traversals are provided.

Uploaded by

Jk Alviento
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 3 - Trees

Algorithms and Complexity


Fernandino S. Perilla, MIT
Lecture 3 – Trees
• Binary Trees
• Forest and trees
• Combinatorial equivalents to trees and binary trees
• Properties of trees
• Example of tree algorithms
• Binary search trees
• Average path length in Catalan trees
• Path length in binary search trees
Trees
Trees are fundamental structures that arise implicitly and explicitly in
many practical algorithms, and it is important to understand their
properties in order to be able to analyze these algorithms.
Many algorithms construct trees explicitly; in other cases trees assume
significance as models of programs, especially recursive programs.
Indeed, trees are the quintessential nontrivial recursively defined
objects: a tree is either empty or a root node connected to a sequence
(or a multiset) of trees.
Binary trees
The simplest type of tree. Binary trees are recursive structures that are
made up of two different types of nodes that are attached together
according to a simple recursive definition:
Definition A binary tree is either an external node or an internal node
attached to an ordered pair of binary trees called the left subtree and
the right subtree of that node.
Binary trees
Binary Tree is a special data structure used for data storage purposes. A
binary tree has a special condition that each node can have a maximum
of two children. A binary tree has the benefits of both an ordered array
and a linked list as search is as quick as in a sorted array and insertion
or deletion operation are as fast as in linked list.
Three Binary trees
Important Terms
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one
root per tree and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a
node called parent.
• Child − The node below a given node connected by its edge
downward is called its child node.
• Descendants – Nodes farther down the child/children
• Ancestors – Nodes farther up
Binary Tree
Important Terms
• Leaf − The node which does not have any child node is called the leaf
node.
• Subtree − Subtree represents the descendants of a node
• Visiting − Visiting refers to checking the value of a node when control
is on the node.
• Traversing − Traversing means passing through nodes in a specific
order.
• Levels − Level of a node represents the generation of a node. If the
root node is at level 0, then its next child node is at level 1, its
grandchild is at level 2, and so on.
Important Terms
• keys − Key represents a value of a node based on which a search
operation is to be carried out for a node.
• Length – number of edges on the path
• Depth of a Node - length of the unique path from the root to that
node
• The depth of a tree is equal to the depth of the deepest leaf
• Height of a Node - length of the longest path from that node to a leaf
• all leaves are at height 0
• The height of a tree is equal to the height of the root
Tree Traversals
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc)
which have only one logical way to traverse them, trees can be
traversed in different ways. Following are the generally used ways for
traversing trees.
Inorder Traversal
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call
Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call
Inorder(right-subtree)

4 2 5 1 3
Preorder Traversal
Algorithm Inorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call
Inorder(left-subtree)
3. Traverse the right subtree, i.e., call
Inorder(right-subtree)

1 2 4 5 3
Postorder Traversal
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call
Inorder(left-subtree)
2. Traverse the right subtree, i.e., call
Inorder(right-subtree)
3. Visit the root.

4 5 2 3 1
Example
Binary Search Trees
Binary Search Tree is a node-based binary tree data structure which
has the following properties:
• The left subtree of a node contains only nodes with keys lesser than
the node’s key.
• The right subtree of a node contains only nodes with keys greater
than the node’s key.
• The left and right subtree each must also be a binary search tree.

You might also like