Final Flashcards
How many root nodes does a tree have?
One
A _____ consists of a collection of nodes connected by directed arcs.
Tree
A node that points to one or more other nodes…
Parent
What are the nodes that are pointed to?
Children
How many parents does every node have? (except root node)
One
What are nodes with no children
Leaf nodes
What are the nodes with children called?
Interior nodes
What are nodes that have the same parent?
Siblings
Consists of its children, and their children, and so on…
Descendants
A _____ rooted at a node consists of that node and all of its descendants.
Subtree
A path’s _____ is equal to the number of arcs traversed.
Length
A node’s _____ is equal to the maximum path length from that node to a leaf node.
Height
A leaf node has a height of…
0
The height of a tree is equal to…
The height of the root.
A node’s _____ is equal to the path length from the root to that node.
Depth
How many children can a node have?
Two
For a Full Binary Tree, every internal node has _ children.
2
For a Full Binary Tree, every leaf is at the same _____.
Depth
What is the height of nodes for a Full Binary Tree?
2^h+1 - 1
What is the height of leaves for a Full Binary Tree?
2^h
Write the structs for a Tree?
struct Node
{
TYPE val;
struct Node *left;
struct Node *rght;
};
structBSTree
{
struct Node *root;
int cnt;
};
If the tree is reasonably full, what is the search time for an element?
O(n)
Returns elements in sorted order.
In-order traversal
Write the implementation:
void add(struct BSTree *tree, TYPE val);
void add(struct BSTree *tree, TYPE val) {
tree->root = _addNode(tree->root, val);
tree->cnt++;
}
Write the implementation:
struct Node *_addNode(struct Node *cur, TYPE val);
struct Node *_addNode(struct Node *cur, TYPE val)
{
struct Node *newNode;
if(cur == NULL)
{
newnode = malloc(sizeof(struct Node));
assert (newnode != 0);
newnode->val = val;
newnode->left = newnode->right = 0;
return newnode;
}
if (val val)
cur->left = _addNode(cur->left,val); else cur->right = _addNode(cur->right, val); return cur;
}
Write the implementation:
void initBST(struct BinarySearchTree *tree);
void initBST(struct BinarySearchTree *tree) {
tree->size = 0;
tree->root = 0;
}
Write the implementation:
void addBST(struct BinarySearchTree *tree, TYPE newValue)
void addBST(struct BinarySearchTree *tree, TYPE newValue)
{
tree->root = _nodeAddBst(tree->root, newValue);
tree->size++;
}