2. What we have done previously ?
Summary of previous lectures.
● Introduction to Data Structure
● Abstract Data Types
● Why data structure
● Introduction to Algorithms
● Measurements of Algorithms
● Stacks & Queues
● Arrays & Linked Lists
● Stack , Queue With Linked List
● Algorithms
● Big O notation
● Recursion
● Sorting
● Searching
3. Binary Trees
Fundamental data structure
Combines the advantages of an ordered array and a linked list.
You can search an ordered array quickly [O(log N)].
You can insert and delete items quickly in a linked list [O(1)].
It would be nice, if there were a data structure with quick insertion/deletion
and quick search.
Trees provide both of these characteristics.
4. Trees
A tree consists of nodes connected by edges.
The nodes are represented as circles.
The edges are represented as lines.
In computer programs,
Nodes - people, car parts, airline reservations, and so on(objects).
Edges - relationship between two nodes (represented as a reference in a Java program).
Traversing
Only way to get from node to node is to follow a path along the lines.
5. Trees (Contd.)
Structure
There is one node (root) in the top row of a tree.
One or more nodes in the second row connecting to the root node.
Even more nodes in the third row and they are connected to nodes in the second row, and
so on.
Programs starts an operation at the top (root), and traverse from top to bottom.
Binary Tree
Each node in a binary tree has a maximum of two children.
More generally, a tree can have more than two children, and are called as multiway trees.
7. Tree Terminology (Contd.)
Path - Think of someone walking from node to node along the edges that
connect them. The resulting sequence of nodes is called a path.
Root - The node at the top of the tree.
- There is only one root in a tree.
- For a collection of nodes and edges to be a tree, there must be one and
only one path from the root to any other node.
8. Tree Terminology (Contd.)
Parent - Any node (except the root) has exactly one edge running upward to
another node. The node above it is called the parent of the node.
Child - Any node may have one or more lines running downward to other
nodes. These nodes below a given node are called its children.
Leaf - A node that has no children is called a leaf node or simply a leaf. There
can be only one root in a tree, but there can be many leaves.
Subtree - Any node may be considered to be the root of a subtree, which
consists of its children, and its children’s children, and so on. If you think in
terms of families, a node’s subtree contains all its descendants.
9. Tree Terminology (Contd.)
Visiting
A node is visited when the program control arrives at the node.
Carry out some operation on the node (E.g. display contents).
Passing the control from one node to another is not considered as visiting
the node.
Traversing
Visit all the nodes in some specified order (E.g. visiting all the nodes in
ascending order).
10. Tree Terminology (Contd.)
Level of a node
How many generations the node is from the root.
Root - level 0
Its children - level 1
Its grandchildren - level 2
Keys
One data field in an object is designated as a key value.
This value is used to search for the item or perform other operations on it.
11. Binary Trees
A tree that can have at most two children.
Simplest and most common in Data Structures.
Two children - left and right (corresponding to their position).
A node in a binary tree doesn’t need to have the maximum of two children.
It may have only a left child, or only a right child, or it can have no children at
all (leaf).
E.g. See slide 5.
12. Binary Search Trees
It’s a binary tree.
For each node,
Left child must have a key value less than the node’s key value.
Right child must have a key value greater than or equal to the node’s key
value.
13. Unbalanced Trees
Most of their nodes on one side of the root or the other.
Trees become unbalanced because of the order in which the data items are
inserted.
E.g. If the sequence of numbers 11, 18, 33, 42 and 65 is inserted into a binary
search tree, all the values will be right children and the tree will be
unbalanced.
25. ● We start by inOrder() with the root A as an argument (say inOrder(A)).
● inOrder(A) first calls inOrder() with its left child, B, as an argument
(inOrder(B)).
● inOrder(B) now calls inOrder() with its left child as an argument.
● However, it has no left child, so this argument is null (inOrder(null)).
● There are now three frames of inOrder() in the call stack (inOrder(A),
inOrder(B) and inOrder(null)).
● However, inOrder(null) returns immediately to its called method
(inOrder(B)) because its argument is null.
26. Now inOrder(B) goes on to visit B and display it.
Then inOrder(B) calls inOrder() again, with its right child (null) as an argument
(i.e. inOrder(null)).
Again it returns immediately to inOrder(B).
Now inOrder(B) completed its three steps, so the program control goes back to
inOrder(A). ? Now inOrder(A) visits A and display it.
Next inOrder(A) calls inOrder() with its right child, C, as an argument (inOrder(C)).
Like inOrder(B), inOrder(C) has no children, so step 1 returns with no action, step
2 visits C, and step 3 returns with no action.
Next, control goes back to inOrder(A), and inOrder(A) has completed its 3 steps,
and the entire traversal is complete.
29. Preorder and Postorder Traversals
Preorder Traversal (NLR)
1. Visit the node.
2. Call itself to traverse the nodes left subtree.
3. Call itself to traverse the nodes right subtree.
Postorder Traversal (LRN)
1. Call itself to traverse the nodes left subtree.
2. Call itself to traverse the nodes right subtree.
3. Visit the node.
40. Big O of Binary tree ?
Do you have any idea ?
Let’s take N = No of nodes and L = No of levels
N = 2L
1
−
N + 1 = 2L
L = log2(N + 1)
Thus, the time needed to carry out the common tree operati
is proportional to the base 2 log of N.
In Big O notation we say such operations take O(logN) time.