Upload
meljun-cortes-mbampa
View
215
Download
0
Embed Size (px)
Citation preview
8/21/2019 Meljun Cortes Data Structures Trees
1/34
Data Structures
Trees
root is on the top and the leaves are at thebottom
displays data items in a hierarchical structure
organize information in database systems
also work as ordered array and a linked list
Trees *Property of STI Page 1 of 34
A tree is composed of nodes connected byedges or lines
8/21/2019 Meljun Cortes Data Structures Trees
2/34
Data Structures
Basic Terminologies
Nodes
Root Node
Edges / lines / paths
Leaf nodes
Internal Nodes / Child Nodes
Subtree
Visiting
Traversin
Trees *Property of STI Page 2 of 34
Levels
Degree
Depth of the tree
Keys
8/21/2019 Meljun Cortes Data Structures Trees
3/34
Data Structures
Exercise
Given the tree below, find the following:
1. Root node
2. Depth of the tree
3. Nodes in level 4
4. Degree of D5. Child/Children of Z
6. Total Nodes in the tree
7. Parent of F
Trees *Property of STI Page 3 of 34
8. Leaf Nodes
9. Child nodes
10. Maximum degree of the tree
8/21/2019 Meljun Cortes Data Structures Trees
4/34
Data Structures
Binary Tree
every node in a tree can have at most twochildren
Left subtree
Right subtree
Trees *Property of STI Page 4 of 34
two applications of binary tree
binary expression tree
binary search tree
8/21/2019 Meljun Cortes Data Structures Trees
5/34
Data Structures
Binary Search Tree
D:\Annual\Martin\Inventory\January.dat.
Hierarchical file structure in a computer systemis one of the common trees
Hierarchical file structure is not a binary tree
Binary search trees are trees such that any
node has a key which is no more than the keyin its right child node and no less than the key
Trees *Property of STI Page 5 of 34
Figure A – BinarySearch Tree
Figure B – NOT a BST
8/21/2019 Meljun Cortes Data Structures Trees
6/34
Data Structures
Operations on Binary Tree
Node Class
class Node
{
int iData; //data used as key value
float fData; //other datanode leftChild; //node’s left child
node rightChild; //node’s right child
public void displayNode()
Trees *Property of STI Page 6 of 34
{
}
{
class Node
{
person p1; //reference to person object
node leftChild; //node’s left child
node rightChild; //node’s right child
}
class Person{
int iData;
float fData;
}
8/21/2019 Meljun Cortes Data Structures Trees
7/34
Data Structures
Operations on Binary Tree
Tree Class
class Tree
{
private Node root; //the only data field
//in Tree
public void find(int key)
{
Trees *Property of STI Page 7 of 34
}
public void insert(int id, double dd)
{
}
public void delete(int id)
{
}
//you may include other methods
}
8/21/2019 Meljun Cortes Data Structures Trees
8/34
Data Structures
Operations on Binary Tree
TreeApp Class
class TreeApp
{ public static voids main(String[] args)
{
Tree theTree = new Tree; //make a tree
theTree.insert(50, 1.5); //insert 3 nodes
Trees *Property of STI Page 8 of 34
theTree.insert(25, 1.7);
theTree.insert(75, 1.9);
node found = theTree.find(25); //find
//node with key 25
if(found != null)
System.out.println(″Found the node with
key 25″);
else
System.out.println(″Could not find node
with key 25″);
}
}
8/21/2019 Meljun Cortes Data Structures Trees
9/34
Data Structures
Operations on Binary Tree
Finding a Node
pubic Node find(int key)
{ Node current = root
while(current.iData != key)
{
Trees *Property of STI Page 9 of 34
.
current = current.leftChild;
else
current = current.rightChild if(current == null)
return null;
}
return current;
}
8/21/2019 Meljun Cortes Data Structures Trees
10/34
Data Structures
Inserting a Node
Operations on Binary Tree
public void insert(int id, double dd)
{
Node newNode = new Node();
newNode.iData = id;
newNode.dData = dd;
if(root==null)
root = newNode;else
{
Node current = root;
Node parent;
while(true)
{
Trees *Property of STI Page 10 of 34
parent = current;
if(id < current.iData)
{
current = current.leftChild;
if(current == null){
parent.leftChild = newNode;
return;
}
}
else
{
current = current.rightChild;
if(current == null)
{
parent.rightChild = newNode;
return;
}
}
}
}
}
8/21/2019 Meljun Cortes Data Structures Trees
11/34
Data Structures
a binary tree used in representing arithmeticexpression
specifically the combinations of operators,
operands, and order of evaluation
Binary Expression Tree
Trees *Property of STI Page 11 of 34
8/21/2019 Meljun Cortes Data Structures Trees
12/34
Data Structures
Constructing an Expression Tree
Read the symbols one at a time.
If the symbol is an operand:
Create a one-node tree.
Push a pointer to the node into thestack.
If the symbol is an operator:
3.1 Pop two pointers from stack. These
Binary Expression Tree
Trees *Property of STI Page 12 of 34
po n ers represen e a ress o eroot nodes of two trees, T1 and T2.
3.2 Form a new tree whose root is theoperator and whose left and rightchildren are T1 and T2, respectively.
3.3 Push the pointer to the new tree intothe stack.
Repeat steps 1 thru 3 until the last symbol
has been read and processed.
8/21/2019 Meljun Cortes Data Structures Trees
13/34
Data Structures
Example: AB+CDE+∗∗∗∗/
“A” pushed into stack
“B” pushed into stack
Binary Expression Tree
Trees *Property of STI Page 13 of 34
subtree for “+” created
“+” pushed into stack
8/21/2019 Meljun Cortes Data Structures Trees
14/34
Data Structures
“C” pushed into stack
“D” and “E” pushed into stack
Binary Expression Tree
Trees *Property of STI Page 14 of 34
subtree for “+” created
8/21/2019 Meljun Cortes Data Structures Trees
15/34
Data Structures
“+” pushed into stack
subtree for “*” created
Binary Expression Tree
Trees *Property of STI Page 15 of 34
“*” pushed into stack
8/21/2019 Meljun Cortes Data Structures Trees
16/34
Data Structures
subtree for “/” created
“/” ushed into stack
Binary Expression Tree
Trees *Property of STI Page 16 of 34
8/21/2019 Meljun Cortes Data Structures Trees
17/34
Data Structures
Create a binary expression tree for the infixnotation given below.
1. ( ( A + B ) - ( C / D ) ) + ( E * ( F / D ) )
2. A + ( B * ( C – D ) ) – ( E – F * ( G + H ) )
Exercise
Trees *Property of STI Page 17 of 34
8/21/2019 Meljun Cortes Data Structures Trees
18/34
Data Structures
Binary Tree Traversal
to visit all the nodes of the tree in order
not commonly used as finding, inserting,deleting nodes
useful in some circumstances and simpler thandeleting of nodes
Inorder
Preorder
Postorder
Inorder Traversal
Trees *Property of STI Page 18 of 34
1. e no e as a e su ree:
Traverse the left subtree in inorder
(recursive call). Once completed,proceed to step 2.
Otherwise, proceed to step 2.
2. Read the root node.
3. If the node has a right subtree:
Traverse the right subtree in inorder(recursive call).
The nodes of the tree willbe visited in the order:
A B C D E F G
8/21/2019 Meljun Cortes Data Structures Trees
19/34
Data Structures
Binary Tree Traversal
Preorder Traversal
1. Read the root node.
2. If the node has a left subtree:
Traverse the left subtree in preorder
(recursive call). Once completed,proceed to step 3.
Otherwise, proceed to step 3.
3. If the node has a right subtree:
Trees *Property of STI Page 19 of 34
raverse e r g su ree n preor er(recursive call).
The nodes of the treewill be visited in the
order:
D B A C F E G
8/21/2019 Meljun Cortes Data Structures Trees
20/34
Data Structures
Binary Tree Traversal
Postorder Traversal
1. If the node has a left subtree:
Traverse the left subtree in postorder(recursive call). Once completed,
proceed to step 2.Otherwise, proceed to step 2.
2. If the node has a right subtree:
Traverse the right subtree in postorder
Trees *Property of STI Page 20 of 34
. ,proceed to step 3.
Otherwise, proceed to step 3.
3. Read the root node.
The nodes of the tree
will be visited in the
order:A C B E G F D
8/21/2019 Meljun Cortes Data Structures Trees
21/34
Data Structures
Generating the given expression tree into thethree traversal methods.
Inorder Sequence
Inorder Traversal
Trees *Property of STI Page 21 of 34
8/21/2019 Meljun Cortes Data Structures Trees
22/34
Data Structures
Inorder Traversal
Trees *Property of STI Page 22 of 34
8/21/2019 Meljun Cortes Data Structures Trees
23/34
Data Structures
Inorder Traversal
Trees *Property of STI Page 23 of 34
8/21/2019 Meljun Cortes Data Structures Trees
24/34
Data Structures
Inorder Traversal
Trees *Property of STI Page 24 of 34
8/21/2019 Meljun Cortes Data Structures Trees
25/34
Data Structures
Preorder Traversal
Trees *Property of STI Page 25 of 34
8/21/2019 Meljun Cortes Data Structures Trees
26/34
Data Structures
Preorder Traversal
Trees *Property of STI Page 26 of 34
8/21/2019 Meljun Cortes Data Structures Trees
27/34
Data Structures
Preorder Traversal
Trees *Property of STI Page 27 of 34
8/21/2019 Meljun Cortes Data Structures Trees
28/34
Data Structures
Preorder Traversal
Trees *Property of STI Page 28 of 34
8/21/2019 Meljun Cortes Data Structures Trees
29/34
Data Structures
Postorder Traversal
Trees *Property of STI Page 29 of 34
8/21/2019 Meljun Cortes Data Structures Trees
30/34
Data Structures
Postorder Traversal
Trees *Property of STI Page 30 of 34
8/21/2019 Meljun Cortes Data Structures Trees
31/34
Data Structures
Postorder Traversal
Trees *Property of STI Page 31 of 34
8/21/2019 Meljun Cortes Data Structures Trees
32/34
Data Structures
Postorder Traversal
Trees *Property of STI Page 32 of 34
8/21/2019 Meljun Cortes Data Structures Trees
33/34
Data Structures
Inorder Traversal Code
private void inOrder(node localRoot)
{if(localRoot != null)
{
inOrder(localRoot.leftChild);
localRoot.displayNode();
Trees *Property of STI Page 33 of 34
inOrder(localRoot.rightChild);
}
}
8/21/2019 Meljun Cortes Data Structures Trees
34/34
Data Structures
Exercise
Given the expression tree below, generate thethree traversal methods.