Algorithms and Data Structures
Week 5
TREES – Non linear data structure
By – Priyanka Rana
Do you have a non linear mind?
Breakthroughs come by thinking "nonlinearly“.
Data Structure - Trees
 Non linear.
 Faster than linear data structures.
Applications
Trees
 Is an abstract data type.
 Defined as a set of nodes that stores elements
hierarchically.
 Consists of nodes with a
parent- child relation.
Parent-child relationship
Tree terminology
 Root
 If tree T is nonempty, it has a special node, called the
root of T, that has no parent.
 Internal Node
 Node with at least one child.
 External Node (leaf)
 Node without children.
Tree terminology
 Siblings
 Nodes that are children of the same parent.
 Ancestors of a Node
 Parent, grandparent, grand-grandparent.
 Descendants of a Node
 Child, grand child, grand-grandchild etc.
Tree terminology
 Edge
 An edge of tree T is a pair of nodes (u, v) such
that u is the parent of v, or vice versa.
 Path
 Path of T is a sequence of nodes such that any
two consecutive nodes in the sequence form an
edge.
A
DCB
E F
I J K
G H
ROOT - A
Internal Node- A, B, F,
C
External Node- E, I, J, K, G,
H, D
Sibling of B – C,D
Ancestors of F - A, B,
Descendant of B - E, I, J, K, F
Path – A, B, F, K
Tree terminology
 Ordered Trees
If there is a linear ordering defined for the children of each
node, visualized by arranging siblings left to right.
Book
Prefac
e
Part A Part B
Referenc
es
Ch. 5 Ch.
6
^ ^ ^ ^
Sec.
1.1 Sec. 1.9
Ch.
1
Sec. 10.1 Sec. 10.9
Ch.
10
Tree Abstract Data Type
 positions in a tree are its nodes.
 position object for a tree supports the method:
position <E> element()
Return the object stored at this position.
Tree’s accessors methods
 position root():
 Return the tree’s root.
 position parent(p):
 Return the parent of p.
 Iterable children(p):
 Return an iterable collection containing the children of node
p.
Tree’s query methods
 boolean isInternal(p):
 Test whether node P is internal.
 boolean isExternal(p):
 Test whether node p is external.
 boolean isRoot(p):
 Test whether node p is the root
Tree’s generic methods
 int size()
 Return the number of nodes in the tree.
 boolean isEmpty()
 Indicates whether the tree has any nodes.
 iterator iterator()
 Return an iterator of all elements stored at nodes of the tree.
 iterable position()
 Return an iterable collection of all the nodes of the tree.
 element replace(v, e)
 Replace with e and return the element stored at node v.
A
DCB
E F
I J K
G H
Size() - 11
isInternal(A) -
True
isExternal(E) - True
isRoot(C) - False isEmpty - False
Method Running Time
Size, isEmpty() O(1)
iterator, positions O(n)
Replace O(1)
Root, parent O(1)
isExternal O(1)
isInternal O(1)
isRoot O(1)
Tree traversal Algorithms
 A traversal of a Tree T is a systematic way of
accessing(or visiting) all the nodes of T.
 How can we index node?
 Position
 public Iterable <Position<E>> positions();
 How can we index children of node?
 Iterable
 public Iterator<E> iterator();
Tree ADT
 Depth of a node
 number of ancestors.
 Height of a tree
 maximum depth of any node.
 Subtree
 tree consisting of a node and its descendants.
Algorithm depth(T, v):
if v is the root of T then
return 0
else
return 1 + depth(T,w)
Algorithm height(T)
h0
for each vertex v in T do
if v is an external node in T then
h max (h, depth(T,v))
return h
A
DCB
E F
I J K
G H
Depth of F – 2
Height of a Tree -
3
Subtree – C, G, H
Traversal schemes
 preorder traversal
 Node is visited before its descendants.
 Children of each node of an ordered tree are
ordered from left to right.
 postorder traversal
 Node is visited after its descendants.
 Children of each node of an ordered tree are
ordered from left to right.
Preorder traversal
 Algorithm preorder(T, v):
visit(v);
for each child w of v do
preorder(T, w);
 Application
 print a structured document
 Running Time
 O(n)
Preorder example
Journal
Abstra
ct
#1 #2
Referenc
es
Sec.
1.1 Sec. 1.9 Sec. 2.1 Sec. 2.9
Title
Postorder traversal
 Algorithm postorder(T, v):
– for each child w of v do
– postorder(T, w);
– visit(v);
 Application
 computer space used by files in a directory and its subdirectories.
 Running Time
 O(n)
