








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Sengupta; Class: Data Structures & Algorithms; Subject: Computer Information Science; University: SUNY Institute of Technology at Utica-Rome; Term: Fall 2006;
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!
n ( 0 ) 1 n ( h ) 1 2 n ( h 1 )
n ( h ) 1 21 22 23 ... 2 h
public void insert(Object d){ if (d <data){ if (left == null) left = new TreeNode(d); else left.insert(d); } else if (d > data) { if (right == null) right= new TreeNode(d); else right.insert(d); } }
public class Tree { class TreeNode { TreeNode left; int data; TreeNode right; public TreeNode (int d) { data=d; left=right=null; } public void insert(int d){ if (d <data){ if (left == null) left = new TreeNode(d); else left.insert(d); } else if (d > data) { if (right == null) right= new TreeNode(d); else right.insert(d); } } } /* Now that a TreeNode is defined, we are going to build a binary tree. / private TreeNode root; / Construct an empty binary tree at the root / public Tree () {root=null;} / Insert a new node in the binary tree. If the root node is null, create the root node now. */ public void insertNode (int d){
if (root == null) root = new TreeNode(d); else root.insert(d); } private void inOrderDriver(TreeNode node){ if (node == null) return; else { inOrderDriver(node.left); System.out.print(" "+node.data+" -> "); inOrderDriver(node.right); } } private void preOrderDriver(TreeNode node){ if (node == null) return; else { System.out.print(" "+node.data+" -> "); preOrderDriver(node.left); preOrderDriver(node.right); } } private void postOrderDriver(TreeNode node){ if (node == null) return; else { postOrderDriver(node.left); postOrderDriver(node.right); System.out.print(node.data+" -> "); } } public void inOrderTraversal(){inOrderDriver(root);} public void preOrderTraversal(){preOrderDriver(root);} public void postOrderTraversal(){postOrderDriver(root);} public static void main(String args []){ Tree thisTree = new Tree(); int i, j, k; for (i=0; i<10; i++){
import java.io.; import java.util.; import java.lang.Math; public class Tree { class TreeNode { TreeNode left; int data; TreeNode right; public TreeNode (int d) { data=d; left=right=null; } public void insert(int d){ if (d <data){ if (left == null) left = new TreeNode(d); else left.insert(d); } else if (d > data) { if (right == null) right= new TreeNode(d);
else right.insert(d); } } } /* Now that a TreeNode is defined, we are going to build a binary tree. / private TreeNode root; / Construct an empty binary tree at the root / public Tree () {root=null;} / Insert a new node in the binary tree. If the root node is null, create the root node now. */ public void insertNode (int d){ if (root == null) root = new TreeNode(d); else root.insert(d); } private void inOrderDriver(TreeNode node){ if (node == null) return; else { inOrderDriver(node.left); System.out.print(" "+node.data+" -> "); inOrderDriver(node.right); } } public void inOrderTraversal(){inOrderDriver(root);} public int treeSize() {int k=treeSizeDriver(root); return k;} public int treeHeight() {int k=treeHeightDriver(root); return k;}
System.out.print(j+" "); thisTree.insertNode(j); } System.out.println("\n\n"); System.out.println("Height of this tree: "+ thisTree.treeHeight()+'\n'); System.out.println("Node count in this tree: "+ thisTree.treeSize()+'\n'); System.out.println("Leaf count of this tree: "+ thisTree.leafCount()+'\n'); // Traversal routine System.out.println("Inorder traversal profile: "); System.out.println(" "); thisTree.inOrderTraversal(); System.out.println(" "); System.out.println("\n *****************\n"); System.out.println("\n"); } }
Height of this tree: 6 Node count in this tree: 10 Leaf count of this tree: 4 Inorder traversal profile: 3 -> 12 -> 22 -> 50 -> 55 -> 58 -> 61 -> 85 -> 90 -> 93 ->
public void interchange() {interchangeDriver(root);} private void interchangeDriver(TreeNode r) { TreeNode tmp; if (r != null) { tmp=r.left; r.left=r.right; r.right=tmp; interchangeDriver(r.left); interchangeDriver(r.right); } }