Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes on Binary Tree - Data Structures and Algorithms | CS 240, Study notes of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-2rg-1
koofers-user-2rg-1 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Tree.
A binary tree is either an empty tree or a tree
comprising a root node, left subtree and a right
subtree.
This is an empty binary tree at right.
Some more binary trees:
Every binary tree can have at most two subtrees: left
and right. The notion of parent, children, grand-
parent, grand-children. Basically,
Single node binary
tree.
Two-node binary
trees.
Seven-node binary
tree.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Notes on Binary Tree - Data Structures and Algorithms | CS 240 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Binary Tree.

A binary tree is either an empty tree or a tree

comprising a root node, left subtree and a right

subtree.

This is an empty binary tree at right.

Some more binary trees:

Every binary tree can have at most two subtrees: left

and right. The notion of parent, children, grand-

parent, grand-children. Basically,

Single node binary

tree.

Two-node binary

trees.

Seven-node binary

tree.

A tree, if it’s not empty, begins at a root which is at

level 0. The children of the root are at level 1, and

their children are at the next level, and so on.

The height of a tree is the maximum level that any of

its node attains. Also, recursively,

height(tree) = 1 + max (height(tree.left), height(tree.right))

A complete binary tree is one where every node

except those at the deepest layer (level) has precisely

two children.

Let n (^ h ) = total number of nodes in a complete binary

tree of height h.^ Then,

n ( 0 ) 1 n ( h ) 1  2 n ( h  1 )

This shows that

n ( h ) 1  21  22  23 ... 2 h

 2 h ^1  1

From this, we conclude:

parent

parent

Left Child

Left Child

Right child

Child

Right child

Child

Unless indicated otherwise, our binary trees are going

to be created as binary search trees.

Check textbook or any reference book how to insert

nodes (objects) in a binary (search) tree.

A typical TreeNode of a binary tree is a class

class TreeNode {

TreeNode left;

Object data;

TreeNode right;

public TreeNode (Object d) {

data=d;

left=right=null;

The method insert that inserts a node in the tree is

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); } }

Method of traversals. A traversal of a list or a tree is

the specification of the visit sequence of its nodes. In

binary trees, three major ways tree nodes could be

visited are: (in pseudo codes)

Preorder_traversal ::

If (root !=null) {

print (root.data);

preorder_traversal(root.left);

preorder_traversal(root.right);

Inorder_traversal ::

If (root !=null) {

inorder_traversal(root.left);

print(root.data);

inorder_traversal(root.right);

Postorder_traversal ::

If (root !=null) {

postorder_traversal(root.left);

postorder_traversal(root.right);

print(root.data);

Other traversals could also be designed and under

some circumstances they might offer more appeal.

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++){

Introducing more methods for binary tree.

a. Compute the height of a binary tree recursively

b. Compute the number of nodes in a tree

c. Compute the number of leaves in a tree

d. Compute the number of single child parents

e. Compute the number of two children parents

We do the first three here!

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"); } }

OUTPUT:

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 ->


Let’s try to design a function that would

interchange the two subtrees of a binary tree.

Example.

The code for this:

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); } }

A

A

B

B

C

C

D

D

E

E

A

A

B

B

C

C

D

D

E

E