Data Structures and
Algorithms
Week 4: Trees
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 4
• Tree ADT and its applications
• Tree Traversals
• Binary Trees
• Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
Tree ADT
• Example: Family trees
Carole's children and grandchildren.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
Hierarchical Organization
• Example: Family trees
Jared's parents and grandparents.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
4
Hierarchical Organization
• Example: A university's organization
A university's administrative structure.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
Hierarchical Organization
• Example: File Directories
Computer files organized into folders
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
Tree Terminology
• A tree is
• A set of nodes
• Connected by edges
• The edges indicate relationships among nodes
• Nodes arranged in levels
• Indicate the nodes' hierarchy
• Top level is a single node called the root
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
Tree Terminology
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
Tree Terminology
• Nodes at a given level are children of nodes of
previous level
• Node with children is the parent node of those
children
• Nodes with same parent are siblings
• Node with no children is a leaf node
• The only node with no parent is the root node
• All others have one parent each
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
Tree Terminology
• Empty trees?
• Some authors specify a general tree must have at least
the root node
• This text will allow all trees to be empty
• A node is reached from the root by a path
• The length of the path is the number of edges that
compose it
• The height of a tree is the number of levels in the
tree
• The subtree of a node is a tree rooted at a child of
that node
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
Binary Trees
• Each node has at most two children
Three binary trees.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
• A binary tree is either empty or has the following
form
• Where Tleft and Tright are binary trees
Binary Trees
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
Binary Trees
• Every nonleaf in a full binary tree has exactly two
children
• A complete binary tree is full to its next-to-last level
• Leaves on last level filled from left to right
• The height of a binary tree with n nodes that is
either complete or full is
log2(n + 1)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
Nodes in a Binary Tree
A node in a binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
Interface for a Node
• Usually the class that represents a node in a tree is
a detail hidden from the client
• It is placed within a package
• Makes it available
• Available to all classes involved in implementation of a
tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
The Method privateSetTree
• Problem: previous implementation of
privateSetTree not sufficient
• Given: treeA.setTree(a, treeB, treeC);
• Now treeA shares nodes with treeB, treeC
• Changes to treeB also affect treeA
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
The Method privateSetTree
• Possible solution
• Have privatSetTree copy the nodes in TreeB and
TreeC
• Now treeA is separate and distinct from treeB and
treeC
• Changes to either treeB or treeC do NOT affect treeA
• Note that copying nodes is expensive
• Other solutions are considered
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
The Method privateSetTree
• Another possible solution for
treeA.setTree(a, treeB, treeC);
• Have privateSetTree first link the root node of
treeA to root nodes of treeB, treeC
• Then set treeB.root, treeC.root to null
This still has some
problems
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
The Method privateSetTree
1. Create root node r for containing the data
2. If left subtree exists, not empty
• Attach root node to r as left child
3. If right subtree exists, not empty, distinct from left
subtree
• Attach root node r as right child
• If right, left subtrees are the same, attach copy of right
subtree to r instead
4. If left (right) subtree exists, different from the
invoking tree object
• Set its data field root to null
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
Methods for Binary Tree
• getRootData
• isEmpty
• clear
• setRootData
• setRootNode
• getRootNode
• BinaryNodeInterface
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
Computing Height, Counting Nodes
• Within BinaryTree – methods in context
• getHeight()
• getNumberOfNodes()
• Within BinaryNode – methods in context
• getHeight()
• getHeight(BinaryNode node)
• getNumberOfNodes()
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
Traversals
• Make recursive method private
• Call from public method with no parameters
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
Traversals
A binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
Traversals
Using a stack to perform an inorder
traversal of the binary tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
Traversals
Using a stack to traverse the
binary tree in (a) preorder
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
Traversals
Using a stack to traverse the binary
tree in (b) postorder.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
Traversals
Using a queue
to traverse the
binary tree in
level order.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
General Trees
A node for a general tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
Using a Binary Tree to Represent a General
Tree
A general tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
Using a Binary Tree to Represent a General
Tree
an equivalent binary tree;
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
30
Using a Binary Tree to Represent a General
Tree
a more conventional view
of the same binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
Traversals
• Traversals of general tree in a
• Preorder: A B E F C G H I D J
• Postorder: E F B G H I C J D A
• Level order: A B C D E F G H I J
• Traversals of binary tree in c
• Preorder: A B E F C G H I D J
• Postorder: F E I H G J D C B A
• Level order: A B E C F G D H J I
• Inorder: E F B G H I C J D A
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
32
Data Scientists’ view
• To connect a series of data corresponding to a
hierarchy
• Manage data from social network
• Blockchain transaction data analysis
• Binary decision sequence management in robotics
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
33
Activity - Graded
• Grading for 3 points
• Write a java code to perform preorder and post
order traversal using stack in a binary tree.
• Bonus points: 1 If the submission is done before
the end of 8 AM 9 Feb 2019.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
Next Week
Binary Search Trees
AVL Trees
Binary Heaps
Red Black Trees
Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35