Postorder example
CP2410
Tutorial
Lectur
es
Week 10
Subject Outline
Tut 1 Tut 10 Week 1 Week 5
Exercise
What is the output of preorder and postorder
traversal of this tree?
What’s special about this tree?
Binary Trees
A binary tree is an ordered tree :
 Every node has at most two children.
 Each child node is labelled as being either a left child or
a right child.
 A left child precedes a right child in the
ordering of children of a node.
Binary trees Application
 A binary tree is proper or full
 If each node has either zero or two children.
 Application
 Representation of arithmetic expressions
 Internal nodes : operators
 External nodes : operands
Arithmetic expression tree
(2 x (a -1) + (3 x
b))
2
a 1
3 b
+
-
x x
Binary trees Application
 Representation of decision process
 Internal nodes : questions with yes/no answer
 External nodes : decisions
Binary Tree ADT - Accessor methods
 position left(p):
 Return the left child of p.
 position right(p):
 Return the right child of p.
 boolean hasLeft(p):
 Test whether p has a left child.
 boolean hasRight(p):
 Test whether p has a right child.
Binary Tree Implementation
 Linked Lists
Binary Tree Implementation
........ .... .
.
A B D G H
Array
If v is the root of T, then p(v) = 1.
If v is the left child of node u, then p(v) = 2p(u).
If v is the right child of node u, then p(v) = 2p(u)
Inorder traversal of a Binary Tree
Algorithm inorder(T, v):
if v has a left child u in T then
inorder(T, u) {recursively traverse left subtree}
perform the “visit action for node v
if v has a right child w in T then
inorder(T, w) {recursively traverse right subtree}
Inorder traversal of a Binary Tree
(3+1)
(3+1) * 4
((3+1)*4)/
((3+1)*4)/ (9-5)
((3+1)*4)/ ((9-5) + 2)
Answer = 16/6
Binary search
 x(v) – element stored in any internal node.
 For each internal node v of T:
 The elements stored in the left subtree of v are less than or
equal to x(v).
 The elements stored in the right subtree of v are greater
than or equal to x(v).
 The external nodes of T do not store any element.
Binary search tree
The blue solid path is traversed when searching (successfully) for 36.
The blue dashed path is traversed when searching (unsuccessfully)
for 70.

