0

When I am adding a value to an AVL tree, how do I know where to insert it? I'm not talking about the coding aspect, but more of a pictorial representation. I'm assuming the insertion is done at the first available external node, but I'm unsure, as the example given in my textbook inserts at a random external node.

jsan
  • 1,047
  • 8
  • 20
  • 33

1 Answers1

0

AVL tree is a self-balancing binary search tree and binary search trees have following property: the left subtree of every node contains only nodes with keys less than the node's key and the right subtree of every node contains only nodes with keys greater than the node's key. So you have to insert new node in such way that stated property remains after insertion: you start with root node and working towards leaf by comparing new node key and current node key if the new node key is greater you visit right child else you visit left child next and when there is no child to visit in that place you insert new node.

Bula
  • 1,590
  • 1
  • 14
  • 33