Data Structures and Algorithm - Week 4 - Trees, Binary Trees

  • 1.
    Data Structures and Algorithms Week4: Trees Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2.
    Week 4 • TreeADT and its applications • Tree Traversals • Binary Trees • Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3.
    Tree ADT • Example:Family trees Carole's children and grandchildren. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 3
  • 4.
    Hierarchical Organization • Example:Family trees Jared's parents and grandparents. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 4
  • 5.
    Hierarchical Organization • Example:A university's organization A university's administrative structure. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6.
    Hierarchical Organization • Example:File Directories Computer files organized into folders Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7.
    Tree Terminology • Atree is • A set of nodes • Connected by edges • The edges indicate relationships among nodes • Nodes arranged in levels • Indicate the nodes' hierarchy • Top level is a single node called the root Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8.
    Tree Terminology Lecture seriesfor Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9.
    Tree Terminology • Nodesat a given level are children of nodes of previous level • Node with children is the parent node of those children • Nodes with same parent are siblings • Node with no children is a leaf node • The only node with no parent is the root node • All others have one parent each Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10.
    Tree Terminology • Emptytrees? • Some authors specify a general tree must have at least the root node • This text will allow all trees to be empty • A node is reached from the root by a path • The length of the path is the number of edges that compose it • The height of a tree is the number of levels in the tree • The subtree of a node is a tree rooted at a child of that node Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11.
    Binary Trees • Eachnode has at most two children Three binary trees. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12.
    • A binarytree is either empty or has the following form • Where Tleft and Tright are binary trees Binary Trees Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 12
  • 13.
    Binary Trees • Everynonleaf in a full binary tree has exactly two children • A complete binary tree is full to its next-to-last level • Leaves on last level filled from left to right • The height of a binary tree with n nodes that is either complete or full is log2(n + 1) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14.
    Nodes in aBinary Tree A node in a binary tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15.
    Interface for aNode • Usually the class that represents a node in a tree is a detail hidden from the client • It is placed within a package • Makes it available • Available to all classes involved in implementation of a tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16.
    The Method privateSetTree •Problem: previous implementation of privateSetTree not sufficient • Given: treeA.setTree(a, treeB, treeC); • Now treeA shares nodes with treeB, treeC • Changes to treeB also affect treeA Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17.
    The Method privateSetTree •Possible solution • Have privatSetTree copy the nodes in TreeB and TreeC • Now treeA is separate and distinct from treeB and treeC • Changes to either treeB or treeC do NOT affect treeA • Note that copying nodes is expensive • Other solutions are considered Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18.
    The Method privateSetTree •Another possible solution for treeA.setTree(a, treeB, treeC); • Have privateSetTree first link the root node of treeA to root nodes of treeB, treeC • Then set treeB.root, treeC.root to null This still has some problems Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19.
    The Method privateSetTree 1.Create root node r for containing the data 2. If left subtree exists, not empty • Attach root node to r as left child 3. If right subtree exists, not empty, distinct from left subtree • Attach root node r as right child • If right, left subtrees are the same, attach copy of right subtree to r instead 4. If left (right) subtree exists, different from the invoking tree object • Set its data field root to null Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20.
    Methods for BinaryTree • getRootData • isEmpty • clear • setRootData • setRootNode • getRootNode • BinaryNodeInterface Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20
  • 21.
    Computing Height, CountingNodes • Within BinaryTree – methods in context • getHeight() • getNumberOfNodes() • Within BinaryNode – methods in context • getHeight() • getHeight(BinaryNode node) • getNumberOfNodes() Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 21
  • 22.
    Traversals • Make recursivemethod private • Call from public method with no parameters Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 22
  • 23.
    Traversals A binary tree. Lectureseries for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 23
  • 24.
    Traversals Using a stackto perform an inorder traversal of the binary tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 24
  • 25.
    Traversals Using a stackto traverse the binary tree in (a) preorder Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 25
  • 26.
    Traversals Using a stackto traverse the binary tree in (b) postorder. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 26
  • 27.
    Traversals Using a queue totraverse the binary tree in level order. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 27
  • 28.
    General Trees A nodefor a general tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 28
  • 29.
    Using a BinaryTree to Represent a General Tree A general tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 29
  • 30.
    Using a BinaryTree to Represent a General Tree an equivalent binary tree; Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 30
  • 31.
    Using a BinaryTree to Represent a General Tree a more conventional view of the same binary tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 31
  • 32.
    Traversals • Traversals ofgeneral tree in a • Preorder: A B E F C G H I D J • Postorder: E F B G H I C J D A • Level order: A B C D E F G H I J • Traversals of binary tree in c • Preorder: A B E F C G H I D J • Postorder: F E I H G J D C B A • Level order: A B E C F G D H J I • Inorder: E F B G H I C J D A Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 32
  • 33.
    Data Scientists’ view •To connect a series of data corresponding to a hierarchy • Manage data from social network • Blockchain transaction data analysis • Binary decision sequence management in robotics Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 33
  • 34.
    Activity - Graded •Grading for 3 points • Write a java code to perform preorder and post order traversal using stack in a binary tree. • Bonus points: 1 If the submission is done before the end of 8 AM 9 Feb 2019. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 34
  • 35.
    Next Week Binary SearchTrees AVL Trees Binary Heaps Red Black Trees Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 35