Trees - Non Linear Data Structure

  • 1.
    Algorithms and DataStructures Week 5 TREES – Non linear data structure By – Priyanka Rana
  • 2.
    Do you havea non linear mind?
  • 3.
    Breakthroughs come bythinking "nonlinearly“.
  • 4.
    Data Structure -Trees  Non linear.  Faster than linear data structures.
  • 5.
  • 8.
    Trees  Is anabstract data type.  Defined as a set of nodes that stores elements hierarchically.  Consists of nodes with a parent- child relation.
  • 9.
  • 10.
    Tree terminology  Root If tree T is nonempty, it has a special node, called the root of T, that has no parent.  Internal Node  Node with at least one child.  External Node (leaf)  Node without children.
  • 11.
    Tree terminology  Siblings Nodes that are children of the same parent.  Ancestors of a Node  Parent, grandparent, grand-grandparent.  Descendants of a Node  Child, grand child, grand-grandchild etc.
  • 12.
    Tree terminology  Edge An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa.  Path  Path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge.
  • 13.
    A DCB E F I JK G H ROOT - A Internal Node- A, B, F, C External Node- E, I, J, K, G, H, D Sibling of B – C,D Ancestors of F - A, B, Descendant of B - E, I, J, K, F Path – A, B, F, K
  • 14.
    Tree terminology  OrderedTrees If there is a linear ordering defined for the children of each node, visualized by arranging siblings left to right. Book Prefac e Part A Part B Referenc es Ch. 5 Ch. 6 ^ ^ ^ ^ Sec. 1.1 Sec. 1.9 Ch. 1 Sec. 10.1 Sec. 10.9 Ch. 10
  • 15.
    Tree Abstract DataType  positions in a tree are its nodes.  position object for a tree supports the method: position <E> element() Return the object stored at this position.
  • 16.
    Tree’s accessors methods position root():  Return the tree’s root.  position parent(p):  Return the parent of p.  Iterable children(p):  Return an iterable collection containing the children of node p.
  • 17.
    Tree’s query methods boolean isInternal(p):  Test whether node P is internal.  boolean isExternal(p):  Test whether node p is external.  boolean isRoot(p):  Test whether node p is the root
  • 18.
    Tree’s generic methods int size()  Return the number of nodes in the tree.  boolean isEmpty()  Indicates whether the tree has any nodes.  iterator iterator()  Return an iterator of all elements stored at nodes of the tree.  iterable position()  Return an iterable collection of all the nodes of the tree.  element replace(v, e)  Replace with e and return the element stored at node v.
  • 19.
    A DCB E F I JK G H Size() - 11 isInternal(A) - True isExternal(E) - True isRoot(C) - False isEmpty - False
  • 20.
    Method Running Time Size,isEmpty() O(1) iterator, positions O(n) Replace O(1) Root, parent O(1) isExternal O(1) isInternal O(1) isRoot O(1)
  • 21.
    Tree traversal Algorithms A traversal of a Tree T is a systematic way of accessing(or visiting) all the nodes of T.
  • 22.
     How canwe index node?  Position  public Iterable <Position<E>> positions();  How can we index children of node?  Iterable  public Iterator<E> iterator();
  • 23.
    Tree ADT  Depthof a node  number of ancestors.  Height of a tree  maximum depth of any node.  Subtree  tree consisting of a node and its descendants.
  • 24.
    Algorithm depth(T, v): ifv is the root of T then return 0 else return 1 + depth(T,w)
  • 25.
    Algorithm height(T) h0 for eachvertex v in T do if v is an external node in T then h max (h, depth(T,v)) return h
  • 26.
    A DCB E F I JK G H Depth of F – 2 Height of a Tree - 3 Subtree – C, G, H
  • 27.
    Traversal schemes  preordertraversal  Node is visited before its descendants.  Children of each node of an ordered tree are ordered from left to right.  postorder traversal  Node is visited after its descendants.  Children of each node of an ordered tree are ordered from left to right.
  • 28.
    Preorder traversal  Algorithmpreorder(T, v): visit(v); for each child w of v do preorder(T, w);  Application  print a structured document  Running Time  O(n)
  • 29.
  • 30.
    Postorder traversal  Algorithmpostorder(T, v): – for each child w of v do – postorder(T, w); – visit(v);  Application  computer space used by files in a directory and its subdirectories.  Running Time  O(n)
  • 31.
  • 32.
    Exercise What is theoutput of preorder and postorder traversal of this tree?
  • 33.
  • 34.
    Binary Trees A binarytree is an ordered tree :  Every node has at most two children.  Each child node is labelled as being either a left child or a right child.  A left child precedes a right child in the ordering of children of a node.
  • 35.
    Binary trees Application A binary tree is proper or full  If each node has either zero or two children.  Application  Representation of arithmetic expressions  Internal nodes : operators  External nodes : operands
  • 36.
    Arithmetic expression tree (2x (a -1) + (3 x b)) 2 a 1 3 b + - x x
  • 37.
    Binary trees Application Representation of decision process  Internal nodes : questions with yes/no answer  External nodes : decisions
  • 38.
    Binary Tree ADT- Accessor methods  position left(p):  Return the left child of p.  position right(p):  Return the right child of p.  boolean hasLeft(p):  Test whether p has a left child.  boolean hasRight(p):  Test whether p has a right child.
  • 39.
  • 40.
    Binary Tree Implementation ............ . . A B D G H Array If v is the root of T, then p(v) = 1. If v is the left child of node u, then p(v) = 2p(u). If v is the right child of node u, then p(v) = 2p(u)
  • 41.
    Inorder traversal ofa Binary Tree Algorithm inorder(T, v): if v has a left child u in T then inorder(T, u) {recursively traverse left subtree} perform the “visit action for node v if v has a right child w in T then inorder(T, w) {recursively traverse right subtree}
  • 42.
    Inorder traversal ofa Binary Tree (3+1) (3+1) * 4 ((3+1)*4)/ ((3+1)*4)/ (9-5) ((3+1)*4)/ ((9-5) + 2) Answer = 16/6
  • 43.
    Binary search  x(v)– element stored in any internal node.  For each internal node v of T:  The elements stored in the left subtree of v are less than or equal to x(v).  The elements stored in the right subtree of v are greater than or equal to x(v).  The external nodes of T do not store any element.
  • 44.
    Binary search tree Theblue solid path is traversed when searching (successfully) for 36. The blue dashed path is traversed when searching (unsuccessfully) for 70.

