Data Structures and Algorithms IT 2202 - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Data Structures and Algorithms IT 2202

Description:

The keys in the first i children are smaller than the ith key ... 30, 12, 17, 49, 22, 65, 51, 56, 70, 68 is a set of 10 integers. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 46
Provided by: stnand
Category:

less

Transcript and Presenter's Notes

Title: Data Structures and Algorithms IT 2202


1
Data Structures and Algorithms (IT 2202)
  • Trees

2
In previous lessons
  • Overview of the syllabus
  • Basic concepts of DS A variables, data
    types ADT, data structures
  • Lists, Stacks, and Queues

3
In this lesson
  • Trees
  • Define and describe various types of
    trees(general definition, binary and binary
    search trees, balanced trees, AVL trees, multiway
    and multiway search trees, self adjusting trees)
  • Tree traversal techniques(pre-order, in-order,
    post-order)
  • Implementations
  • Sample questions from past papers

4
Introduction
  • Lists are more flexible than arrays but are still
    linear structures
  • Trees let us hierarchically organize a collection
    of data items

5
A definition
  • A tree is a collection of nodes
  • Together with a relation (parenthood) that
    imposes a hierarchical structure on these nodes.
  • One of these nodes is called the root.

6
A definition
  • Trees are formally defined recursively
  • A single node by itself is a tree. This node is
    also the root of this tree.
  • Let t1, t2, , tk be disjoint trees with roots
    r1, r2, , rk respectively, and let R be another
    node.We can get a new tree by making R the
    parent of the nodes r1, r2, , rk.

7
A definition
  • In this tree R is the root and t1, t2, , tk are
    subtrees of the root. Nodes r1, r2, , rk are
    called the children of node R.

8
Path
  • If n1, n2, nk is a sequence of nodes in a tree
    such that ni is the parent of ni1, for 1i k,
    then this sequence is called a path from node n1
    to nk

9
Length of a Path
  • The length of a path is one less than the number
    of nodes in the path.
  • There is a path of length zero from every node to
    itself.

10
Ancestors and Descendants
  • If there is a path from node a to node b, then a
    is an ancestor of b, and b is a descendant of a.
  • Any node is both an ancestor and a descendant of
    itself.

11
Ancestors and Descendants
  • An ancestor or a descendant of a node, other than
    the node itself, is called a proper ancestor or
    proper descendant, respectively.
  • In a tree, the root is the only node with no
    proper ancestors. A node with no proper
    descendants is called a leaf.

12
In Brief
  • a is the root
  • b and d are leaves
  • The height of the tree is 2
  • c is an ancestor of d
  • b is a descendant of a

13
Subtrees
  • A subtree of a tree is a node, together with all
    its descendants.

14
Height of a node/tree
  • The height of a node in a tree is the length of a
    longest path from the node to a leaf.
  • The height of a tree is the height of the root.

15
Depth of a node
  • The depth of a node is the length of the unique
    path from the root to that node.

16
Order of siblings
  • Children of a node (siblings) are usually ordered
    from left-to-right.
  • If we wish to explicitly ignore the order of
    children, we shall refer to a tree as an
    unordered tree.

17
Order of siblings
  • We can extend the left-to-right ordering of
    siblings to compare two nodes that are not
    related by ancestor-descendant relationship.

18
Order of siblings
  • The relevant rule is that if a and b are
    siblings, and a is to the left of b, then all
    descendants of a re to the left of all
    descendants of b.

19
Tree Traversal
  • Tree traversal is the process of visiting each
    node in the tree exactly one time.
  • Traversing Methods
  • Breadth First
  • Depth First

20
Breadth First Traversal

30, 15, 45, 10, 13, 42, 29
21
Depth First Traversal
  • Three methods of depth first traversal is defined
  • Preorder
  • Inorder
  • Postorder

22
Preorder Tree Traversal

30, 15, 10, 13, 45, 42, 29
23
Inorder Tree Traversal

10, 15, 13, 30, 42, 45, 29
24
Postorder Tree Traversal

