cosc2007 data structures ii chapter 10 trees ii. 2 topics adt binary tree (bt) operations tree...
TRANSCRIPT
![Page 1: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/1.jpg)
COSC2007 Data Structures II
Chapter 10
Trees II
![Page 2: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/2.jpg)
2
Topics
ADT Binary Tree (BT) Operations Tree traversal
BT Implementation Array-based LL-based
Expression Notation and Tree traversal
![Page 3: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/3.jpg)
3
ADT Binary Tree
![Page 4: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/4.jpg)
4
ADT Binary Tree
Operations+CreateBinaryTree()// Creates an empty Binary Tree
+CretaeBinaryTree(in rootItem:TreeItemType)// Creates a one-node BT whose root contains RootItem
+CreateBT(in rootItem:TreeItemType,inout leftTree:BinaryTree,inout rightTree:BinaryTree)
// Creates a BT with root data RootItem and a leftTree and rightTree, // respectively as its left & right subtrees. Makes leftTree & rightTree empty// so they can’t be used to gain access to the new tree.
+destroyBinaryTree()// Destroys a binary tree
+isEmpty():boolean {query}// Determines whether a binary tree is empty
root:
![Page 5: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/5.jpg)
5
ADT Binary Tree Operations
+getRootData():TreeItemType throw TreeException// Returns the data item in the root of a nonempty subtree.// Throws an exception if the tree is empty
+setRootData(in newItem:TreeItemType) throw TreeException// Replaces the data item in the root of a binary tree with newItem, if the// tree is not empty, otherwise a root node is created with newItem as its data// item and is inserted in the tree. // If a new node can’t be created, TreeException is thrown
+attachLeft(in newItem: TreeItemType) throw TreeException// Attaches a left child containing newItem to the root of a binary tree.// Throws TreeException if the operation the binary tree is empty // or a left subtree already exists.
+attachRight(in newItem:TreeItemType) throw TreeException// Attaches a left child containing newItem to the root of a binary tree.// Throws TreeException if the operation the binary tree is empty // or a right subtree already exists.
root:tree1:
root:
tree1:
![Page 6: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/6.jpg)
6
ADT Binary Tree Operations+attachLeftSubtree(inout leftTree:BinaryTree) throw TreeException// Attaches leftTree as the left subtree of the root of a binary tree and makes // leftTree empty so that it can’t be used to access this tree. Throws// TreeException is the binary tree is empty or a left subtree already exists.
+attachRightSubtree(inout rightTree:BinaryTree) throw TreeException// Attaches rightTree as the right subtree of the root of a binary tree and makes // rightTree empty so that it can’t be used to access this tree. Throws// TreeException is the binary tree is empty or a right subtree already exists.
+detachLeftSubtree(out leftTree:BinaryTree) ThrowTreeException// Detaches the left subtree of a binary tree’s root. and retains it in // leftTree. Throws TreeException if the tree is empty.
+detachRightSubtree(out rightTree:BinaryTree) ThrowTreeException// Detaches the right subtree of a binary tree’s root. and retains it in // rightTree. Throws TreeException if the tree is empty.
![Page 7: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/7.jpg)
7
ADT Binary Tree Operations
+leftSubtree( ):BinaryTree// Returns a copy of the left subtree of a binary tree’s root without // detaching the subtree. Returns an empty tree if the binary tree is empty.
+rightSubtree( ):BinaryTree// Returns a copy of the right subtree of a binary tree’s root without // detaching the subtree. Returns an empty tree if the binary tree is empty.
+preorderTraverse(in visit:FunctionType)// Traverses a binary tree in preorder & // calls function visit() once for each node
+inorderTraverse(in visit:FunctionType)// Traverses a binary tree in inorder & // calls function visit() once for each node
+postorderTraverse(in visit:FunctionType)// Traverses a binary tree in postorder & // calls function visit() once for each node
![Page 8: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/8.jpg)
8
BT Traversal Suppose I wanted to ‘visit’ all the nodes in this tree. Furthermore, suppose I wanted to perform some
operation on the data there (such as printing it out).
A
B
D E
H I J K
C
F G
L
root
What are some of the possible visitation patterns we could use?
![Page 9: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/9.jpg)
9
BT Traversal Visit every node in a BT exactly once. Each visit performs the same operations on each
node Visiting sequence of nodes is of importance Natural traversal orders:
Pre-order Depth-First or Node-Left-Right (NLR)
In-order Symmetric or Left-Node-Right (LNR)
Post-order Left-Right-Node (LRN)
![Page 10: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/10.jpg)
10
Pre-order Traversal Each node is processed the first time it is
observed. i.e. before any node in either subtree. A Possible Pattern
Visit Root Node Visit Left subtree in preorder Visit Right subtree
Must be applied at all levels of the tree.
A
B
D E
H I J K
C
F G
L
root
4
3
5
2
7
6
8
1
10
9
11
12
Called an PreOrder Traversal
A B D H I E J K C F G L
![Page 11: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/11.jpg)
11
Pre-order Traversal
Pseudocodepreorder (binaryTree:BinaryTree )
// Traverses the binary tree binaryTree in preorder.
// Assumes that “visit a node” means to display the node’s data item
if (binaryTree is not empty )
{
Display the data in the root of binaryTree
preorder ( Left subtree of binaryTree ’s root )
preorder ( Right subtree of binaryTree ’s root )
}
![Page 12: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/12.jpg)
12
In-order Traversal Each node is processed the second time it is
observed. i.e. after all the nodes in its left subtree but before any of the nodes in its right subtree.
A Possible Pattern Visit Left subtree Visit Root node Visit Right subtree
Must be applied at all levels of the tree.
A
B
D E
H I J K
C
F G
L
root
1
2
3
4
5
6
7
8
9
10
11
12
H D I B J E K A F C G L
![Page 13: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/13.jpg)
13
In-order Traversal
Pseudocode inorder (binaryTree:BinaryTree )
// Traverses the binary tree binaryTree in inorder.
// Assumes that “visit a node” means to display the node’s data item
if (binaryTree is not empty )
{
inorder ( Left subtree of binaryTree ’s root )
Display the data in the root of binaryTree
inorder ( Right subtree of binaryTree ’s root )
}
![Page 14: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/14.jpg)
14
Post-order Traversal Each node is processed the third time it is
observed. i.e. after all nodes in both of its subtrees.
A Possible Pattern Visit Left subtree Visit Right subtree Visit Root Node
Must be applied at all levels of the tree.
A
B
D E
H I J K
C
F G
L
root
1
3
2
7
4
6
5
8
11
10
9
12
H I D J K E B F L G C A
![Page 15: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/15.jpg)
15
Post-order Traversal
Pseudocode postorder (binaryTree:BinaryTree ) // Traverses the binary tree binaryTree in postorder. // Assumes that “visit a node” means to display the node’s data itemif (binaryTree is not empty ){
postorder ( Left subtree of binaryTree ’s root )postorder ( Right subtree of binaryTree ’s root ) Display the data in the root of binaryTree
}
![Page 16: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/16.jpg)
16
Implementation of BT
Possible Implementations: Array based
Fast access Difficult to change Used if elements are not modified often
Reference (Pointer) based Slower Easy to modify More flexible when elements are modified often
![Page 17: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/17.jpg)
17
BT Array-Based Implementation
Nodes: A Java Node class Each node should contain:
Data portion Left-child index Right-child index
Tree: Array of structures For empty tree
Root's index = -1
JamesJames
TonyTonyBobBob
NeilNeilDavisDavisAdamAdam
![Page 18: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/18.jpg)
18
BT Array-Based Implementationpublic class TreeNode // node in the tree{ private Object item ; // data item in the tree private int leftChild; // index to left child private int rightChild; // index to right child
……. //constructor
} // end of TreeNode
public abstract class BinaryTreeArrayedBased {
protected final int MAX_NODES =100;protected TreeNode tree [];protected int root; // index of root
protected int free; // index of next unused array location ……..//constructor and methods} // end BinaryTreeArrayedBased
![Page 19: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/19.jpg)
19
BT Array-Based Implementation
ExampleItem Lchild RChild
O James 1 2 {root}1 Bob 3 42 Tony 5 -13 Adams -1 -14 Davis -1 -15 Neil -1 -16 ? -1 77 ? -1 88 ? -1 9
JamesJames
TonyTonyBobBob
NeilNeilDavisDavisAdamAdam
List of Free Nodes
![Page 20: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/20.jpg)
20
BT Array-Based Implementation
As insertion and/or deletion operations are performed, the elements of the tree might not be in consecutive locations in the array
A list of free nodes should be maintained ; When a node is deleted, it is added to the free list When a node is inserted, it is removed from the free
list If you know that the binary tree is complete, a
simpler array-based implementation can be used that contains no Lchild, or Rchild entries.
![Page 21: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/21.jpg)
21
BT Array-Based Implementation Since tree is complete, it maps nicely onto an array representation.
0 1 2 3 4 5 6 7 8 9 10 11 12
A B C D E F G H I J K LT:
last
A
B
D E
H I J K
C
F G
L
![Page 22: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/22.jpg)
22
BT Array-Based Implementation
For Complete Trees: A formula can be used to find the location of any
node in the tree
Lcild ( Tree [ i ] ) = Tree [ 2 * i + 1 ]
Rcild ( Tree [ i ] ) = Tree [ 2 * i + 2 ]
Parent (Tree [ i ] ) = Tree [ ( i - 1) / 2 ]
0123456 Neil
David
AdamsTonyBob
James
JamesJames
TonyTonyBobBob
NeilNeilDavisDavisAdamAdam
![Page 23: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/23.jpg)
23
Reference-Based Implementation
Typical for implementing BTs Different constructors to allow defining the
BT in a variety of circumstances Only the pointer to the root of the tree can
be accessed by BT class clients The data structure would be private data
member of a class of binary trees
![Page 24: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/24.jpg)
24
Reference -Based Implementation
public class TreeNode // node in the tree{ private Object item; // data portion private TreeNode leftChildPtr; // pointer to left child private TreeNode rightChildPtr; // pointer to right child
TreeNode() {}; TreeNode(Object nodeItem, TreeNode left , TreeNode right ) { }…….} // end TreeNode class
![Page 25: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/25.jpg)
25
Reference -Based Implementation
public abstract class BinaryTreeBasis {
protected TreeNode root;……. // constructor and methods
} // end BinaryTreeBasis class
If the tree is empty, then root is null The root of a nonempty BT has a left subtree and right
subtree, each is BT root.getRight () root.getLeft()
![Page 26: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/26.jpg)
26
Reference -Based Implementation
Example:
Item
LChildPtr RChildPtr
Root
Root of left child subtree
Root of right child subtree
![Page 27: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/27.jpg)
27
The TreeNode Classpublic class TreeNode {
private Object item;
private TreeNode leftChild;
private TreeNode rightChild;
public TreeNode() {} //Constructors
public TreeNode(Object myElement) {
item = myElement;
leftChild = null;
rightChild = null;
}
public TreeNode(Object newItem, TreeNode left, TreeNode right){ item = newItem;
leftChild = left;
rightChild = right;
}
![Page 28: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/28.jpg)
28
TreeNode, cont'd.public Object getItem()
{ return item;}
public void setItem(Object newItem)
{item = newItem;}
public void setLeft(TreeNode left)
{ leftChild = left;}
public TreeNode getLeft()
{ return leftChild;}
public void setRight(TreeNode right)
{ rightChild = right;}
public TreeNode getRight()
{ return rightChild;}
}
![Page 29: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/29.jpg)
29
TreeException
public class TreeException extends RuntimeException
{
public TreeException(String s) { super (s);}
} // end TreeException
![Page 30: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/30.jpg)
30
The BinaryTreeBasis classpublic abstract class BinaryTreeBasis {
protected TreeNode root;
public BinaryTreeBasis () {
root = null;
} //end constructor
public BinaryTreeBasis (object rootItem) {
root = new TreeNode (rootItem, null, null);
} //end constructor
public boolean isEmpty() { //true is tree is empty
return root == null;
}
public void makeEmpty() { //sets root of tree to null
root =null;
}
![Page 31: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/31.jpg)
31
The BinaryTreeBasis class
public Object getRootItem() throws TreeException { //returns element at root if it exists
if (root == null)
throw new TreeException (“ Empty tree”);
else
return root.getItem();
} //end getRootItem()
public TreeNode getRoot() {
return root;
} //end getRoot()
} //end class BinaryTreeBasis
t: 1
![Page 32: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/32.jpg)
32
The BinaryTree Classpublic class BinaryTree extend BinaryTreeBasis{
//the Constructorspublic BinaryTree() {}public BinaryTree(Object rootItem) {
super (rootItem);} //end constructorpublic BinaryTree(Object rootItem, BinaryTree leftTree, BinaryTree rightTree) {
root = new TreeNode(rootItem, null, null);attachLeftSubTree (leftTree);attachRightSubTree (rightTree);
} // end constructor
••••••••all the methods go here} //end BinaryTree
t: 1
![Page 33: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/33.jpg)
33
The BinaryTree Class
public void setRootItem(Object newItem) {if (root == null)
root = new TreeNode (newItem, null, null);
else root.setItem(newItem);
} //end setRootItem
tree: 2
4 5
![Page 34: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/34.jpg)
34
The BinaryTree Class
public void attachLeft(Object newItem) {
if (!isEmpty() && root.getLeft() == null) root.setLeft(new TreeNode(newItem, null, null));} // end attachLeft
public void attachRight(Object newItem) { if (!isEmpty() && root.getRight() == null) root.setRight(new TreeNode(newItem, null, null));} // end attachRight
t 1
2 3
t: 1
![Page 35: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/35.jpg)
35
The BinaryTree Classpublic void attachLeftSubtree(BinaryTree leftTree) throws TreeException{ if (isEmpty()) throw new TreeException("Cannot attach left
subtree to empty tree."); else if (root.getLeft() != null) throw new
TreeException("Cannot overwrite left subtree."); else { //no empty tree, no left child root.setLeft(leftTree.root); leftTree.makeEmpty(); } //end attachLeftSubtree
}
t: 1
3
6
7 8
2
4 5
t: 2
4 5
tree1:3
6
7 8
![Page 36: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/36.jpg)
36
The BinaryTree Classpublic void attachRightSubtree(BinaryTree rightTree) throws TreeException{ if (isEmpty()) throw new TreeException("Cannot attach left subtree to empty tree."); else if (root.getRight() != null) throw new TreeException("Cannot overwrite right subtree."); else { //no empty tree, no right child root.setRight(rightTree.root); rightTree.makeEmpty(); } //end attachRightSubtree
}
![Page 37: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/37.jpg)
37
The BinaryTree Classprotected BinaryTree (TreeNode rootNode) {
root = rootNode} //end constructor
public BinaryTree detachLeftSubtree() throws TreeException{ if (isEmpty()) throw new TreeException("Cannot detach empty tree.");
else { // create a tree points to the leftsubtreeBinaryTree leftTree;leftTree = new BinaryTree (root.getLeft());root.setLeft (null);return leftTree;
} } //end detachLeftSubtree
}
![Page 38: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/38.jpg)
38
The BinaryTree Class
public BinaryTree detachRightSubtree() throws TreeException{ if (isEmpty()) throw new TreeException("Cannot detach empty tree.");
else { // create a tree points to the rightsubtreeBinaryTree rightTree;rightTree = new BinaryTree (root.getRight());root.setRight (null);return rightTree;
} } //end detachRightSubtree
}
![Page 39: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/39.jpg)
39
preOrderPrint( ) public void preOrderPrint(TreeNode rootNode)
throws TreeException
{
if (rootNode!=null)
{
System.out.print(rootNode.getItem() + " ");
preOrderPrint(rootNode.getLeft());
preOrderPrint(rootNode.getRight());
}
}
Print tree using preorder traversal:1 2 4 5 3 6 7 8
t: 1
3
6
7 8
2
4 5
![Page 40: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/40.jpg)
40
inOrderPrint()
Print tree using inorder traversal:4 2 5 1 7 6 8 3
t: 1
3
6
7 8
2
4 5
public void inOrderPrint(TreeNode rootNode) throws TreeException
{
if (rootNode!=null)
{
inOrderPrint(rootNode.getLeft());
System.out.print(rootNode.getItem() + " ");
inOrderPrint(rootNode.getRight());
}
}
![Page 41: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/41.jpg)
41
postOrderPrint( )
Print tree using postOrder traversal:4 5 2 7 8 6 3 1
t: 1
3
6
7 8
2
4 5
public void postOrderPrint(TreeNode rootNode) throws TreeException
{
if (rootNode!=null)
{
postOrderPrint(rootNode.getLeft());
postOrderPrint(rootNode.getRight());
System.out.print(rootNode.getItem() + " ");
}
}
![Page 42: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/42.jpg)
42
An Example Programpublic class BinaryTreeTest{
public static void main (String args[]) throws BinaryTreeException{ BinaryTree t = new BinaryTree(new Integer(1)); System.out.println("Element at root of tree = " + t.getRootElement()); System.out.println("Initial tree is: "); t.reOrderPrint(t.getRoot()); System.out.println(); BinaryTree tree1 = new BinaryTree(new Integer(2)); tree1.attachLeft(new Integer(4)); tree1.attachRight(new Integer(5)); System.out.println("Tree1 is: "); tree1.preOrderPrint(tree1.getRoot()); System.out.println();
tree1: 2
4 5
t: 1
![Page 43: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/43.jpg)
43
Example, cont'd. BinaryTree tree2 = new BinaryTree(new Integer(6));
tree2.attachLeft(new Integer(7)); tree2.attachRight(new Integer(8)); System.out.println("tree2 is: "); tree2.preOrderPrint(tree2.getRoot()); System.out.println(); BinaryTree tree3 = new BinaryTree(new Integer(3)); tree3.attachLeftSubtree(tree2); System.out.println("Tree3 is: "); tree3.preOrderPrint(tree3.getRoot()); System.out.println();
t.attachLeftSubtree(tree1); t.attachRightSubtree(tree3); System.out.println("Final tree is: "); t.preOrderPrint(t.getRoot()); System.out.println();
}}
tree2: 6
7 8
tree3:3
6
7 8
t: 1
3
6
7 8
2
4 5
![Page 44: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/44.jpg)
44
Expression Tree
Given a math expression, there exists a unique corresponding expression tree.
5+ (6* (8-6)) +
5 *
6 -
8 6
![Page 45: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/45.jpg)
45
Preorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives prefix form of expression!
/ * + a b - c d + e f
![Page 46: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/46.jpg)
46
Inorder By Projection
a
b c
d ef
g h i j
g d h b e i a f j c
![Page 47: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/47.jpg)
47
Inorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives infix form of expression (sans parentheses)!ea + b * c d / + f-
![Page 48: COSC2007 Data Structures II Chapter 10 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression](https://reader035.vdocument.in/reader035/viewer/2022062618/5514254a550346d8488b59ee/html5/thumbnails/48.jpg)
48
Postorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives postfix form of expression!
a b + c d - * e f + /