3
class AST
{
private:
    TreeNode **a;       
    int capacity;        
public:                       
void deletenode(const int &i);        

That's my AST tree class(TreeNode is an abstract class).

let's pretend a[t] is a[i]'s left child, after the call to deletenode(i), a[t] and its left child should be deleted while a[t]'s right child should become a[i]'s new left child.

I'm really clueless, I should have said "here's what I tried" but I'm going to say here's what I thought of :

1-Build a new tree from a[t]'s right child(and its children) and then inserting the new tree at left of a[i], I have a function to insert a tree to a tree but this is a very poor, slow algorithm.

2-Thought of some relation to apply on every node so that a[i]=a[f(i)] and then delete a[f(i)] but I didn't find any f(i) and there are some nodes which are unaffected by the delete.

What I need is the algorithm, nothing else, this as its name implies is for a compiler project, deletion is important for optimization, I need it to be based on an array because breadth-first pass algorithm is a lot simpler with it.

A question similar to mine

Delete operation in Array Binary Tree. However, it doesn't answer my question and does not say any thing about rotation.

And there are many questions about array-based binary trees but not about delete and rotate.

So is There a fast algorithm to do that ? Thanks for any help.

PS

The root is a[0] not a[1]. so the left child of a[i] is a[2*i+1] and the right is a[2*i+2].

Community
  • 1
  • 1
niceman
  • 2,653
  • 29
  • 57

0 Answers0