10, 13, 15, 42, 29, 45, 30
25
Binary Trees
  • A binary tree is either an empty tree, or a tree
    in which every node has either no children, only
    a left child, only a right child, or both a left
    child and a right child.

26
A Complete Binary Tree
  • A Complete Binary Tree is a binary tree where
  • Nodes at all levels, except the last have exactly
    two non-null children.
  • The number of nodes
  • Number of leaves
  • Number of non-leaf nodes
  • 2h-1 (2h-1 1 ) 2h - 1
  • (h is the height of the tree)

27
Binary Search Trees
  • A binary search tree is a binary tree where
  • For each node n of the tree the values stored in
    the nodes of its left subtree are less than the
    value v stored at that node. And the values
    stored in the nodes of its right subtree are
    greater.
  • This is called the binary search tree
    property. Multiple copies of the same value are
    not stored.

28
Balanced Trees
  • A binary tree is a balanced tree if the
    difference in the heights of both subtrees of any
    node is either 0 or 1.
  • A binary tree is perfectly balanced if
  • It is balanced
  • And all leaves are found at one or two levels

29
AVL Trees
  • An AVL tree is a tree where the height of the
    left and right subtrees of every node differ by
    at most one.
  • The accepted balance factors are
  • -1, 0, 1
  • AVL Adelson Velskii Landis

30
AVL Tree

31
Self Adjusting Trees
  • Is an alternative method of improving the
    performance of the insert, delete, and retrieve
    operation on a binary tree by
  • Moving the most often accessed nodes up the tree

32
Multiway Trees
  • A tree that can have more than 2 children at its
    nodes. (as opposed to binary trees)
  • A multiway search tree is m-tree, where
  • Each node has m children and m-1 keys
  • The keys in each node are in ascending order
  • The keys in the first i children are smaller than
    the ith key
  • The keys in the last m-1 children are larger than
    the ith key

33
Implementation of Trees
  • Commonly implemented
  • By Using arrays
  • As Linked structures

34
Question 1
  • Consider the following statements is/are true?
  • 1. A binary tree can contain at least 2L nodes at
    level L.
  • 2. A complete binary tree of depth d is a binary
    tree that contains 2L Nodes at each level L
    between 0 and d, both inclusive.

35
Question 1
  • 3. The total number of nodes (Tn ) in a complete
    binary tree of depth d is
  • 2 d1 - 1 .
  • 4. The height of the complete binary tree can be
    written as h log2 (Tn1)-1 where Tn is Total
    number of Nodes.

36
Question 1
  • Which of them is correct in respect of the above
    statements regarding the Binary trees?
  • A complete binary tree of depth d is a binary
    tree that contains 2L Nodes at each level L
    between 0 and d, both inclusive.
  • The total number of nodes (Tn ) in a complete
    binary tree of depth d is 2d1 - 1 .
  • The height of the complete binary tree can be
    written as h log2 (Tn1)-1 where Tn is Total
    number of Nodes.

37
Question 2
  • 30, 12, 17, 49, 22, 65, 51, 56, 70, 68 is a set
    of 10 integers.
  • Create a Binary search tree using the above set
    of integers.

38
Question 2
30

30, 12, 17, 49, 22, 65, 51, 56, 70, 68
49
12
65
17
51
22
70
56
68
39
Question 3
  • Which of the following is/are not (an) AVL
    tree(s)?

40
Question 3

Not AVL Tree
AVL Tree
41
Question 3

Not AVL Tree
42
Question 4
  • Consider the following expression tree

43
Question 4
  • Which of the following expressions shows the post
    order traversal of the above tree?
  • (a) abcdefg
  • (b) abcdefg
  • (c) abcdefg
  • (d) abcdefg
  • (e) abcdefg

44
Review of what we have covered
  • Define and describe various types of
    trees(general definition, binary and binary
    search trees, balanced trees, AVL trees, multiway
    and multiway search trees, self adjusting trees)
  • Tree traversal techniques(pre-order, in-order,
    post-order)
  • Implementations
  • A sample question from a past paper

45
In the next lesson
  • Graphs
Write a Comment
User Comments (0)
About PowerShow.com