Editor's Notes

  • #2 &amp;lt;number&amp;gt;
  • #3 Do YOU have a non-linear mind? frequently turn 5 minute tasks into &amp;apos;all nighters&amp;apos;, can&amp;apos;t put something down until it&amp;apos;s finished, and there are many other symptoms as well, &amp;lt;number&amp;gt;
  • #4 according to productivity experts , breakthroughs come by thinking &amp;quot;nonlinearly&amp;quot; , &amp;lt;number&amp;gt;
  • #5 non-linear data structure in computing known as TREES.// Tree structures are a breakthrough in data organization, as they implement a host of algorithms much faster than when using linear data structures, such as list. &amp;lt;number&amp;gt;
  • #6 Trees provide a natural organization for data, and consequently have become ubiquitous structures in //file systems, graphical user interfaces, //databases, Web sites, and other computer systems. //Moves in a game are also put in a tree as well. &amp;lt;number&amp;gt;
  • #7 When we say that trees are &amp;quot;nonlinear,&amp;quot; we are referring to an organizational relationship that is more than &amp;quot;before&amp;quot; and &amp;quot;after&amp;quot; relationships, relationships in a tree are hierarchical, being &amp;quot;above&amp;quot; and &amp;quot;below&amp;quot;. //Actually, the main terminology for tree data structures comes from family trees, with the terms &amp;quot;parent,&amp;quot; &amp;quot;child,&amp;quot; &amp;quot;ancestor,&amp;quot; and &amp;quot;descendent&amp;quot; being the most common words used to describe relationships. &amp;lt;number&amp;gt;
  • #8 &amp;lt;number&amp;gt;
  • #9 &amp;lt;number&amp;gt;
  • #10 elaborate the parent child relationship in a tree with this example.// // // // this is how tree is extended from one level to another. A very good example of organisational hierarchy. &amp;lt;number&amp;gt;
  • #11 &amp;lt;number&amp;gt;
  • #15 Ordered trees: A tree is ordered if there is a linear ordering defined for the children of each node; that is, we can identify the children of a node as being the first, second, third, and so on. Such an ordering is usually visualized by arranging siblings left to right, according to their order. &amp;lt;number&amp;gt;
  • #16 In a tree abstract data type, positions in a tree are its nodes, and it supports a method called element() that returns the object stored at this position. &amp;lt;number&amp;gt;
  • #17 Is A internal , yes it is as it has children, is E external yes, as it has no children, is// // &amp;lt;number&amp;gt;
  • #21 Here is the running time for all the nodes, its all O(1) except iterator and position which has O(n) where running time does depend on data size. &amp;lt;number&amp;gt;
  • #22 &amp;lt;number&amp;gt;
  • #23 , in simple words, tree traversal is a systematic way of accessing all the nodes. // Do you think we can access nodes? if yes, how? position method, that returns an iterable collection of the nodes. if we have to index children of node, then iterator method can be used., iterator() method returns an iterator of the elements stored in the tree &amp;lt;number&amp;gt;
  • #24 &amp;lt;number&amp;gt;
  • #25 The depth of a node v can also be recursively defined as follows: • If v is the root, then the depth of v is 0 , • Otherwise, the depth of v is one plus the depth of the parent of v. this algorithm works recursively. height of a node v in a tree T is also defined recursively: • If v is an external node, then the height of v is 0 • Otherwise, the height of v is one plus the maximum height of a child of v. &amp;lt;number&amp;gt;
  • #26 Alorithm postorder: recursively traverse the subtree rooted at w., perform the visit action for node v. &amp;lt;number&amp;gt;
  • #29 Algoritm preorder: perform the &amp;quot;visit&amp;quot; action for node v, here it recursively traverse the subtree rooted at w. The preorder traversal algorithm is useful for producing a linear ordering of the nodes of a tree where parents must always come before their children in the ordering. Such orderings have several different applications. &amp;lt;number&amp;gt;
  • #30 &amp;lt;number&amp;gt;
  • #31 Alorithm postorder: recursively traverse the subtree rooted at w., perform the visit action for node v. &amp;lt;number&amp;gt;
  • #32 takes O(n) time, assuming that visiting each node takes O(1) time &amp;lt;number&amp;gt;
  • #34 If observe closely, each node has two children, and hence this type of tree is called as Binary tree. &amp;lt;number&amp;gt;
  • #35 // // natural way to realize a binary tree T is to use a linked structure, // A node is represented by an object storing element, parent node, left child nod, right child node. //And when we make a tree out of these nodes , it looks like something like this// &amp;lt;number&amp;gt;
  • #36 &amp;lt;number&amp;gt;
  • #42 inorder traversal: To traverse a binary tree in Inorder, we traverse the left most subtree starting at the left external node, (ii) Visit the root, and (iii) Traverse the right subtree starting at the left external node. &amp;lt;number&amp;gt;
  • #44 // /// // Lets see the example of the binary search tree storing integers. try to find 36 and 70 are in S, by traversing a path down the tree starting at the root. &amp;lt;number&amp;gt;
  • #45 Lets do it for 36 first, At each internal node we encountered, we compare our 36 with the element stored at node. If y = x(v), then the search continues in the left subtree of v. If y = x(v), then the search terminates successfully. If y ≥ x(v), then the search continues in the right subtree of v. Finally, if we reach an external node, the search terminates unsuccessfully. &amp;lt;number&amp;gt;