Binary Trees and Binary Search Trees Flashcards
how many children can a node have?
two children(left and right)
what is a node with no children called?
leaf node
a strict binary tree is what…
one that can have either 2 or 0 children
what makes a complete binary tree?
a tree where all levels are filled except possible the last and all nodes are as left as possible
what is the maximum number of nodes for every level i?
i = 2^i
what is the max # of nodes in a binary tree with height h
2^(h+1) -1
what is the perfect height of a binary tree given nodes?
h = log base 2 (n+1) -1
when do you have a balanced binary tree?
when the difference between the heights of the left and right subtrees for every node is not more than k(mostly 1)
what is the height of a binary tree?
the # of edges in longest path from root to a leaf
what is the height of an empty tree?
-1
what is the height of a tree with 1 node?
0
what is a binary search tree?
a binary tree in which for each node, the value of all the nodes in the left subtree is lesser or equal to and the value of all the nodes in the right subtree is greater.
how do you create the node class for a BST?
protected static class Node {
public E data; // The information stored in this node.
public Node left; //Reference to the left child
public Node right; //Reference to the right child.
public Node(E data) { this.data = data; left = null; right = null; }
how do you initiate the root node of BT?
protected Node root;
how do you set the root node of a class to be null?
public BinaryTree(){ //BinaryTree is the name of the class root=null; }
how do you set the root node of to equal this node?
public BinaryTree(Node root){ this.root=root; }
how do you set the root node of a BT to have data in it?
public BinaryTree(E data, BinaryTree leftTree, BinaryTree rightTree){ root = new Node<> data; if(leftTree.root != null){ root.left=leftTree.root; } else{ root.left=null; } if(rightTree!=null){ root.right=rightTree.root; } else{ rightTree=null; } }
how do you return the left subtree of a given tree?
public BinaryTree getLeftSubTree(){ if(root != null && root.left!=null){ return new BinaryTree(root.left); } else{ return null; } }
how do you return the right subtree of a given Binary Tree?
public BinaryTree getRightSubTree(){ if(root != null && root.left !=null){ return new BinaryTree(root.right); } else{ return null; } }
how do you return the data stored in the root node?
public E getData(){ if(root!=null){ return root.data; } else{ return null; } }
how do you find out if a subtree is a leaf node(has no children)?
public boolean isLeaf(){ return if(root!=null && (root.right==null && root.left=null); // will return true if the node is a leaf node,, false otherwise
what is preorder traversal?
pre order traversal is iterating through a node by going through its root, then its subtrees recursively
How do you perform a recursive pre-order traversal on a binary tree?
public void preOrderTraverse(){
preOrder(root);
}
public void preOrder(Node node){ if(node==null){ return; } else{ System.out.println(node.data + " "); preOrder(node.left); preorder(node.right); } }
what is post order traversal?
processing through all the subtrees, then processing through the root node